@csegames/camelot-unchained
Version:
Camelot Unchained Client Library
95 lines (94 loc) • 3.92 kB
JavaScript
;
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var SignalRHub_1 = require("../SignalRHub");
var __1 = require("../..");
exports.PATCHER_EVENTS_SERVERUPDATED = 'patcher/serverUpdated';
exports.PATCHER_EVENTS_SERVERUNAVAILABLE = 'patcher/serverUnavailable';
exports.PATCHER_EVENTS_ALERT = 'patcher/alert';
exports.PATCHER_EVENTS_CHARACTERREMOVED = 'patcher/characterRemoved';
exports.PATCHER_EVENTS_CHARACTERUPDATED = 'patcher/characterUpdated';
exports.PATCHER_LIFETIME_EVENT_STARTING = 'patcher/starting';
exports.PATCHER_LIFETIME_EVENT_CONNECTIONSLOW = 'patcher/connectionslow';
exports.PATCHER_LIFETIME_EVENT_RECONNECTING = 'patcher/reconnecting';
exports.PATCHER_LIFETIME_EVENT_RECONNECTED = 'patcher/reconnected';
exports.PATCHER_LIFETIME_EVENT_CONNECTING = 'patcher/connecting';
exports.PATCHER_LIFETIME_EVENT_CONNECTED = 'patcher/connected';
exports.PATCHER_LIFETIME_EVENT_DISCONNECTED = 'patcher/disconnected';
exports.PATCHER_LIFETIME_EVENT_IDENTIFIED = 'patcher/identified';
exports.PATCHER_LIFETIME_EVENT_STATECHANGED = 'patcher/statechanged';
var patcherEventsMap = [{
receive: 'serverUpdate',
send: exports.PATCHER_EVENTS_SERVERUPDATED
}, {
receive: 'serverUnavailable',
send: exports.PATCHER_EVENTS_SERVERUNAVAILABLE
}, {
receive: 'patcherAlert',
send: exports.PATCHER_EVENTS_ALERT
}, {
receive: 'characterUpdated',
send: exports.PATCHER_EVENTS_CHARACTERUPDATED
}, {
receive: 'characterRemoved',
send: exports.PATCHER_EVENTS_CHARACTERREMOVED
}];
exports.patcherHub = new SignalRHub_1.SignalRHub('patcherHub', patcherEventsMap, { debug: __1.client.debug });
var reconnectTries = 0;
////////////////////////////////////
// lifetime events
////////////////////////////////////
exports.patcherHub.onStarting = function (hub) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_STARTING, hub);
};
exports.patcherHub.onConnectionSlow = function (hub) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_CONNECTIONSLOW);
};
exports.patcherHub.onConnected = function (hub) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_CONNECTED, hub);
// if identify method fails then try to reconnect
hub.invoke('identify', __1.client.loginToken).done(function (success) {
if (!success) {
if (__1.client.debug) console.log('PatcherHub identify failed.');
// Try again!
setTimeout(function () {
return hub.onConnected(hub);
}, 5000);
return;
}
// invalidate to force a resend of all data to this client
hub.invoke('invalidate');
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_IDENTIFIED, hub);
}).fail(function () {
setTimeout(function () {
if (reconnectTries === 5) {
reconnectTries = 0;
hub.onStarting(hub);
}
reconnectTries++;
hub.onConnected(hub);
}, 5000);
});
};
exports.patcherHub.onReconnecting = function (hub) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_RECONNECTING, hub);
};
exports.patcherHub.onReconnected = function (hub) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_RECONNECTED, hub);
};
exports.patcherHub.onStateChanged = function (hub, state) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_STATECHANGED, hub, state);
switch (state.newState) {
case SignalRHub_1.ConnectionState.Connecting:
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_CONNECTING, hub);
break;
}
};
exports.patcherHub.onDisconnected = function (hub) {
__1.events.fire(exports.PATCHER_LIFETIME_EVENT_DISCONNECTED, hub);
};
exports.default = exports.patcherHub;