/
libwebsockets-test-server.js
executable file
·186 lines (158 loc) · 6.3 KB
/
libwebsockets-test-server.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
/************************************************************************
* Copyright 2010-2015 Brian McKelvey.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***********************************************************************/
var WebSocketServer = require('../../lib/WebSocketServer');
var WebSocketRouter = require('../../lib/WebSocketRouter');
var http = require('http');
var fs = require('fs');
var args = { /* defaults */
secure: false
};
/* Parse command line options */
var pattern = /^--(.*?)(?:=(.*))?$/;
process.argv.forEach(function(value) {
var match = pattern.exec(value);
if (match) {
args[match[1]] = match[2] ? match[2] : true;
}
});
args.protocol = args.secure ? 'wss:' : 'ws:';
if (!args.port) {
console.log('WebSocket-Node: Test Server implementing Andy Green\'s');
console.log('libwebsockets-test-server protocols.');
console.log('Usage: ./libwebsockets-test-server.js --port=8080 [--secure]');
console.log('');
return;
}
if (args.secure) {
console.log('WebSocket-Node: Test Server implementing Andy Green\'s');
console.log('libwebsockets-test-server protocols.');
console.log('ERROR: TLS is not yet supported.');
console.log('');
return;
}
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
if (request.url === '/') {
fs.readFile('libwebsockets-test.html', 'utf8', function(err, data) {
if (err) {
response.writeHead(404);
response.end();
}
else {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(data);
}
});
}
else {
response.writeHead(404);
response.end();
}
});
server.listen(args.port, function() {
console.log((new Date()) + ' Server is listening on port ' + args.port);
});
var wsServer = new WebSocketServer({
httpServer: server
});
var router = new WebSocketRouter();
router.attachServer(wsServer);
var mirrorConnections = [];
var mirrorHistory = [];
function sendCallback(err) {
if (err) { console.error('send() error: ' + err); }
}
router.mount('*', 'lws-mirror-protocol', function(request) {
var cookies = [
{
name: 'TestCookie',
value: 'CookieValue' + Math.floor(Math.random()*1000),
path: '/',
secure: false,
maxage: 5000,
httponly: true
}
];
// Should do origin verification here. You have to pass the accepted
// origin into the accept method of the request.
var connection = request.accept(request.origin, cookies);
console.log((new Date()) + ' lws-mirror-protocol connection accepted from ' + connection.remoteAddress +
' - Protocol Version ' + connection.webSocketVersion);
if (mirrorHistory.length > 0) {
var historyString = mirrorHistory.join('');
console.log((new Date()) + ' sending mirror protocol history to client; ' + connection.remoteAddress + ' : ' + Buffer.byteLength(historyString) + ' bytes');
connection.send(historyString, sendCallback);
}
mirrorConnections.push(connection);
connection.on('message', function(message) {
// We only care about text messages
if (message.type === 'utf8') {
// Clear canvas command received
if (message.utf8Data === 'clear;') {
mirrorHistory = [];
}
else {
// Record all other commands in the history
mirrorHistory.push(message.utf8Data);
}
// Re-broadcast the command to all connected clients
mirrorConnections.forEach(function (outputConnection) {
outputConnection.send(message.utf8Data, sendCallback);
});
}
});
connection.on('close', function(closeReason, description) {
var index = mirrorConnections.indexOf(connection);
if (index !== -1) {
console.log((new Date()) + ' lws-mirror-protocol peer ' + connection.remoteAddress + ' disconnected, code: ' + closeReason + '.');
mirrorConnections.splice(index, 1);
}
});
connection.on('error', function(error) {
console.log('Connection error for peer ' + connection.remoteAddress + ': ' + error);
});
});
router.mount('*', 'dumb-increment-protocol', function(request) {
// Should do origin verification here. You have to pass the accepted
// origin into the accept method of the request.
var connection = request.accept(request.origin);
console.log((new Date()) + ' dumb-increment-protocol connection accepted from ' + connection.remoteAddress +
' - Protocol Version ' + connection.webSocketVersion);
var number = 0;
connection.timerInterval = setInterval(function() {
connection.send((number++).toString(10), sendCallback);
}, 50);
connection.on('close', function() {
clearInterval(connection.timerInterval);
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
if (message.utf8Data === 'reset\n') {
console.log((new Date()) + ' increment reset received');
number = 0;
}
}
});
connection.on('close', function(closeReason, description) {
console.log((new Date()) + ' dumb-increment-protocol peer ' + connection.remoteAddress + ' disconnected, code: ' + closeReason + '.');
});
});
console.log('WebSocket-Node: Test Server implementing Andy Green\'s');
console.log('libwebsockets-test-server protocols.');
console.log('Point your WebSocket Protocol Version 8 complant browser to http://localhost:' + args.port + '/');