Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 1 | var WebSocketServer = require('ws').Server; |
| 2 | var dgram = require('dgram'); |
| 3 | |
| 4 | var ccndhost = 'localhost'; |
| 5 | |
| 6 | var wss = new WebSocketServer({port:9696, host:ccndhost}); |
| 7 | |
| 8 | var MaxNumOfClients = 2; |
| 9 | |
| 10 | wss.on('connection', function(ws) { |
| 11 | console.log('wss.onconnection: WebSocket client connection received.'); |
| 12 | console.log('wss.onconnection: Number of clients now is ' + wss.clients.length); |
| 13 | |
| 14 | if (wss.clients.length > MaxNumOfClients) { |
| 15 | console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.'); |
| 16 | ws.terminate(); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | var udp = dgram.createSocket("udp4"); |
| 21 | |
| 22 | /* |
| 23 | * According to the email discussion with Michael, when we use |
| 24 | * UDP to connect to ccnd, we MUST first send a 'heartbeat' |
| 25 | * UDP packet with 1-byte payload (content of this byte can |
| 26 | * be random). The purpose of this packet is to let ccnd |
| 27 | * mark the incoming FACE as 'friendly' (with CCN_FACE_GG |
| 28 | * flag set). We also need to periodically send this 'heartbeat' |
| 29 | * packet every few seconds to keep ccnd from recycling the UDP |
| 30 | * face. Michael recomended 8 seconds interval. |
| 31 | * --Wentao |
| 32 | */ |
| 33 | // Send 'heartbeat' packet now |
| 34 | var heartbeat = new Buffer(1); |
| 35 | heartbeat[0] = 0x21; |
| 36 | udp.send(heartbeat, 0, 1, 9695, ccndhost, null); |
| 37 | |
| 38 | // Schedule a timer to send 'heartbeat' periodically |
| 39 | var timerID = setInterval(function() { |
| 40 | if (udp == null || udp == undefined) |
| 41 | return; |
| 42 | |
| 43 | var hb = new Buffer(1); |
| 44 | hb[0] = 0x21; |
| 45 | udp.send(hb, 0, 1, 9695, ccndhost, null); |
| 46 | //console.log('UDP heartbeat fired at ccnd.'); |
| 47 | }, |
| 48 | 8000 // 8000 ms delay |
| 49 | ); |
| 50 | |
| 51 | ws.on('message', function(message) { |
| 52 | if (typeof message == 'string') |
| 53 | console.log("ws.onmessage: Message from clinet: " + message); |
| 54 | else if (typeof message == 'object') { |
| 55 | // From JS array to Buffer |
| 56 | var buffer = new Buffer(message); |
| 57 | |
| 58 | var logMsg = 'ws.onmessage: Byte array from client: '; |
| 59 | for (var i = 0; i < buffer.length; i++) |
| 60 | logMsg += String.fromCharCode(buffer[i]); |
| 61 | console.log(logMsg); |
| 62 | |
| 63 | udp.send(buffer, 0, buffer.length, 9695, ccndhost, null); |
| 64 | console.log('ws.onmessage: udp.send() returned.'); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | ws.on('close', function() { |
| 69 | console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ccnd and stop "heartbeat" timer.'); |
| 70 | clearInterval(timerID); |
| 71 | udp.close(); |
| 72 | udp = null; |
| 73 | }); |
| 74 | |
| 75 | udp.on('message', function(msg, rinfo) { |
| 76 | if (typeof msg == 'object') { |
| 77 | // From Buffer to ArrayBuffer |
| 78 | var bytesView = new Uint8Array(msg); |
| 79 | |
| 80 | console.log('udp.onmessage: Byte array from server: '); |
| 81 | console.log('udp.onmessage: bytesView.length ' + bytesView.length); |
| 82 | var logMsg = ""; |
| 83 | for (var i = 0; i < bytesView.length; i++) |
| 84 | logMsg += String.fromCharCode(bytesView[i]); |
| 85 | console.log(logMsg); |
| 86 | |
| 87 | ws.send(bytesView.buffer, {binary: true, mask: false}); |
| 88 | console.log('udp.onmessage: ws.send() returned.'); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | // Actually the socket close by ccnd will not cause the 'close' event to raise. |
| 93 | // So this event handle is only called when the client browser shuts down the WS |
| 94 | // connection, causing ws 'close' event to raise. In that event handle, we explicitly |
| 95 | // call udp.close(). So in this function we can do nothing. Anyway, here we clear the |
| 96 | // timer and terminate ws for a second time since that will not throw exception. 'ws' |
| 97 | // will check the 'readyState' before closing, therefore avoids 'close' event loop. |
| 98 | // --Wentao |
| 99 | udp.on('close', function() { |
| 100 | console.log('udp.onclose: UDP connection to ccnd terminated. Shut down WS connection to client and stop "heartbeat" timer.'); |
| 101 | clearInterval(timerID); |
| 102 | ws.terminate(); |
| 103 | }); |
| 104 | |
| 105 | udp.on('error', function() { |
| 106 | console.log('udp.onerror: Error on UDP connection to ccnd. Shut down WS connection to client and stop "heartbeat" timer.'); |
| 107 | clearInterval(timerID); |
| 108 | ws.terminate(); |
| 109 | }); |
| 110 | }); |