]>
git.proxmox.com Git - mirror_novnc.git/blob - include/websock.js
20d51d6a2d0b5f929691252b2114bd38ea833ce3
2 * Websock: high-performance binary WebSockets
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under LGPL-3 (see LICENSE.txt)
6 * Websock is similar to the standard WebSocket object but Websock
7 * enables communication with raw TCP sockets (i.e. the binary stream)
8 * via websockify. This is accomplished by base64 encoding the data
9 * stream between Websock and websockify.
11 * Websock has built-in receive queue buffering; the message event
12 * does not contain actual data but is simply a notification that
13 * there is new data available. Several rQ* methods are available to
14 * read binary data off of the receive queue.
17 /*jslint browser: true, bitwise: false, plusplus: false */
18 /*global Util, Base64 */
21 // Load Flash WebSocket emulator if needed
23 // To force WebSocket emulator even when native WebSocket available
24 //window.WEB_SOCKET_FORCE_FLASH = true;
25 // To enable WebSocket emulator debug:
26 //window.WEB_SOCKET_DEBUG=1;
28 if (window
.WebSocket
&& !window
.WEB_SOCKET_FORCE_FLASH
) {
29 Websock_native
= true;
30 } else if (window
.MozWebSocket
&& !window
.WEB_SOCKET_FORCE_FLASH
) {
31 Websock_native
= true;
32 window
.WebSocket
= window
.MozWebSocket
;
34 /* no builtin WebSocket so load web_socket.js */
36 Websock_native
= false;
38 function get_INCLUDE_URI() {
39 return (typeof INCLUDE_URI
!== "undefined") ?
40 INCLUDE_URI
: "include/";
43 var start
= "<script src='" + get_INCLUDE_URI(),
44 end
= "'><\/script>", extra
= "";
46 window
.WEB_SOCKET_SWF_LOCATION
= get_INCLUDE_URI() +
47 "web-socket-js/WebSocketMain.swf";
48 if (Util
.Engine
.trident
) {
49 Util
.Debug("Forcing uncached load of WebSocketMain.swf");
50 window
.WEB_SOCKET_SWF_LOCATION
+= "?" + Math
.random();
52 extra
+= start
+ "web-socket-js/swfobject.js" + end
;
53 extra
+= start
+ "web-socket-js/web_socket.js" + end
;
54 document
.write(extra
);
62 var api
= {}, // Public API
63 websocket
= null, // WebSocket object
64 rQ
= [], // Receive queue
65 rQi
= 0, // Receive queue index
66 rQmax
= 10000, // Max receive queue size before compacting
67 sQ
= [], // Send queue
70 'message' : function() {},
71 'open' : function() {},
72 'close' : function() {},
73 'error' : function() {}
80 // Queue public functions
93 function set_rQi(val
) {
98 return rQ
.length
- rQi
;
104 function rQshift8() {
107 function rQunshift8(num
) {
116 function rQshift16() {
117 return (rQ
[rQi
++] << 8) +
120 function rQshift32() {
121 return (rQ
[rQi
++] << 24) +
126 function rQshiftStr(len
) {
127 if (typeof(len
) === 'undefined') { len
= rQlen(); }
128 var arr
= rQ
.slice(rQi
, rQi
+ len
);
130 return arr
.map(function (num
) {
131 return String
.fromCharCode(num
); } ).join('');
134 function rQshiftBytes(len
) {
135 if (typeof(len
) === 'undefined') { len
= rQlen(); }
137 return rQ
.slice(rQi
-len
, rQi
);
140 function rQslice(start
, end
) {
142 return rQ
.slice(rQi
+ start
, rQi
+ end
);
144 return rQ
.slice(rQi
+ start
);
148 // Check to see if we must wait for 'num' bytes (default to FBU.bytes)
149 // to be available in the receive queue. Return true if we need to
150 // wait (and possibly print a debug message), otherwise false.
151 function rQwait(msg
, num
, goback
) {
152 var rQlen
= rQ
.length
- rQi
; // Skip rQlen() function call
156 throw("rQwait cannot backup " + goback
+ " bytes");
160 //Util.Debug(" waiting for " + (num-rQlen) +
161 // " " + msg + " byte(s)");
162 return true; // true means need more data
168 // Private utility routines
171 function encode_message() {
173 return Base64
.encode(sQ
);
176 function decode_message(data
) {
177 //Util.Debug(">> decode_message: " + data);
179 rQ
= rQ
.concat(Base64
.decode(data
, 0));
180 //Util.Debug(">> decode_message, rQ: " + rQ);
185 // Public Send functions
189 if (websocket
.bufferedAmount
!== 0) {
190 Util
.Debug("bufferedAmount: " + websocket
.bufferedAmount
);
192 if (websocket
.bufferedAmount
< api
.maxBufferedAmount
) {
193 //Util.Debug("arr: " + arr);
194 //Util.Debug("sQ: " + sQ);
196 websocket
.send(encode_message(sQ
));
201 Util
.Info("Delaying send, bufferedAmount: " +
202 websocket
.bufferedAmount
);
207 // overridable for testing
209 //Util.Debug(">> send_array: " + arr);
214 function send_string(str
) {
215 //Util.Debug(">> send_string: " + str);
216 api
.send(str
.split('').map(
217 function (chr
) { return chr
.charCodeAt(0); } ) );
221 // Other public functions
223 function recv_message(e
) {
224 //Util.Debug(">> recv_message: " + e.data.length);
227 decode_message(e
.data
);
229 eventHandlers
.message();
230 // Compact the receive queue
231 if (rQ
.length
> rQmax
) {
232 //Util.Debug("Compacting receive queue");
237 Util
.Debug("Ignoring empty message");
240 if (typeof exc
.stack
!== 'undefined') {
241 Util
.Warn("recv_message, caught exception: " + exc
.stack
);
242 } else if (typeof exc
.description
!== 'undefined') {
243 Util
.Warn("recv_message, caught exception: " + exc
.description
);
245 Util
.Warn("recv_message, caught exception:" + exc
);
247 if (typeof exc
.name
!== 'undefined') {
248 eventHandlers
.error(exc
.name
+ ": " + exc
.message
);
250 eventHandlers
.error(exc
);
253 //Util.Debug("<< recv_message");
257 // Set event handlers
258 function on(evt
, handler
) {
259 eventHandlers
[evt
] = handler
;
275 websocket
= new WebSocket(uri
, 'base64');
276 // TODO: future native binary support
277 //websocket = new WebSocket(uri, ['binary', 'base64']);
280 websocket
.onmessage
= recv_message
;
281 websocket
.onopen = function() {
282 Util
.Debug(">> WebSock.onopen");
283 if (websocket
.protocol
) {
284 Util
.Info("Server chose sub-protocol: " + websocket
.protocol
);
286 Util
.Error("Server select no sub-protocol!: " + websocket
.protocol
);
288 eventHandlers
.open();
289 Util
.Debug("<< WebSock.onopen");
291 websocket
.onclose = function(e
) {
292 Util
.Debug(">> WebSock.onclose");
293 eventHandlers
.close(e
);
294 Util
.Debug("<< WebSock.onclose");
296 websocket
.onerror = function(e
) {
297 Util
.Debug(">> WebSock.onerror: " + e
);
298 eventHandlers
.error(e
);
299 Util
.Debug("<< WebSock.onerror");
305 if ((websocket
.readyState
=== WebSocket
.OPEN
) ||
306 (websocket
.readyState
=== WebSocket
.CONNECTING
)) {
307 Util
.Info("Closing WebSocket connection");
310 websocket
.onmessage = function (e
) { return; };
314 // Override internal functions for testing
315 // Takes a send function, returns reference to recv function
316 function testMode(override_send
) {
318 api
.send
= override_send
;
319 api
.close = function () {};
323 function constructor() {
324 // Configuration settings
325 api
.maxBufferedAmount
= 200;
327 // Direct access to send and receive queues
330 api
.get_rQi
= get_rQi
;
331 api
.set_rQi
= set_rQi
;
333 // Routines to read from the receive queue
335 api
.rQpeek8
= rQpeek8
;
336 api
.rQshift8
= rQshift8
;
337 api
.rQunshift8
= rQunshift8
;
338 api
.rQshift16
= rQshift16
;
339 api
.rQshift32
= rQshift32
;
340 api
.rQshiftStr
= rQshiftStr
;
341 api
.rQshiftBytes
= rQshiftBytes
;
342 api
.rQslice
= rQslice
;
347 api
.send_string
= send_string
;
353 api
.testMode
= testMode
;
358 return constructor();