Don't delay WebSocket flushing
The native WebSocket is in a much better position to do queue management than us. We also failed to check the return value and set up a timer, causing stalls. This gets us in sync with websockify as of 40238b00.
This commit is contained in:
parent
84b05d24b0
commit
d2467189f5
|
@ -3,10 +3,8 @@
|
||||||
* Copyright (C) 2012 Joel Martin
|
* Copyright (C) 2012 Joel Martin
|
||||||
* Licensed under MPL 2.0 (see LICENSE.txt)
|
* Licensed under MPL 2.0 (see LICENSE.txt)
|
||||||
*
|
*
|
||||||
* Websock is similar to the standard WebSocket object but Websock
|
* Websock is similar to the standard WebSocket object but with extra
|
||||||
* enables communication with raw TCP sockets (i.e. the binary stream)
|
* buffer handling.
|
||||||
* via websockify. This is accomplished by base64 encoding the data
|
|
||||||
* stream between Websock and websockify.
|
|
||||||
*
|
*
|
||||||
* Websock has built-in receive queue buffering; the message event
|
* Websock has built-in receive queue buffering; the message event
|
||||||
* does not contain actual data but is simply a notification that
|
* does not contain actual data but is simply a notification that
|
||||||
|
@ -16,7 +14,6 @@
|
||||||
|
|
||||||
/* [module]
|
/* [module]
|
||||||
* import Util from "./util";
|
* import Util from "./util";
|
||||||
* import Base64 from "./base64";
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*jslint browser: true, bitwise: true */
|
/*jslint browser: true, bitwise: true */
|
||||||
|
@ -39,9 +36,6 @@
|
||||||
this._sQlen = 0;
|
this._sQlen = 0;
|
||||||
this._sQ = null; // Send queue
|
this._sQ = null; // Send queue
|
||||||
|
|
||||||
this._mode = 'binary'; // Current WebSocket mode: 'binary', 'base64'
|
|
||||||
this.maxBufferedAmount = 200;
|
|
||||||
|
|
||||||
this._eventHandlers = {
|
this._eventHandlers = {
|
||||||
'message': function () {},
|
'message': function () {},
|
||||||
'open': function () {},
|
'open': function () {},
|
||||||
|
@ -182,24 +176,16 @@
|
||||||
Util.Debug("bufferedAmount: " + this._websocket.bufferedAmount);
|
Util.Debug("bufferedAmount: " + this._websocket.bufferedAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._websocket.bufferedAmount < this.maxBufferedAmount) {
|
if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) {
|
||||||
if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) {
|
this._websocket.send(this._encode_message());
|
||||||
this._websocket.send(this._encode_message());
|
this._sQlen = 0;
|
||||||
this._sQlen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
Util.Info("Delaying send, bufferedAmount: " +
|
|
||||||
this._websocket.bufferedAmount);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
send: function (arr) {
|
send: function (arr) {
|
||||||
this._sQ.set(arr, this._sQlen);
|
this._sQ.set(arr, this._sQlen);
|
||||||
this._sQlen += arr.length;
|
this._sQlen += arr.length;
|
||||||
return this.flush();
|
this.flush();
|
||||||
},
|
},
|
||||||
|
|
||||||
send_string: function (str) {
|
send_string: function (str) {
|
||||||
|
@ -222,80 +208,24 @@
|
||||||
this._sQ = new Uint8Array(this._sQbufferSize);
|
this._sQ = new Uint8Array(this._sQbufferSize);
|
||||||
},
|
},
|
||||||
|
|
||||||
init: function (protocols, ws_schema) {
|
init: function () {
|
||||||
this._allocate_buffers();
|
this._allocate_buffers();
|
||||||
this._rQi = 0;
|
this._rQi = 0;
|
||||||
this._websocket = null;
|
this._websocket = null;
|
||||||
|
|
||||||
// Check for full typed array support
|
|
||||||
var bt = false;
|
|
||||||
if (('Uint8Array' in window) &&
|
|
||||||
('set' in Uint8Array.prototype)) {
|
|
||||||
bt = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for full binary type support in WebSockets
|
|
||||||
// Inspired by:
|
|
||||||
// https://github.com/Modernizr/Modernizr/issues/370
|
|
||||||
// https://github.com/Modernizr/Modernizr/blob/master/feature-detects/websockets/binary.js
|
|
||||||
var wsbt = false;
|
|
||||||
try {
|
|
||||||
if (bt && ('binaryType' in WebSocket.prototype ||
|
|
||||||
!!(new WebSocket(ws_schema + '://.').binaryType))) {
|
|
||||||
Util.Info("Detected binaryType support in WebSockets");
|
|
||||||
wsbt = true;
|
|
||||||
}
|
|
||||||
} catch (exc) {
|
|
||||||
// Just ignore failed test localhost connection
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default protocols if not specified
|
|
||||||
if (typeof(protocols) === "undefined") {
|
|
||||||
protocols = 'binary';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(protocols) && protocols.indexOf('binary') > -1) {
|
|
||||||
protocols = 'binary';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!wsbt) {
|
|
||||||
throw new Error("noVNC no longer supports base64 WebSockets. " +
|
|
||||||
"Please use a browser which supports binary WebSockets.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (protocols != 'binary') {
|
|
||||||
throw new Error("noVNC no longer supports base64 WebSockets. Please " +
|
|
||||||
"use the binary subprotocol instead.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return protocols;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
open: function (uri, protocols) {
|
open: function (uri, protocols) {
|
||||||
var ws_schema = uri.match(/^([a-z]+):\/\//)[1];
|
var ws_schema = uri.match(/^([a-z]+):\/\//)[1];
|
||||||
protocols = this.init(protocols, ws_schema);
|
this.init();
|
||||||
|
|
||||||
this._websocket = new WebSocket(uri, protocols);
|
this._websocket = new WebSocket(uri, protocols);
|
||||||
|
this._websocket.binaryType = 'arraybuffer';
|
||||||
if (protocols.indexOf('binary') >= 0) {
|
|
||||||
this._websocket.binaryType = 'arraybuffer';
|
|
||||||
}
|
|
||||||
|
|
||||||
this._websocket.onmessage = this._recv_message.bind(this);
|
this._websocket.onmessage = this._recv_message.bind(this);
|
||||||
this._websocket.onopen = (function () {
|
this._websocket.onopen = (function () {
|
||||||
Util.Debug('>> WebSock.onopen');
|
Util.Debug('>> WebSock.onopen');
|
||||||
if (this._websocket.protocol) {
|
if (this._websocket.protocol) {
|
||||||
this._mode = this._websocket.protocol;
|
|
||||||
Util.Info("Server choose sub-protocol: " + this._websocket.protocol);
|
Util.Info("Server choose sub-protocol: " + this._websocket.protocol);
|
||||||
} else {
|
|
||||||
this._mode = 'binary';
|
|
||||||
Util.Error('Server select no sub-protocol!: ' + this._websocket.protocol);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._mode != 'binary') {
|
|
||||||
throw new Error("noVNC no longer supports base64 WebSockets. Please " +
|
|
||||||
"use the binary subprotocol instead.");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._eventHandlers.open();
|
this._eventHandlers.open();
|
||||||
|
|
Loading…
Reference in New Issue