UNPKG

@csegames/camelot-unchained

Version:

Camelot Unchained Client Library

155 lines (154 loc) 6.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* * 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/. */ var events_1 = require("../events"); var client_1 = require("../core/client"); var eventMapper_1 = require("../utils/eventMapper"); function signalRToEvents(recieve, send, hub, hubName, debug) { if (!hub) return; hub.on(recieve, function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } events_1.default.fire.apply(events_1.default, [send].concat(params)); }); } var ConnectionState; (function (ConnectionState) { ConnectionState[ConnectionState["Connecting"] = 0] = "Connecting"; ConnectionState[ConnectionState["Connected"] = 1] = "Connected"; ConnectionState[ConnectionState["Reconnecting"] = 2] = "Reconnecting"; ConnectionState[ConnectionState["Disconnected"] = 4] = "Disconnected"; })(ConnectionState = exports.ConnectionState || (exports.ConnectionState = {})); var SignalRHub = function () { function SignalRHub(hubName, eventMaps, options, signalRHost) { if (signalRHost === void 0) { signalRHost = client_1.default.signalRHost; } var _this = this; this.reconnectOnDisconnect = true; this.wantStop = false; this.tryingToReconnect = false; this.connectionState = ConnectionState.Disconnected; //////////////////////////////////// // lifetime events //////////////////////////////////// // Raised before any data is sent over the connection this.internalOnStarting = function () { if (_this.onStarting) _this.onStarting(_this); }; // Raised when any data is received on the connection. Provides the received data this.internalOnReceived = function (data) { if (_this.onReceived) _this.onReceived(_this, data); }; // Raised when the client detecs a slow or frequently dropping connection this.internalOnConnectionSlow = function () { if (_this.onConnectionSlow) _this.onConnectionSlow(_this); }; // Raised when the underlying transport begins reconnecting this.internalOnReconnecting = function () { if (_this.onReconnecting) _this.onReconnecting(_this); }; // Raised when the underlying transport has reconnected this.internalOnReconnected = function () { if (_this.onReconnected) _this.onReconnected(_this); }; // Raised when the connection state changes. Provides the old state and the new state // (Connecting, Connected, Reconnecting, or Disconnect) this.internalOnStateChanged = function (state) { _this.connectionState = state.newState; if (state.newState === ConnectionState.Connected) { if (_this.onConnected) _this.onConnected(_this); } if (_this.onStateChanged) _this.onStateChanged(_this, state); }; // Raised when the connection has disconnected this.internalOnDisconnected = function () { // try to reconnect again in 5 seconds. if (_this.reconnectOnDisconnect) { setTimeout(function () { _this.start(); }, 5000); } if (_this.onDisconnected) _this.onDisconnected(_this); }; //////////////////////////////////// // error handling //////////////////////////////////// this.internalOnError = function (error) { if (_this.onError) _this.onError(_this, error); }; this.hubName = hubName; this.eventMaps = eventMaps; this.signalRHost = signalRHost; if (options) { this.debug = options.debug || false; this.reconnectOnDisconnect = options.reconnectOnDisconnect || true; } } SignalRHub.prototype.start = function (onStart, options) { var _this = this; if (options) { if (options.host) { this.signalRHost = options.host; } } this.conn = $.hubConnection(); this.conn.url = this.signalRHost; this.hub = this.conn.createHubProxy(this.hubName); // hook up lifetime events this.conn.starting(this.internalOnStarting); this.conn.received(this.internalOnReceived); this.conn.connectionSlow(this.internalOnConnectionSlow); this.conn.reconnecting(this.internalOnReconnecting); this.conn.reconnected(this.internalOnReconnected); this.conn.stateChanged(this.internalOnStateChanged); this.conn.disconnected(this.internalOnDisconnected); // hoook up error handler this.conn.error(this.internalOnError); if (client_1.default.debug) { this.conn.logging = true; } this.registerEvents(); this.conn.start().done(function () { if (onStart) onStart(_this); }); }; SignalRHub.prototype.stop = function () { this.conn.stop(); }; SignalRHub.prototype.invoke = function (method) { var params = []; for (var _i = 1; _i < arguments.length; _i++) { params[_i - 1] = arguments[_i]; } return (_a = this.hub).invoke.apply(_a, [method].concat(params)); var _a; }; //////////////////////////////////// // map to event emitters //////////////////////////////////// SignalRHub.prototype.registerEvents = function () { eventMapper_1.eventMapper(this.eventMaps, signalRToEvents, this.hub, this.hubName, this.debug); }; SignalRHub.prototype.unregisterEvents = function () { var _this = this; if (this.hub) { this.eventMaps.map(function (evt) { _this.hub.off(evt.receive); events_1.default.off(evt.send); }); } else { this.eventMaps.map(function (evt) { events_1.default.off(evt.send); }); } }; return SignalRHub; }(); exports.SignalRHub = SignalRHub;