-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
51 lines (37 loc) · 996 Bytes
/
example.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
var WebSocket = require('./ews');
var Promise = require('bluebird').Promise;
var wsServer = new WebSocket.Server({ port: 23200 });
var ws = new WebSocket('ws://localhost:23200');
wsServer.on('connection', function(wsClient) {
wsClient.sendRequest('config').then(function(config) {
console.log('Got config', config);
});
setInterval(function() {
wsClient.sendRequest('ping').then(function(data) {
console.log('Got', data);
});
}, 1000)
wsClient.onRequest('version', function() {
return '1.0';
});
wsClient.sendEvent('testEvent', {data: 'test'});
});
ws.on('open', function() {
ws.sendRequest('version').then(function(version) {
console.log('version', version);
});
});
ws.onRequest('config', function() {
// you can return a promise here or a value
return {
key: 'value'
};
});
ws.onRequest('ping', function() {
return Promise.delay(100).then(function() {
return 'pong'
});
});
ws.onEvent('testEvent', function(ev) {
console.log('Got event', ev);
});