source: sandbox/jabberit_messenger/trophy_expresso/strophejs/examples/echobot.js @ 2397

Revision 2397, 2.3 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Importacao do modulo trophy integrado ao expresso.

  • Property svn:executable set to *
Line 
1var BOSH_SERVICE = '/xmpp-httpbind';
2var connection = null;
3
4function log(msg)
5{
6    $('#log').append('<div></div>').append(document.createTextNode(msg));
7}
8
9function onConnect(status)
10{
11    if (status == Strophe.Status.CONNECTING) {
12        log('Strophe is connecting.');
13    } else if (status == Strophe.Status.CONNFAIL) {
14        log('Strophe failed to connect.');
15        $('#connect').get(0).value = 'connect';
16    } else if (status == Strophe.Status.DISCONNECTING) {
17        log('Strophe is disconnecting.');
18    } else if (status == Strophe.Status.DISCONNECTED) {
19        log('Strophe is disconnected.');
20        $('#connect').get(0).value = 'connect';
21    } else if (status == Strophe.Status.CONNECTED) {
22        log('Strophe is connected.');
23        log('ECHOBOT: Send a message to ' + connection.jid +
24            ' to talk to me.');
25
26        connection.addHandler(onMessage, null, 'message', null, null,  null);
27        connection.send($pres().tree());
28    }
29}
30
31function onMessage(msg) {
32    var to = msg.getAttribute('to');
33    var from = msg.getAttribute('from');
34    var type = msg.getAttribute('type');
35    var elems = msg.getElementsByTagName('body');
36
37    if (type == "chat" && elems.length > 0) {
38        var body = elems[0];
39
40        log('ECHOBOT: I got a message from ' + from + ': ' +
41            Strophe.getText(body));
42   
43        var reply = $msg({to: from, from: to, type: 'chat'})
44            .cnode(Strophe.copyElement(body));
45        connection.send(reply.tree());
46
47        log('ECHOBOT: I sent ' + from + ': ' + Strophe.getText(body));
48    }
49
50    // we must return true to keep the handler alive. 
51    // returning false would remove it after it finishes.
52    return true;
53}
54
55$(document).ready(function () {
56    connection = new Strophe.Connection(BOSH_SERVICE);
57
58    // Uncomment the following lines to spy on the wire traffic.
59    //connection.rawInput = function (data) { log('RECV: ' + data); };
60    //connection.rawOutput = function (data) { log('SEND: ' + data); };
61
62    // Uncomment the following line to see all the debug output.
63    //Strophe.log = function (level, msg) { log('LOG: ' + msg); };
64
65
66    $('#connect').bind('click', function () {
67        var button = $('#connect').get(0);
68        if (button.value == 'connect') {
69            button.value = 'disconnect';
70
71            connection.connect($('#jid').get(0).value,
72                               $('#pass').get(0).value,
73                               onConnect);
74        } else {
75            button.value = 'connect';
76            connection.disconnect();
77        }
78    });
79});
Note: See TracBrowser for help on using the repository browser.