-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathxmpp_client.js
97 lines (82 loc) · 2.12 KB
/
xmpp_client.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
'use strict'
/*
* example usage:
*
* node examples/answer_bot.js [email protected] password component.evilprofessor.co.uk
*
* Chats with the example component in example/send_message_component.js
*/
var Client = require('node-xmpp-client')
var component = '[email protected]'
var client = new Client({
jid: '[email protected]',
password: '****',
host: 'isj3cmx.webexconnect.com',
reconnect: true
})
var x = 0
var old = x
var average = 0
setInterval(function () {
var n = x - old
console.log(n, average)
average = (n + average) * 0.5
old = x
}, 1e3)
var interval
var firstMessage = true
var c = 0
client.on('stanza', function (stanza) {
console.log('Received stanza: ', c++, stanza.toString())
if (stanza.is('message') && stanza.attrs.type === 'chat' && !stanza.getChild('delay')) {
clearInterval(interval)
if (firstMessage) console.log('Someone started chatting …')
firstMessage = false
var i = parseInt(stanza.getChildText('body'), 10)
x = i
var reply = new Client.Stanza('message', {
to: stanza.attrs.from,
from: stanza.attrs.to,
type: 'chat'
})
reply.c('body').t(isNaN(i) ? 'i can count!' : ('' + (i + 1)))
setTimeout(function () {
client.send(reply)
}, 321)
}
})
client.on('online', function () {
console.log('Client is online')
firstMessage = true
client.send('<presence/>')
interval = setInterval(function () {
if (!firstMessage) return
// firstMessage = false
console.log('Start chatting …')
var reply = new Client.Stanza('message', {
to: component,
type: 'chat'
})
reply.c('body').t('0')
client.send(reply)
}, 321)
})
client.on('offline', function () {
console.log('Client is offline')
})
client.on('connect', function () {
console.log('Client is connected')
})
client.on('reconnect', function () {
console.log('Client reconnects …')
})
client.on('disconnect', function (e) {
console.log('Client is disconnected', client.connection.reconnect, e)
})
client.on('error', function (e) {
console.error(e)
process.exit(1)
})
process.on('exit', function () {
client.end()
})