diff options
Diffstat (limited to 'js/src/Ice/browser/WSTransceiver.js')
-rw-r--r-- | js/src/Ice/browser/WSTransceiver.js | 188 |
1 files changed, 100 insertions, 88 deletions
diff --git a/js/src/Ice/browser/WSTransceiver.js b/js/src/Ice/browser/WSTransceiver.js index 675bcbfd518..dae8f618430 100644 --- a/js/src/Ice/browser/WSTransceiver.js +++ b/js/src/Ice/browser/WSTransceiver.js @@ -7,10 +7,9 @@ // // ********************************************************************** -var Ice = require("../Ice/ModuleRegistry").Ice; +const Ice = require("../Ice/ModuleRegistry").Ice; Ice.__M.require(module, [ - "../Ice/Class", "../Ice/Debug", "../Ice/ExUtil", "../Ice/Network", @@ -21,7 +20,7 @@ Ice.__M.require(module, "../Ice/Timer", "../Ice/ConnectionInfo" ]); -var IceSSL = Ice.__M.module("IceSSL"); +const IceSSL = Ice.__M.module("IceSSL"); // // With Chrome we don't want to close the socket while connection is in progress, @@ -29,49 +28,51 @@ var IceSSL = Ice.__M.module("IceSSL"); // // We need to check for Edge browser as it might include Chrome in its user agent. // -var IsChrome = navigator.userAgent.indexOf("Edge/") === -1 && - navigator.userAgent.indexOf("Chrome/") !== -1; - -var Debug = Ice.Debug; -var ExUtil = Ice.ExUtil; -var Network = Ice.Network; -var SocketOperation = Ice.SocketOperation; -var Conn = Ice.Connection; -var LocalException = Ice.LocalException; -var SocketException = Ice.SocketException; -var Timer = Ice.Timer; - -var StateNeedConnect = 0; -var StateConnectPending = 1; -var StateConnected = 2; -var StateClosePending = 3; -var StateClosed = 4; - -var WSTransceiver = Ice.Class({ - __init__: function(instance) +const IsChrome = navigator.userAgent.indexOf("Edge/") === -1 && + navigator.userAgent.indexOf("Chrome/") !== -1; + +const Debug = Ice.Debug; +const ExUtil = Ice.ExUtil; +const Network = Ice.Network; +const SocketOperation = Ice.SocketOperation; +const Conn = Ice.Connection; +const LocalException = Ice.LocalException; +const SocketException = Ice.SocketException; +const Timer = Ice.Timer; + +const StateNeedConnect = 0; +const StateConnectPending = 1; +const StateConnected = 2; +const StateClosePending = 3; +const StateClosed = 4; + +class WSTransceiver +{ + constructor(instance) { - this._logger = instance.logger(); this._readBuffers = []; this._readPosition = 0; this._maxSendPacketSize = instance.properties().getPropertyAsIntWithDefault("Ice.TCP.SndSize", 512 * 1024); this._writeReadyTimeout = 0; - }, - writeReadyTimeout: function() + } + + writeReadyTimeout() { var t = Math.round(this._writeReadyTimeout); this._writeReadyTimeout += (this._writeReadyTimeout >= 5 ? 5 : 0.2); return Math.min(t, 25); - }, - setCallbacks: function(connectedCallback, bytesAvailableCallback, bytesWrittenCallback) + } + + setCallbacks(connectedCallback, bytesAvailableCallback, bytesWrittenCallback) { this._connectedCallback = connectedCallback; this._bytesAvailableCallback = bytesAvailableCallback; this._bytesWrittenCallback = bytesWrittenCallback; - }, + } // // Returns SocketOperation.None when initialization is complete. // - initialize: function(readBuffer, writeBuffer) + initialize(readBuffer, writeBuffer) { try { @@ -85,10 +86,9 @@ var WSTransceiver = Ice.Class({ this._state = StateConnectPending; this._fd = new WebSocket(this._url, "ice.zeroc.com"); this._fd.binaryType = "arraybuffer"; - var self = this; - this._fd.onopen = function(e) { self.socketConnected(e); }; - this._fd.onmessage = function(e) { self.socketBytesAvailable(e.data); }; - this._fd.onclose = function(e) { self.socketClosed(e); }; + this._fd.onopen = e => this.socketConnected(e); + this._fd.onmessage = e => this.socketBytesAvailable(e.data); + this._fd.onclose = e => this.socketClosed(e); return SocketOperation.Connect; // Waiting for connect to complete. } else if(this._state === StateConnectPending) @@ -111,8 +111,9 @@ var WSTransceiver = Ice.Class({ Debug.assert(this._state === StateConnected); return SocketOperation.None; - }, - register: function() + } + + register() { // // Register the socket data listener. @@ -123,15 +124,17 @@ var WSTransceiver = Ice.Class({ this._bytesAvailableCallback(); this._hasBytesAvailable = false; } - }, - unregister: function() + } + + unregister() { // // Unregister the socket data listener. // this._registered = false; - }, - close: function() + } + + close() { if(this._fd === null) { @@ -167,11 +170,12 @@ var WSTransceiver = Ice.Class({ { this._fd = null; } - }, + } + // // Returns true if all of the data was flushed to the kernel buffer. // - write: function(byteBuffer) + write(byteBuffer) { if(this._exception) { @@ -195,12 +199,11 @@ var WSTransceiver = Ice.Class({ Debug.assert(packetSize > 0); if(this._fd.bufferedAmount + packetSize > this._maxSendPacketSize) { - var transceiver = this; - Timer.setTimeout(function() + Timer.setTimeout(() => { - if(transceiver._fd && transceiver._fd.bufferedAmount + packetSize <= transceiver._maxSendPacketSize) + if(this._fd && this._fd.bufferedAmount + packetSize <= this._maxSendPacketSize) { - transceiver._bytesWrittenCallback(); + this._bytesWrittenCallback(); } }, this.writeReadyTimeout()); @@ -212,8 +215,9 @@ var WSTransceiver = Ice.Class({ byteBuffer.position = byteBuffer.position + packetSize; } return true; - }, - read: function(byteBuffer, moreData) + } + + read(byteBuffer, moreData) { if(this._exception) { @@ -264,12 +268,14 @@ var WSTransceiver = Ice.Class({ moreData.value = this._readBuffers.byteLength > 0; return byteBuffer.remaining === 0; - }, - type: function() + } + + type() { return this._secure ? "wss" : "ws"; - }, - getInfo: function() + } + + getInfo() { Debug.assert(this._fd !== null); var info = new Ice.WSConnectionInfo(); @@ -283,19 +289,23 @@ var WSTransceiver = Ice.Class({ info.sndSize = this._maxSendPacketSize; info.headers = {}; return info; - }, - checkSendSize: function(stream) + } + + checkSendSize(stream) { - }, - setBufferSize: function(rcvSize, sndSize) + } + + setBufferSize(rcvSize, sndSize) { this._maxSendPacketSize = sndSize; - }, - toString: function() + } + + toString() { return this._desc; - }, - socketConnected: function(e) + } + + socketConnected(e) { if(this._state == StateClosePending) { @@ -305,8 +315,9 @@ var WSTransceiver = Ice.Class({ Debug.assert(this._connectedCallback !== null); this._connectedCallback(); - }, - socketBytesAvailable: function(buf) + } + + socketBytesAvailable(buf) { Debug.assert(this._bytesAvailableCallback !== null); if(buf.byteLength > 0) @@ -321,8 +332,9 @@ var WSTransceiver = Ice.Class({ this._hasBytesAvailable = true; } } - }, - socketClosed: function(err) + } + + socketClosed(err) { if(this._state == StateClosePending) { @@ -339,8 +351,30 @@ var WSTransceiver = Ice.Class({ { this._bytesAvailableCallback(); } - }, -}); + } + + static createOutgoing(instance, secure, addr, resource) + { + var transceiver = new WSTransceiver(instance); + + var url = secure ? "wss" : "ws"; + url += "://" + addr.host; + if(addr.port !== 80) + { + url += ":" + addr.port; + } + url += resource ? resource : "/"; + transceiver._url = url; + transceiver._fd = null; + transceiver._addr = addr; + transceiver._desc = "local address = <not available>\nremote address = " + addr.host + ":" + addr.port; + transceiver._state = StateNeedConnect; + transceiver._secure = secure; + transceiver._exception = null; + + return transceiver; + } +} function fdToString(address) { @@ -363,26 +397,4 @@ function translateError(state, err) } } -WSTransceiver.createOutgoing = function(instance, secure, addr, resource) -{ - var transceiver = new WSTransceiver(instance); - - var url = secure ? "wss" : "ws"; - url += "://" + addr.host; - if(addr.port !== 80) - { - url += ":" + addr.port; - } - url += resource ? resource : "/"; - transceiver._url = url; - transceiver._fd = null; - transceiver._addr = addr; - transceiver._desc = "local address = <not available>\nremote address = " + addr.host + ":" + addr.port; - transceiver._state = StateNeedConnect; - transceiver._secure = secure; - transceiver._exception = null; - - return transceiver; -}; - Ice.WSTransceiver = WSTransceiver; |