/
node-client.js
40 lines (36 loc) · 1.46 KB
/
node-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
/**
* Project: wiola
* User: KSDaemon
* Date: 21.10.16
*/
const Wampy = require('wampy').Wampy;
const w3cws = require('websocket').w3cwebsocket;
let ws, d = Date.now();
console.log('0: Initializing wampy and connecting to server...');
ws = new Wampy('ws://webxp/ws/', {
ws: w3cws,
realm: 'test',
onConnect: function () {
console.log('+' + (Date.now() - d) + 'ms: Yahoo! We are online!');
ws.subscribe('message.received', {
onSuccess: function () {
console.log('+' + (Date.now() - d) + 'ms: Successfully subscribed to topic');
global.setTimeout(function () {
ws.publish('message.received', ['New message'], null, { exclude_me: false });
}, 5000);
},
onError: function (err) { console.log('+' + (Date.now() - d) + 'ms: Subscription error:' + err); },
onEvent: function (res) {
console.log('+' + (Date.now() - d) + 'ms: Received new message!');
console.log('+' + (Date.now() - d) + 'ms: Closing connection...');
ws.disconnect();
}
});
},
onClose: function () {
console.log('+' + (Date.now() - d) + 'ms: Connection to WAMP server closed!');
},
onError: function (err) { console.log('Breakdown happened! ', err); },
onReconnect: function () { console.log('Reconnecting...'); },
onReconnectSuccess: function () { console.log('Reconnection succeeded...'); }
});