-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
46 lines (37 loc) · 979 Bytes
/
test.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
'use strict';
var sockjs = require('sockjs')
, http = require('http')
, SockJS = require('./');
//
// Setup a Sockjs server so we can listen for incoming connections and regular
// HTTP server to listen for incoming requests.
//
var realtime = sockjs.createServer()
, server = http.createServer();
realtime.on('connection', function (socket) {
socket.on('data', function echo(data) {
socket.write(data);
});
});
realtime.installHandlers(server, {
prefix: '/test'
});
//
// Listen to the server.
//
server.listen(8123, function listening() {
var socket = new SockJS('http://localhost:8123/test')
, timeout;
socket.onopen = function onopen() {
socket.send('foo');
};
socket.onmessage = function onmessage(evt) {
if ('foo' !== evt.data) throw new Error('Invalid message');
clearTimeout(timeout);
socket.close();
server.close();
};
timeout = setTimeout(function timeout() {
throw new Error('Timeout');
}, 1000);
});