Skip to content

Commit

Permalink
lib: decouple ensureClientConnected()
Browse files Browse the repository at this point in the history
This is a backport of eb49729.
  • Loading branch information
aqrln committed Apr 3, 2017
1 parent 6583663 commit 432a3e4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 35 deletions.
12 changes: 12 additions & 0 deletions lib/transport.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

var common = {};
module.exports = common;

// Check if the client is in the connected state and throw an error otherwise
//
common.ensureClientConnected = function(client) {
if (!client.isConnected) {
throw new Error('Not connected yet');
}
};
13 changes: 3 additions & 10 deletions lib/transport.tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var jsrs = require('./record-serialization');
var common = require('./common');
var Server = require('./server');
var Client = require('./client');
var transportCommon = require('./transport.common');

var SEPARATOR = common.createZeroFilledBuffer(1);
var MAX_PACKET_SIZE = 8 * 1024 * 1024;
Expand Down Expand Up @@ -139,7 +140,7 @@ TcpClient.prototype.connect = function(callback) {
// Disconnect from the server
//
TcpClient.prototype.disconnect = function(callback) {
this._ensureConnected();
transportCommon.ensureClientConnected(this);

if (callback) {
this.socket.once('close', callback);
Expand All @@ -151,7 +152,7 @@ TcpClient.prototype.disconnect = function(callback) {
// Create a JSTP transport from the underlying TCP socket
//
TcpClient.prototype.createTransport = function() {
this._ensureConnected();
transportCommon.ensureClientConnected(this);
return new TcpTransport(this.socket);
};

Expand All @@ -162,14 +163,6 @@ TcpClient.prototype._onSocketClose = function() {
this.emit('close');
};

// Check if the client is in the connected state and throw an error otherwise
//
TcpClient.prototype._ensureConnected = function() {
if (!this.isConnected) {
throw new Error('Not connected yet');
}
};

// JSTP transport for TCP
// socket - TCP socket instance
//
Expand Down
23 changes: 8 additions & 15 deletions lib/transport.ws.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var jsrs = require('./record-serialization');
var Client = require('./client');
var common = require('./common');
var constants = require('./internal-constants');
var transportCommon = require('./transport.common');

var ws = {};
module.exports = ws;
Expand All @@ -32,7 +33,7 @@ function W3CWebSocketClient(url) {
this.url = url;
this.socket = null;
this.socketEventEmitter = null;
this.socketDidOpen = false;
this.isConnected = false;
}

util.inherits(W3CWebSocketClient, events.EventEmitter);
Expand All @@ -42,7 +43,7 @@ ws.W3CWebSocketClient = W3CWebSocketClient;
// callback - callback function
//
W3CWebSocketClient.prototype.connect = function(callback) {
if (this.socketDidOpen) {
if (this.isConnected) {
if (callback) {
callback(new Error('Already connected'));
}
Expand Down Expand Up @@ -82,7 +83,7 @@ W3CWebSocketClient.prototype.connect = function(callback) {
// Disconnect from the server
//
W3CWebSocketClient.prototype.disconnect = function(callback) {
this._ensureConnected();
transportCommon.ensureClientConnected(this);
if (callback) {
this.socketEventEmitter.once('close', callback);
}
Expand All @@ -92,36 +93,28 @@ W3CWebSocketClient.prototype.disconnect = function(callback) {
// Create a JSTP transport from the underlying WebSocket connection
//
W3CWebSocketClient.prototype.createTransport = function() {
this._ensureConnected();
transportCommon.ensureClientConnected(this);
return new W3CWebSocketTransport(this.socket, this.socketEventEmitter);
};

// Check if the client is in the connected state and throw an error otherwise
//
W3CWebSocketClient.prototype._ensureConnected = function() {
if (!this.socketDidOpen) {
throw new Error('Not connected yet');
}
};

// W3C WebSocket open event handler
//
W3CWebSocketClient.prototype._onOpen = function() {
this.socketDidOpen = true;
this.isConnected = true;
this.socketEventEmitter.emit('open');
};

// W3C WebSocket close event handler
//
W3CWebSocketClient.prototype._onClose = function() {
this.socketDidOpen = false;
this.isConnected = false;
this.socketEventEmitter.emit('close');
};

// W3C WebSocket error event handler
//
W3CWebSocketClient.prototype._onError = function(error) {
if (this.socketDidOpen) {
if (this.isConnected) {
this.socketEventEmitter.emit('error', error);
} else {
this.socketEventEmitter.emit('connectFailed', error);
Expand Down
13 changes: 3 additions & 10 deletions lib/transport.ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var common = require('./common');
var Server = require('./server');
var Client = require('./client');
var constants = require('./internal-constants');
var transportCommon = require('./transport.common');

var WebSocketServer = websocket.server;
var WebSocketClient = websocket.client;
Expand Down Expand Up @@ -214,7 +215,7 @@ JstpWebSocketClient.prototype.connect = function(callback) {
// Disconnect from the server
//
JstpWebSocketClient.prototype.disconnect = function(callback) {
this._ensureConnected();
transportCommon.ensureClientConnected(this);

if (callback) {
this.wsConnection.once('close', callback);
Expand All @@ -226,7 +227,7 @@ JstpWebSocketClient.prototype.disconnect = function(callback) {
// Create a JSTP transport from the underlying WebSocket connection
//
JstpWebSocketClient.prototype.createTransport = function() {
this._ensureConnected();
transportCommon.ensureClientConnected(this);
return new WebSocketTransport(this.wsConnection);
};

Expand All @@ -243,14 +244,6 @@ JstpWebSocketClient.prototype._onClose = function() {
this.isConnected = false;
};

// Check if the client is in the connected state and throw an error otherwise
//
JstpWebSocketClient.prototype._ensureConnected = function() {
if (!this.isConnected) {
throw new Error('Not connected yet');
}
};

// WebSocket transport for JSTP
// connection - WebSocket connection
//
Expand Down

0 comments on commit 432a3e4

Please sign in to comment.