Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env node |
| 2 | // === Mock Web Socket Server - for testing the topology view |
| 3 | // |
| 4 | |
| 5 | var readline = require('readline'); |
| 6 | var WebSocketServer = require('websocket').server; |
| 7 | var http = require('http'); |
| 8 | var port = 8123; |
| 9 | |
| 10 | |
| 11 | var server = http.createServer(function(request, response) { |
| 12 | console.log((new Date()) + ' Received request for ' + request.url); |
| 13 | response.writeHead(404); |
| 14 | response.end(); |
| 15 | }); |
| 16 | |
| 17 | server.listen(port, function() { |
| 18 | console.log((new Date()) + ' Server is listening on port ' + port); |
| 19 | }); |
| 20 | |
| 21 | server.on('listening', function () { |
| 22 | console.log('ok, server is running'); |
| 23 | }); |
| 24 | |
| 25 | var wsServer = new WebSocketServer({ |
| 26 | httpServer: server, |
| 27 | // You should not use autoAcceptConnections for production |
| 28 | // applications, as it defeats all standard cross-origin protection |
| 29 | // facilities built into the protocol and the browser. You should |
| 30 | // *always* verify the connection's origin and decide whether or not |
| 31 | // to accept it. |
| 32 | autoAcceptConnections: false |
| 33 | }); |
| 34 | |
| 35 | function originIsAllowed(origin) { |
| 36 | // put logic here to detect whether the specified origin is allowed. |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | var connection; |
| 41 | |
| 42 | wsServer.on('request', function(request) { |
| 43 | console.log(); // newline after prompt |
| 44 | console.log("Origin: ", request.origin); |
| 45 | |
| 46 | if (!originIsAllowed(request.origin)) { |
| 47 | // Make sure we only accept requests from an allowed origin |
| 48 | request.reject(); |
| 49 | console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | connection = request.accept(null, request.origin); |
| 54 | |
| 55 | |
| 56 | console.log((new Date()) + ' Connection accepted.'); |
| 57 | rl.prompt(); |
| 58 | |
| 59 | |
| 60 | connection.on('message', function(message) { |
| 61 | if (message.type === 'utf8') { |
| 62 | console.log(); // newline after prompt |
| 63 | console.log('Received Message: ' + message.utf8Data); |
| 64 | //connection.sendUTF(message.utf8Data); |
| 65 | rl.prompt(); |
| 66 | } |
| 67 | else if (message.type === 'binary') { |
| 68 | console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); |
| 69 | //connection.sendBytes(message.binaryData); |
| 70 | } |
| 71 | }); |
| 72 | connection.on('close', function(reasonCode, description) { |
| 73 | console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | |
| 78 | var rl = readline.createInterface(process.stdin, process.stdout); |
| 79 | rl.setPrompt('ws> '); |
| 80 | |
| 81 | setTimeout(doCli, 10); |
| 82 | |
| 83 | function doCli() { |
| 84 | rl.prompt(); |
| 85 | rl.on('line', function (line) { |
| 86 | var words = line.trim().split(' '), |
| 87 | cmd = words.shift(), |
| 88 | str = words.join(' '); |
| 89 | |
| 90 | switch(cmd) { |
| 91 | case 'hello': |
| 92 | console.log('hello back: ' + str); |
| 93 | break; |
| 94 | |
| 95 | case 'quit': |
| 96 | process.exit(0); |
| 97 | break; |
| 98 | |
| 99 | case 'm': |
| 100 | console.log('sending message: ' + str); |
| 101 | connection.sendUTF(str); |
| 102 | break; |
| 103 | |
| 104 | default: |
| 105 | console.log('Say what?! [' + line.trim() + ']'); |
| 106 | break; |
| 107 | } |
| 108 | rl.prompt(); |
| 109 | }).on('close', function () { |
| 110 | console.log('quitting...'); |
| 111 | process.exit(0); |
| 112 | }); |
| 113 | |
| 114 | } |