Skip to content

Commit

Permalink
Merge pull request #16 from youngmonkeys/dev
Browse files Browse the repository at this point in the history
v1.2.0: merge dev
  • Loading branch information
tvd12 authored Aug 21, 2021
2 parents 2e2fd87 + f023f4a commit 25ce852
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 368 deletions.
100 changes: 55 additions & 45 deletions ezy-client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Const from './ezy-constants'
import Util from './ezy-util'
import Event from './ezy-events'
import Manager from './ezy-managers'
import Socket from './ezy-sockets'
import EzySetup from './ezy-setup'
import EzyEventMessageHandler from './ezy-event-message-handler'
import Const from './ezy-constants';
import Util from './ezy-util';
import Event from './ezy-events';
import Manager from './ezy-managers';
import Socket from './ezy-sockets';
import EzySetup from './ezy-setup';
import EzyEventMessageHandler from './ezy-event-message-handler';

/**
* Wrapper for JS built-in WebSocket to communicate with websocket server
Expand All @@ -29,47 +29,51 @@ class EzyConnector {
connect(client, url) {
this.disconnectReason = null;
this.ws = new WebSocket(url);
var thiz = this;
let that = this;
var failed = false;
var pingManager = client.pingManager;
var eventMessageHandler = client.eventMessageHandler;
let pingManager = client.pingManager;
let eventMessageHandler = client.eventMessageHandler;

this.ws.onerror = function (e) {
Util.EzyLogger.console('connect to: ' + url + ' error : ' + JSON.stringify(e));
Util.EzyLogger.console(
'connect to: ' + url + ' error : ' + JSON.stringify(e)
);
failed = true;
var event = new Event.EzyConnectionFailureEvent(Const.EzyConnectionFailedReason.UNKNOWN);
var event = new Event.EzyConnectionFailureEvent(
Const.EzyConnectionFailedReason.UNKNOWN
);
eventMessageHandler.handleEvent(event);
}
};

this.ws.onopen = function (e) {
Util.EzyLogger.console('connected to: ' + url);
client.reconnectCount = 0;
client.status = Const.EzyConnectionStatus.CONNECTED;
var event = new Event.EzyConnectionSuccessEvent();
eventMessageHandler.handleEvent(event);
}
};

this.ws.onclose = function (e) {
if (failed)
return;
if (thiz.destroyed)
return;
if (failed) return;
if (that.destroyed) return;
if (client.isConnected()) {
var reason = thiz.disconnectReason || Const.EzyDisconnectReason.UNKNOWN;
var reason =
that.disconnectReason || Const.EzyDisconnectReason.UNKNOWN;
eventMessageHandler.handleDisconnection(reason);
} else {
Util.EzyLogger.console('connection to: ' + url + " has disconnected before");
Util.EzyLogger.console(
'connection to: ' + url + ' has disconnected before'
);
}
}
};

this.ws.onmessage = function (event) {
if (thiz.destroyed)
return;
if (that.destroyed) return;
pingManager.lostPingCount = 0;
var data = event.data;
var message = JSON.parse(data);
eventMessageHandler.handleMessage(message);
}
};
}

/**
Expand Down Expand Up @@ -124,7 +128,10 @@ class EzyClient {
this.pingSchedule = new Socket.EzyPingSchedule(this);
this.handlerManager = new Manager.EzyHandlerManager(this);
this.setup = new EzySetup(this.handlerManager);
this.unloggableCommands = [Const.EzyCommand.PING, Const.EzyCommand.PONG];
this.unloggableCommands = [
Const.EzyCommand.PING,
Const.EzyCommand.PONG,
];
this.eventMessageHandler = new EzyEventMessageHandler(this);
this.pingSchedule.eventMessageHandler = this.eventMessageHandler;
}
Expand All @@ -151,17 +158,13 @@ class EzyClient {
reconnect() {
var reconnectConfig = this.config.reconnect;
var maxReconnectCount = reconnectConfig.maxReconnectCount;
if (this.reconnectCount >= maxReconnectCount)
return false;
if (this.reconnectCount >= maxReconnectCount) return false;
this.preconnect();
this.status = Const.EzyConnectionStatus.RECONNECTING;
this.reconnectTimeout = setTimeout(
() => {
this.connector = new EzyConnector();
this.connector.connect(this, this.url);
},
reconnectConfig.reconnectPeriod
);
this.reconnectTimeout = setTimeout(() => {
this.connector = new EzyConnector();
this.connector.connect(this, this.url);
}, reconnectConfig.reconnectPeriod);
this.reconnectCount++;
var event = new Event.EzyTryConnectEvent(this.reconnectCount);
this.eventMessageHandler.handleEvent(event);
Expand All @@ -174,10 +177,8 @@ class EzyClient {
this.zone = null;
this.me = null;
this.appsById = {};
if (this.connector)
this.connector.destroy();
if (this.reconnectTimeout)
clearTimeout(this.reconnectTimeout);
if (this.connector) this.connector.destroy();
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
}

/**
Expand All @@ -194,16 +195,15 @@ class EzyClient {
}

internalDisconnect(reason) {
if (this.connector)
this.connector.disconnect(reason);
if (this.connector) this.connector.disconnect(reason);
}

/**
* Send data to websocket server
* @param data
*/
send(cmd, data) {
this.sendRequest(cmd, data)
this.sendRequest(cmd, data);
}

/**
Expand All @@ -213,7 +213,9 @@ class EzyClient {
*/
sendRequest(cmd, data) {
if (!this.unloggableCommands.includes(cmd)) {
Util.EzyLogger.console('send cmd: ' + cmd.name + ", data: " + JSON.stringify(data));
Util.EzyLogger.console(
'send cmd: ' + cmd.name + ', data: ' + JSON.stringify(data)
);
}
var request = [cmd.id, data];
this.connector.send(request);
Expand All @@ -234,15 +236,15 @@ class EzyClient {
* @returns {boolean} Whether or not the status is CONNECTED
*/
isConnected() {
var connected = (this.status == Const.EzyConnectionStatus.CONNECTED);
var connected = this.status === Const.EzyConnectionStatus.CONNECTED;
return connected;
}

/**
* Get first app from this client zone
* @returns {EzyApp} Queried app
*/
getApp() {
getApp() {
if (!this.zone) return null;
var appManager = this.zone.appManager;
return appManager.getApp();
Expand Down Expand Up @@ -270,6 +272,10 @@ class EzyClient {
return pluginManager.getPluginById(pluginId);
}

newAppManager(zoneName) {
return new Manager.EzyAppManager(zoneName);
}

/**
* Get the app manager of this client zone
* @returns {EzyAppManager} App manager of current client zone
Expand All @@ -279,6 +285,10 @@ class EzyClient {
return this.zone.appManager;
}

newPluginManager(zoneName) {
return new Manager.EzyPluginManager(zoneName);
}

/**
* Get the plugin manager of this client zone
* @returns {EzyPluginManager} Plugin manager of current client zone
Expand All @@ -289,4 +299,4 @@ class EzyClient {
}
}

export default EzyClient
export default EzyClient;
9 changes: 4 additions & 5 deletions ezy-clients.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EzyClient from './ezy-client'
import EzyClient from './ezy-client';

/**
* Singleton object to manage all clients of a server.
Expand All @@ -8,7 +8,7 @@ import EzyClient from './ezy-client'
class EzyClients {
constructor() {
this.clients = {};
this.defaultClientName = "";
this.defaultClientName = '';
}

/**
Expand All @@ -30,8 +30,7 @@ class EzyClients {
newClient(config) {
var client = new EzyClient(config);
this.addClient(client);
if (this.defaultClientName == "")
this.defaultClientName = client.name;
if (this.defaultClientName === '') this.defaultClientName = client.name;
return client;
}

Expand Down Expand Up @@ -72,4 +71,4 @@ class EzyClients {
}
}

export default EzyClients
export default EzyClients;
9 changes: 4 additions & 5 deletions ezy-configs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EzyClientConfig {
constructor() {
this.zoneName = "";
this.clientName = "";
this.zoneName = '';
this.clientName = '';
this.reconnect = new EzyReconnectConfig();
}

Expand All @@ -10,8 +10,7 @@ class EzyClientConfig {
* @returns {string} Client name
*/
getClientName() {
if (this.clientName == "")
return this.zoneName;
if (this.clientName === '') return this.zoneName;
return this.clientName;
}
}
Expand All @@ -24,4 +23,4 @@ class EzyReconnectConfig {
}
}

export default {EzyClientConfig}
export default { EzyClientConfig };
Loading

0 comments on commit 25ce852

Please sign in to comment.