Newer
Older
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
//
// echoserver.js
// WebSocket echo server
//
// Neil Gershenfeld
// (c) Massachusetts Institute of Technology 2016
//
// This work may be reproduced, modified, distributed, performed, and
// displayed for any purpose, but must acknowledge the mods
// project. Copyright is retained and must be preserved. The work is
// provided as is; no warranty is provided, and users accept all
// liability.
//
// check command line
//
if (process.argv.length < 4) {
console.log("command line: node echoserver.js client_address server_port")
process.exit(-1)
}
//
// start server
//
var client_address = process.argv[2]
var server_port = process.argv[3]
console.log("listening for connection from client address "+client_address+" on server port "+server_port)
var fs = require("fs")
var WebSocketServer = require('ws').Server
wss = new WebSocketServer({port:server_port})
wss.on('connection',function(ws) {
if (ws._socket.remoteAddress != client_address) {
console.log("connection rejected from "+ws._socket.remoteAddress)
wss.close()
process.exit(-1)
}
else {
console.log("connection accepted from "+ws._socket.remoteAddress)
}
ws.on("message",function(msg) {
//console.log(" "+msg)
ws.send(msg)
})
ws.on("close",function() {
console.log("connection closed")
})
})