forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (117 loc) · 3.32 KB
/
Copy pathserver.js
File metadata and controls
146 lines (117 loc) · 3.32 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var fs = require('fs');
var server;
if (process.env.SSL) {
server = require('https').createServer({
key: fs.readFileSync(__dirname + '/key.pem'),
cert: fs.readFileSync(__dirname + '/cert.pem')
});
} else {
server = require('http').createServer();
}
var io = require('socket.io')(server, {
pingInterval: 2000,
wsEngine: 'ws'
});
var port = process.env.PORT || 3000;
var nsp = process.argv[2] || '/';
var slice = Array.prototype.slice;
const fooNsp = io.of('/foo');
fooNsp.on('connection', (socket) => {
socket.on('room', (...args) => {
fooNsp.to(socket.id).emit.apply(fooNsp, ['roomBack'].concat(args));
});
});
io.of('/timeout_socket').on('connection', function() {
// register namespace
});
io.of('/valid').on('connection', function() {
// register namespace
});
io.of('/asd').on('connection', function() {
// register namespace
});
io.of('/abc').on('connection', function(socket) {
socket.emit('handshake', socket.handshake);
});
io.of("/no").use((socket, next) => {
const err = new Error("auth failed");
err.data = { a: "b", c: 3 };
next(err);
});
io.of(nsp).on('connection', function(socket) {
socket.send('hello client');
socket.on('message', function() {
var args = slice.call(arguments);
socket.send.apply(socket, args);
});
socket.on('echo', function() {
var args = slice.call(arguments);
socket.emit.apply(socket, ['echoBack'].concat(args));
});
socket.on('ack', function() {
var args = slice.call(arguments);
var callback = args.pop();
callback.apply(null, args);
});
socket.on('callAck', function() {
socket.emit('ack', function() {
var args = slice.call(arguments);
socket.emit.apply(socket, ['ackBack'].concat(args));
});
});
socket.on('callAckBinary', function() {
socket.emit('ack', function(buf) {
socket.emit('ackBack', buf);
});
});
socket.on('getAckBinary', function(data, callback) {
var buf = new Buffer('huehue', 'utf8');
callback(buf);
});
socket.on('getAckDate', function(data, callback) {
callback(new Date());
});
socket.on('broadcast', function(data) {
var args = slice.call(arguments);
socket.broadcast.emit.apply(socket, ['broadcastBack'].concat(args));
});
socket.on('room', (arg) => {
io.to(socket.id).emit("roomBack", arg);
});
socket.on('requestDisconnect', function() {
socket.disconnect();
});
socket.on('disconnect', function() {
console.log('disconnect');
});
socket.on('error', function() {
console.log('error: ', arguments);
});
socket.on('getHandshake', function(cb) {
cb(socket.handshake);
});
});
function before(context, name, fn) {
var method = context[name];
context[name] = function() {
fn.apply(this, arguments);
return method.apply(this, arguments);
};
}
before(io.engine, 'handleRequest', function(req, res) {
// echo a header value
var value = req.headers['x-socketio'];
if (!value) return;
res.setHeader('X-SocketIO', value);
});
before(io.engine, 'handleUpgrade', function(req, socket, head) {
// echo a header value for websocket handshake
var value = req.headers['x-socketio'];
if (!value) return;
this.ws.once('headers', function(headers) {
headers.push('X-SocketIO: ' + value);
});
});
server.listen(port, function() {
console.log('Socket.IO server listening on port', port);
});