blob: bc2a3e7e046dd18126cf5c347465e0d7396ccdc4 [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
6var WebSocketTransport = function WebSocketTransport() {
Jeff Thompsond40f08a2013-02-24 20:51:17 -08007 if (!WebSocket)
8 throw new Error("WebSocket support is not available on this platform.");
9
Jeff Thompson5b265a72012-11-12 01:13:08 -080010 this.ws = null;
Jeff Thompsonf668acb2013-01-26 20:29:46 -080011 this.connectedHost = null; // Read by NDN.
12 this.connectedPort = null; // Read by NDN.
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080013 this.elementReader = null;
Jeff Thompsonc881b552012-12-14 01:29:12 -080014 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 Thompson5b265a72012-11-12 01:13:08 -080018};
19
Jeff Thompsonf668acb2013-01-26 20:29:46 -080020/*
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 Thompsond771b122013-01-26 19:04:41 -080026WebSocketTransport.prototype.connect = function(ndn, onopenCallback) {
Jeff Thompson5b265a72012-11-12 01:13:08 -080027 if (this.ws != null)
28 delete this.ws;
29
30 this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port);
Jeff Thompson3c263812012-12-01 17:20:28 -080031 if (LOG > 0) console.log('ws connection created.');
Jeff Thompsond771b122013-01-26 19:04:41 -080032 this.connectedHost = ndn.host;
33 this.connectedPort = ndn.port;
Jeff Thompson5b265a72012-11-12 01:13:08 -080034
35 this.ws.binaryType = "arraybuffer";
36
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080037 this.elementReader = new BinaryXmlElementReader(ndn);
Jeff Thompson5b265a72012-11-12 01:13:08 -080038 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 Thompson13c2e7b2012-12-01 17:33:30 -080048 if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray));
Jeff Thompson5b265a72012-11-12 01:13:08 -080049
50 try {
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080051 // Find the end of the binary XML element and call ndn.onReceivedElement.
52 self.elementReader.onReceivedData(bytearray);
Jeff Thompson5b265a72012-11-12 01:13:08 -080053 } catch (ex) {
54 console.log("NDN.ws.onmessage exception: " + ex);
55 return;
56 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080057 }
58 }
59
60 this.ws.onopen = function(ev) {
Jeff Thompson3c263812012-12-01 17:20:28 -080061 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 Thompsond771b122013-01-26 19:04:41 -080064 // NDN.registerPrefix will fetch the ccndid when needed.
65
66 onopenCallback();
Jeff Thompson5b265a72012-11-12 01:13:08 -080067 }
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 Shang0e291c82012-12-02 23:36:29 -080078
79 // Close NDN when WebSocket is closed
80 ndn.readyStatus = NDN.CLOSED;
81 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -080082 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -080083 }
Jeff Thompsonbe85be62012-12-13 22:32:01 -080084};
Jeff Thompson5b265a72012-11-12 01:13:08 -080085
Jeff Thompson75771cb2013-01-20 23:27:38 -080086/*
87 * Send the Uint8Array data.
88 */
89WebSocketTransport.prototype.send = function(data) {
Wentao Shangc0311e52012-12-03 10:38:23 -080090 if (this.ws != null) {
Jeff Thompson75771cb2013-01-20 23:27:38 -080091 // 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 Shangc0311e52012-12-03 10:38:23 -0800101 if (LOG > 3) console.log('ws.send() returned.');
Wentao Shangc0311e52012-12-03 10:38:23 -0800102 }
103 else
104 console.log('WebSocket connection is not established.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800105}