]>
git.proxmox.com Git - mirror_novnc.git/blob - core/websock.js
2 * Websock: high-performance binary WebSockets
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * Websock is similar to the standard WebSocket object but with extra
9 * Websock has built-in receive queue buffering; the message event
10 * does not contain actual data but is simply a notification that
11 * there is new data available. Several rQ* methods are available to
12 * read binary data off of the receive queue.
15 import * as Log
from './util/logging.js';
17 export default function Websock() {
20 this._websocket
= null; // WebSocket object
22 this._rQi
= 0; // Receive queue index
23 this._rQlen
= 0; // Next write position in the receive queue
24 this._rQbufferSize
= 1024 * 1024 * 4; // Receive queue buffer size (4 MiB)
25 this._rQmax
= this._rQbufferSize
/ 8;
26 // called in init: this._rQ = new Uint8Array(this._rQbufferSize);
27 this._rQ
= null; // Receive queue
29 this._sQbufferSize
= 1024 * 10; // 10 KiB
30 // called in init: this._sQ = new Uint8Array(this._sQbufferSize);
32 this._sQ
= null; // Send queue
34 this._eventHandlers
= {
35 'message': function () {},
36 'open': function () {},
37 'close': function () {},
38 'error': function () {}
42 // this has performance issues in some versions Chromium, and
43 // doesn't gain a tremendous amount of performance increase in Firefox
44 // at the moment. It may be valuable to turn it on in the future.
45 var ENABLE_COPYWITHIN
= false;
47 var MAX_RQ_GROW_SIZE
= 40 * 1024 * 1024; // 40 MiB
49 var typedArrayToString
= (function () {
50 // This is only for PhantomJS, which doesn't like apply-ing
53 var arr
= new Uint8Array([1, 2, 3]);
54 String
.fromCharCode
.apply(null, arr
);
55 return function (a
) { return String
.fromCharCode
.apply(null, a
); };
58 return String
.fromCharCode
.apply(
59 null, Array
.prototype.slice
.call(a
));
65 // Getters and Setters
74 get_rQi: function () {
78 set_rQi: function (val
) {
84 return this._rQlen
- this._rQi
;
87 rQpeek8: function () {
88 return this._rQ
[this._rQi
];
91 rQshift8: function () {
92 return this._rQ
[this._rQi
++];
95 rQskip8: function () {
99 rQskipBytes: function (num
) {
103 // TODO(directxman12): test performance with these vs a DataView
104 rQshift16: function () {
105 return (this._rQ
[this._rQi
++] << 8) +
106 this._rQ
[this._rQi
++];
109 rQshift32: function () {
110 return (this._rQ
[this._rQi
++] << 24) +
111 (this._rQ
[this._rQi
++] << 16) +
112 (this._rQ
[this._rQi
++] << 8) +
113 this._rQ
[this._rQi
++];
116 rQshiftStr: function (len
) {
117 if (typeof(len
) === 'undefined') { len
= this.rQlen(); }
118 var arr
= new Uint8Array(this._rQ
.buffer
, this._rQi
, len
);
120 return typedArrayToString(arr
);
123 rQshiftBytes: function (len
) {
124 if (typeof(len
) === 'undefined') { len
= this.rQlen(); }
126 return new Uint8Array(this._rQ
.buffer
, this._rQi
- len
, len
);
129 rQshiftTo: function (target
, len
) {
130 if (len
=== undefined) { len
= this.rQlen(); }
131 // TODO: make this just use set with views when using a ArrayBuffer to store the rQ
132 target
.set(new Uint8Array(this._rQ
.buffer
, this._rQi
, len
));
136 rQwhole: function () {
137 return new Uint8Array(this._rQ
.buffer
, 0, this._rQlen
);
140 rQslice: function (start
, end
) {
142 return new Uint8Array(this._rQ
.buffer
, this._rQi
+ start
, end
- start
);
144 return new Uint8Array(this._rQ
.buffer
, this._rQi
+ start
, this._rQlen
- this._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 rQwait: function (msg
, num
, goback
) {
152 var rQlen
= this._rQlen
- this._rQi
; // Skip rQlen() function call
155 if (this._rQi
< goback
) {
156 throw new Error("rQwait cannot backup " + goback
+ " bytes");
160 return true; // true means need more data
168 if (this._sQlen
> 0 && this._websocket
.readyState
=== WebSocket
.OPEN
) {
169 this._websocket
.send(this._encode_message());
174 send: function (arr
) {
175 this._sQ
.set(arr
, this._sQlen
);
176 this._sQlen
+= arr
.length
;
180 send_string: function (str
) {
181 this.send(str
.split('').map(function (chr
) {
182 return chr
.charCodeAt(0);
187 off: function (evt
) {
188 this._eventHandlers
[evt
] = function () {};
191 on: function (evt
, handler
) {
192 this._eventHandlers
[evt
] = handler
;
195 _allocate_buffers: function () {
196 this._rQ
= new Uint8Array(this._rQbufferSize
);
197 this._sQ
= new Uint8Array(this._sQbufferSize
);
201 this._allocate_buffers();
203 this._websocket
= null;
206 open: function (uri
, protocols
) {
207 var ws_schema
= uri
.match(/^([a-z]+):\/\//)[1];
210 this._websocket
= new WebSocket(uri
, protocols
);
211 this._websocket
.binaryType
= 'arraybuffer';
213 this._websocket
.onmessage
= this._recv_message
.bind(this);
214 this._websocket
.onopen
= (function () {
215 Log
.Debug('>> WebSock.onopen');
216 if (this._websocket
.protocol
) {
217 Log
.Info("Server choose sub-protocol: " + this._websocket
.protocol
);
220 this._eventHandlers
.open();
221 Log
.Debug("<< WebSock.onopen");
223 this._websocket
.onclose
= (function (e
) {
224 Log
.Debug(">> WebSock.onclose");
225 this._eventHandlers
.close(e
);
226 Log
.Debug("<< WebSock.onclose");
228 this._websocket
.onerror
= (function (e
) {
229 Log
.Debug(">> WebSock.onerror: " + e
);
230 this._eventHandlers
.error(e
);
231 Log
.Debug("<< WebSock.onerror: " + e
);
236 if (this._websocket
) {
237 if ((this._websocket
.readyState
=== WebSocket
.OPEN
) ||
238 (this._websocket
.readyState
=== WebSocket
.CONNECTING
)) {
239 Log
.Info("Closing WebSocket connection");
240 this._websocket
.close();
243 this._websocket
.onmessage = function (e
) { return; };
248 _encode_message: function () {
249 // Put in a binary arraybuffer
250 // according to the spec, you can send ArrayBufferViews with the send method
251 return new Uint8Array(this._sQ
.buffer
, 0, this._sQlen
);
254 _expand_compact_rQ: function (min_fit
) {
255 var resizeNeeded
= min_fit
|| this._rQlen
- this._rQi
> this._rQbufferSize
/ 2;
258 // just double the size if we need to do compaction
259 this._rQbufferSize
*= 2;
261 // otherwise, make sure we satisy rQlen - rQi + min_fit < rQbufferSize / 8
262 this._rQbufferSize
= (this._rQlen
- this._rQi
+ min_fit
) * 8;
266 // we don't want to grow unboundedly
267 if (this._rQbufferSize
> MAX_RQ_GROW_SIZE
) {
268 this._rQbufferSize
= MAX_RQ_GROW_SIZE
;
269 if (this._rQbufferSize
- this._rQlen
- this._rQi
< min_fit
) {
270 throw new Exception("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE
+ " bytes, and the new message could not fit");
275 var old_rQbuffer
= this._rQ
.buffer
;
276 this._rQmax
= this._rQbufferSize
/ 8;
277 this._rQ
= new Uint8Array(this._rQbufferSize
);
278 this._rQ
.set(new Uint8Array(old_rQbuffer
, this._rQi
));
280 if (ENABLE_COPYWITHIN
) {
281 this._rQ
.copyWithin(0, this._rQi
);
283 this._rQ
.set(new Uint8Array(this._rQ
.buffer
, this._rQi
));
287 this._rQlen
= this._rQlen
- this._rQi
;
291 _decode_message: function (data
) {
292 // push arraybuffer values onto the end
293 var u8
= new Uint8Array(data
);
294 if (u8
.length
> this._rQbufferSize
- this._rQlen
) {
295 this._expand_compact_rQ(u8
.length
);
297 this._rQ
.set(u8
, this._rQlen
);
298 this._rQlen
+= u8
.length
;
301 _recv_message: function (e
) {
302 this._decode_message(e
.data
);
303 if (this.rQlen() > 0) {
304 this._eventHandlers
.message();
305 // Compact the receive queue
306 if (this._rQlen
== this._rQi
) {
309 } else if (this._rQlen
> this._rQmax
) {
310 this._expand_compact_rQ();
313 Log
.Debug("Ignoring empty message");