-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (50 loc) · 1.43 KB
/
index.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
var express = require('express')
, app = express()
, http = require('http').Server(app)
var io = require('socket.io')(http)
, ltx = require('ltx')
, Component = require('node-xmpp-component')
, BusinessLogic = require('./src/index')
app.use(express.static('public'))
var socket = null
var logic = new BusinessLogic()
var component = new Component({
jid: process.env.JID,
password: process.env.PASSWORD,
host: process.env.HOST || 'localhost',
port: process.env.PORT || 5347
})
component.on('connect', function() {
console.log('Component is connected')
})
component.on('error', function(error) {
console.log('Component error: ' + error)
})
component.on('stanza', function(stanza) {
console.log('Received stanza at component: ' + stanza.toString())
if (socket) {
return socket.emit('stanza', stanza.toString())
}
logic.process(stanza, function(stanza) {
if (!stanza) return
component.send(stanza)
})
})
io.on('connection', function(newSocket) {
if (socket) {
return newSocket.emit('disconnect')
}
console.log('We have a new socket, diverting stanzas')
socket = newSocket
socket.on('disconnect', function() {
console.log('Websocket connection closed')
socket = null
})
socket.on('stanza', function(stanza) {
console.log('Received stanza from websocket: ' + stanza)
component.send(ltx.parse(stanza))
})
})
http.listen(3000, function(){
console.log('listening on *:3000')
})