blob: 6365bcb803e323285289015b20c07b50eec7f4f6 [file] [log] [blame]
Wentao Shangc0311e52012-12-03 10:38:23 -08001/**
Jeff Thompson5b265a72012-11-12 01:13:08 -08002 * @author: Wentao Shang
3 * See COPYING for copyright and distribution information.
Jeff Thompson5b265a72012-11-12 01:13:08 -08004 */
5
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07006/**
7 * @constructor
8 */
Jeff Thompson5b265a72012-11-12 01:13:08 -08009var WebSocketTransport = function WebSocketTransport() {
Jeff Thompsond40f08a2013-02-24 20:51:17 -080010 if (!WebSocket)
11 throw new Error("WebSocket support is not available on this platform.");
12
Jeff Thompson5b265a72012-11-12 01:13:08 -080013 this.ws = null;
Jeff Thompsonf668acb2013-01-26 20:29:46 -080014 this.connectedHost = null; // Read by NDN.
15 this.connectedPort = null; // Read by NDN.
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080016 this.elementReader = null;
Jeff Thompsonc881b552012-12-14 01:29:12 -080017 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
18 (["A.ws.ndn.ucla.edu", "B.ws.ndn.ucla.edu", "C.ws.ndn.ucla.edu", "D.ws.ndn.ucla.edu",
19 "E.ws.ndn.ucla.edu"],
20 9696);
Jeff Thompson5b265a72012-11-12 01:13:08 -080021};
22
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070023/**
Jeff Thompsonf668acb2013-01-26 20:29:46 -080024 * Connect to the host and port in ndn. This replaces a previous connection and sets connectedHost
25 * and connectedPort. Once connected, call onopenCallback().
26 * Listen on the port to read an entire binary XML encoded element and call
27 * ndn.onReceivedElement(element).
28 */
Jeff Thompsond771b122013-01-26 19:04:41 -080029WebSocketTransport.prototype.connect = function(ndn, onopenCallback) {
Jeff Thompson5b265a72012-11-12 01:13:08 -080030 if (this.ws != null)
31 delete this.ws;
32
33 this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port);
Jeff Thompson3c263812012-12-01 17:20:28 -080034 if (LOG > 0) console.log('ws connection created.');
Jeff Thompsond771b122013-01-26 19:04:41 -080035 this.connectedHost = ndn.host;
36 this.connectedPort = ndn.port;
Jeff Thompson5b265a72012-11-12 01:13:08 -080037
38 this.ws.binaryType = "arraybuffer";
39
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080040 this.elementReader = new BinaryXmlElementReader(ndn);
Jeff Thompson5b265a72012-11-12 01:13:08 -080041 var self = this;
42 this.ws.onmessage = function(ev) {
43 var result = ev.data;
44 //console.log('RecvHandle called.');
45
46 if(result == null || result == undefined || result == "" ) {
47 console.log('INVALID ANSWER');
48 } else if (result instanceof ArrayBuffer) {
49 var bytearray = new Uint8Array(result);
50
Jeff Thompson13c2e7b2012-12-01 17:33:30 -080051 if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray));
Jeff Thompson5b265a72012-11-12 01:13:08 -080052
53 try {
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080054 // Find the end of the binary XML element and call ndn.onReceivedElement.
55 self.elementReader.onReceivedData(bytearray);
Jeff Thompson5b265a72012-11-12 01:13:08 -080056 } catch (ex) {
57 console.log("NDN.ws.onmessage exception: " + ex);
58 return;
59 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080060 }
61 }
62
63 this.ws.onopen = function(ev) {
Jeff Thompson3c263812012-12-01 17:20:28 -080064 if (LOG > 3) console.log(ev);
65 if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.');
66 if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState);
Jeff Thompsond771b122013-01-26 19:04:41 -080067 // NDN.registerPrefix will fetch the ccndid when needed.
68
69 onopenCallback();
Jeff Thompson5b265a72012-11-12 01:13:08 -080070 }
71
72 this.ws.onerror = function(ev) {
73 console.log('ws.onerror: ReadyState: ' + this.readyState);
74 console.log(ev);
75 console.log('ws.onerror: WebSocket error: ' + ev.data);
76 }
77
78 this.ws.onclose = function(ev) {
79 console.log('ws.onclose: WebSocket connection closed.');
80 self.ws = null;
Wentao Shang0e291c82012-12-02 23:36:29 -080081
82 // Close NDN when WebSocket is closed
83 ndn.readyStatus = NDN.CLOSED;
84 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -080085 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -080086 }
Jeff Thompsonbe85be62012-12-13 22:32:01 -080087};
Jeff Thompson5b265a72012-11-12 01:13:08 -080088
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070089/**
Jeff Thompson75771cb2013-01-20 23:27:38 -080090 * Send the Uint8Array data.
91 */
92WebSocketTransport.prototype.send = function(data) {
Wentao Shangc0311e52012-12-03 10:38:23 -080093 if (this.ws != null) {
Jeff Thompson75771cb2013-01-20 23:27:38 -080094 // If we directly use data.buffer to feed ws.send(),
95 // WebSocket may end up sending a packet with 10000 bytes of data.
96 // That is, WebSocket will flush the entire buffer
97 // regardless of the offset of the Uint8Array. So we have to create
98 // a new Uint8Array buffer with just the right size and copy the
99 // content from binaryInterest to the new buffer.
100 // ---Wentao
101 var bytearray = new Uint8Array(data.length);
102 bytearray.set(data);
103 this.ws.send(bytearray.buffer);
Wentao Shangc0311e52012-12-03 10:38:23 -0800104 if (LOG > 3) console.log('ws.send() returned.');
Wentao Shangc0311e52012-12-03 10:38:23 -0800105 }
106 else
107 console.log('WebSocket connection is not established.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800108}