-
Notifications
You must be signed in to change notification settings - Fork 4
/
xmpp.js
104 lines (89 loc) · 2.51 KB
/
xmpp.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var connection = null;
var ignore = true;
var current_page = 0;
function log(msg)
{
$('#log').append('<div></div>').append(document.createTextNode(msg));
}
function rawInput(data)
{
log('RECV: ' + data);
}
function rawOutput(data)
{
log('SENT: ' + data);
}
function onEvent(message) {
var server = "^"+PUBSUB_SERVER.replace(/\./g, "\\.");
var re = new RegExp(server);
// Only handle messages from the PubSub Server.
if ($(message).attr('from').match(re)) {
// Grab pubsub entry page number
var event = $(message).children('event')
.children('items')
.children('item')
.children('entry').text();
if (ignore) {
//short circuit first event
ignore = false;
return true;
}
go_page = parseInt(event); // The event should be the current page #
if (go_page >= 0) { // Only handle page # events
// I would have liked to use goTo but the function would cause and odd
// jump to the home page then the correct page. So I added a bit off
// logic to make it look good when transitioning pages.
if (current_page+1 == go_page) {
go(1);
} else if (current_page-1 == go_page) {
go(-1);
} else {
goTo(go_page);
}
current_page = go_page;
}
}
// Return true or we loose this callback.
return true;
}
function onSubscribe(sub) {
// Log when we are subscribed.
log("Subscribed");
return true;
}
function onConnect(status)
{
// This function is taken from the basic example and
// when we are connected we send presence and subscribe to
// the presentation node.
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.send($pres());
connection.pubsub.subscribe(connection.jid,
PUBSUB_SERVER,
PUBSUB_NODE,
[],
onEvent,
onSubscribe
);
}
return true;
}
$(document).ready(function () {
connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
connection.connect(XMPP_SERVER,
null,
onConnect);
});