aboutsummaryrefslogtreecommitdiffstats
path: root/webclient/lib/basic.js
blob: 8ea5b5b4cb334f25ea8c6812bdc6437119782b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var BOSH_SERVICE = 'http://alias.fr.nf/http-bind/';
var connection = null;

function log(msg) 
{
    $('#log').append('<div></div>').append(document.createTextNode(msg));
}

function rawInput(data)
{
    log('RECV: ' + data);
}

function rawOutput(data)
{
    log('SENT: ' + data);
}

function onConnect(status)
{
    var jid = $('#jid').get(0).value;
    if (status == Strophe.Status.CONNECTING) {
        log('Strophe is connecting.');
    }
    else if (status == Strophe.Status.CONNFAIL) {
        log('Strophe failed to connect.');
        $('#connect').get(0).value = 'connect';
    }
    else if (status == Strophe.Status.DISCONNECTING) {
        log('Strophe is disconnecting.');
    }
    else if (status == Strophe.Status.DISCONNECTED) {
        log('Strophe is disconnected.');
        $('#connect').get(0).value = 'connect';
    }
    else if (status == Strophe.Status.CONNECTED) {
        log('Strophe is connected.');
        connection.addHandler(onRoster,Strophe.NS.ROSTER,'iq','result',null, null);
        connection.send($pres().tree());
        var roster = $iq({type:'get'}).c('query',{xmlns: Strophe.NS.ROSTER });
        connection.send(roster.tree());
    }
}

function onRoster(iq)
{
    var elems = iq.getElementsByTagName('query');
    var query = elems[0];
    Strophe.forEachChild(query,'item',function (item){
        var name = item.getAttribute('jid');
        $('#contacts > ul').append('<li>'+name+'</li>');
    });
    return true;
}



$(document).ready(function () {
    connection = new Strophe.Connection(BOSH_SERVICE);
    connection.rawInput = rawInput;
    connection.rawOutput = rawOutput;

    $('#connect').bind('click', function () {
	var button = $('#connect').get(0);
	if (button.value == 'connect') {
	    button.value = 'disconnect';

	    connection.connect($('#jid').get(0).value,
			       $('#pass').get(0).value,
			       onConnect);
	} else {
	    button.value = 'connect';
	    connection.disconnect();
	}
    });
});