Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 2 | * @author: Wentao Shang |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | var WebSocketTransport = function WebSocketTransport() { |
Jeff Thompson | d40f08a | 2013-02-24 20:51:17 -0800 | [diff] [blame] | 7 | if (!WebSocket) |
| 8 | throw new Error("WebSocket support is not available on this platform."); |
| 9 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 10 | this.ws = null; |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 11 | this.connectedHost = null; // Read by NDN. |
| 12 | this.connectedPort = null; // Read by NDN. |
Jeff Thompson | bb9802d | 2013-01-20 23:54:18 -0800 | [diff] [blame] | 13 | this.elementReader = null; |
Jeff Thompson | c881b55 | 2012-12-14 01:29:12 -0800 | [diff] [blame] | 14 | this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort |
| 15 | (["A.ws.ndn.ucla.edu", "B.ws.ndn.ucla.edu", "C.ws.ndn.ucla.edu", "D.ws.ndn.ucla.edu", |
| 16 | "E.ws.ndn.ucla.edu"], |
| 17 | 9696); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 18 | }; |
| 19 | |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 20 | /* |
| 21 | * Connect to the host and port in ndn. This replaces a previous connection and sets connectedHost |
| 22 | * and connectedPort. Once connected, call onopenCallback(). |
| 23 | * Listen on the port to read an entire binary XML encoded element and call |
| 24 | * ndn.onReceivedElement(element). |
| 25 | */ |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 26 | WebSocketTransport.prototype.connect = function(ndn, onopenCallback) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 27 | if (this.ws != null) |
| 28 | delete this.ws; |
| 29 | |
| 30 | this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 31 | if (LOG > 0) console.log('ws connection created.'); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 32 | this.connectedHost = ndn.host; |
| 33 | this.connectedPort = ndn.port; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 34 | |
| 35 | this.ws.binaryType = "arraybuffer"; |
| 36 | |
Jeff Thompson | bb9802d | 2013-01-20 23:54:18 -0800 | [diff] [blame] | 37 | this.elementReader = new BinaryXmlElementReader(ndn); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 38 | var self = this; |
| 39 | this.ws.onmessage = function(ev) { |
| 40 | var result = ev.data; |
| 41 | //console.log('RecvHandle called.'); |
| 42 | |
| 43 | if(result == null || result == undefined || result == "" ) { |
| 44 | console.log('INVALID ANSWER'); |
| 45 | } else if (result instanceof ArrayBuffer) { |
| 46 | var bytearray = new Uint8Array(result); |
| 47 | |
Jeff Thompson | 13c2e7b | 2012-12-01 17:33:30 -0800 | [diff] [blame] | 48 | if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray)); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 49 | |
| 50 | try { |
Jeff Thompson | bb9802d | 2013-01-20 23:54:18 -0800 | [diff] [blame] | 51 | // Find the end of the binary XML element and call ndn.onReceivedElement. |
| 52 | self.elementReader.onReceivedData(bytearray); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 53 | } catch (ex) { |
| 54 | console.log("NDN.ws.onmessage exception: " + ex); |
| 55 | return; |
| 56 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | this.ws.onopen = function(ev) { |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 61 | if (LOG > 3) console.log(ev); |
| 62 | if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.'); |
| 63 | if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 64 | // NDN.registerPrefix will fetch the ccndid when needed. |
| 65 | |
| 66 | onopenCallback(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | this.ws.onerror = function(ev) { |
| 70 | console.log('ws.onerror: ReadyState: ' + this.readyState); |
| 71 | console.log(ev); |
| 72 | console.log('ws.onerror: WebSocket error: ' + ev.data); |
| 73 | } |
| 74 | |
| 75 | this.ws.onclose = function(ev) { |
| 76 | console.log('ws.onclose: WebSocket connection closed.'); |
| 77 | self.ws = null; |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 78 | |
| 79 | // Close NDN when WebSocket is closed |
| 80 | ndn.readyStatus = NDN.CLOSED; |
| 81 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 82 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 83 | } |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 84 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 85 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 86 | /* |
| 87 | * Send the Uint8Array data. |
| 88 | */ |
| 89 | WebSocketTransport.prototype.send = function(data) { |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 90 | if (this.ws != null) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 91 | // If we directly use data.buffer to feed ws.send(), |
| 92 | // WebSocket may end up sending a packet with 10000 bytes of data. |
| 93 | // That is, WebSocket will flush the entire buffer |
| 94 | // regardless of the offset of the Uint8Array. So we have to create |
| 95 | // a new Uint8Array buffer with just the right size and copy the |
| 96 | // content from binaryInterest to the new buffer. |
| 97 | // ---Wentao |
| 98 | var bytearray = new Uint8Array(data.length); |
| 99 | bytearray.set(data); |
| 100 | this.ws.send(bytearray.buffer); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 101 | if (LOG > 3) console.log('ws.send() returned.'); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 102 | } |
| 103 | else |
| 104 | console.log('WebSocket connection is not established.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 105 | } |