forked from mars1211/rust-socketio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket-io.js
62 lines (57 loc) · 1.95 KB
/
socket-io.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
const server = require('http').createServer();
const io = require('socket.io')(server);
console.log('Started');
var callback = client => {
console.log('Connected!');
client.on('test', data => {
// Send a message back to the server to confirm the message was received
client.emit('test-received', data);
console.log(['test', data]);
});
client.on('message', data => {
client.emit('message-received', data);
console.log(['message', data]);
});
client.on('test', function (arg, ack) {
console.log('Ack received')
if (ack) {
ack('woot');
}
});
client.on('binary', data => {
var bufView = new Uint8Array(data);
console.log(['binary', 'Yehaa binary payload!']);
for (elem in bufView) {
console.log(['binary', elem]);
}
client.emit('binary-received', data);
console.log(['binary', data]);
});
client.on('binary', function (arg, ack) {
console.log(['binary', 'Ack received, answer with binary'])
if (ack) {
ack(Buffer.from([1, 2, 3]));
}
});
// This event allows the test framework to arbitrarily close the underlying connection
client.on('close_transport', data => {
console.log(['close_transport', 'Request to close transport received'])
// Close underlying websocket connection
client.client.conn.close();
})
client.emit('Hello from the message event!');
client.emit('test', 'Hello from the test event!');
client.emit(Buffer.from([4, 5, 6]));
client.emit('test', Buffer.from([1, 2, 3]));
client.emit('This is the first argument', 'This is the second argument', {
argCount: 3
});
client.emit('on_abc_event', '', {
abc: 0,
some_other: 'value',
});
};
io.on('connection', callback);
io.of('/admin').on('connection', callback);
// the socket.io client runs on port 4201
server.listen(4200);