UNPKG

react-teamspeak-remote-app-api

Version:
179 lines (178 loc) 9.18 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TS5MessageHandler = void 0; // Handle incoming messages from TS5 client var TS5MessageHandler = /** @class */ (function () { function TS5MessageHandler(ws, dataHandler, setActiveConnectionStateId, logger) { this.activeConnectionId = 0; this.ws = ws; this.dataHandler = dataHandler; this.setActiveConnectionStateId = setActiveConnectionStateId; this.logger = logger; } TS5MessageHandler.prototype.setActiveConnection = function (connectionId) { this.activeConnectionId = connectionId; this.setActiveConnectionStateId(connectionId); }; TS5MessageHandler.prototype.parseChannelInfos = function (channelInfos, connection) { var _this = this; var _a; channelInfos.rootChannels.forEach(function (channel) { _this.dataHandler.addChannel(__assign(__assign({}, channel), { connection: connection })); }); for (var key in channelInfos.subChannels) { (_a = channelInfos.subChannels[key]) === null || _a === void 0 ? void 0 : _a.forEach(function (subChannel) { _this.dataHandler.addChannel(__assign(__assign({}, subChannel), { connection: connection })); }); } }; // This message is sent by the TS5 server when the client is connected // It contains the initial data TS5MessageHandler.prototype.handleAuthMessage = function (data) { var _this = this; localStorage.setItem("apiKey", data.payload.apiKey); // Process auth payload and add initial data data.payload.connections.forEach(function (connection) { var _a; _this.dataHandler.addConnection(connection); // Add channels if (connection.channelInfos !== undefined) { _this.parseChannelInfos(connection.channelInfos, connection); } // Add clients (_a = connection.clientInfos) === null || _a === void 0 ? void 0 : _a.forEach(function (clientInfo) { var clientChannel = _this.dataHandler.getChannelById(clientInfo.channelId, connection.id); if (clientChannel !== undefined) { _this.dataHandler.addClient({ id: clientInfo.id, talkStatus: 0, channel: __assign(__assign({}, clientChannel), { connection: connection }), properties: clientInfo.properties, }); } }); }); }; // This message is sent by the TS5 server when a client moves a channel OR joins/leaves the server TS5MessageHandler.prototype.handleClientMovedMessage = function (data) { var _this = this; var client = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId); //* This gets called when we are connecting to the server and the new clients get loaded if (+data.payload.oldChannelId == 0) { // Create new client(when connecting to server) //set timeout to wait for channels to be created setTimeout(function () { var newChannel = _this.dataHandler.getChannelById(data.payload.newChannelId, data.payload.connectionId); if (newChannel !== undefined) { _this.dataHandler.addClient({ id: data.payload.clientId, talkStatus: 0, channel: newChannel, properties: data.payload.properties, }); _this.logger.ts("New Client found (".concat(data.payload.connectionId, " - ").concat(data.payload.clientId, " - ").concat(data.payload.properties.nickname, ")")); } }, 2000); } else { //* This gets called when a client moves a channel OR joins/leaves the server var newChannel = this.dataHandler.getChannelById(data.payload.newChannelId, data.payload.connectionId); if (newChannel === undefined || newChannel.id === 0) { this.logger.ts("Client left (".concat(data.payload.connectionId, " - ").concat(data.payload.clientId, " - ").concat(data.payload.properties.nickname, ")")); if (client !== undefined) { this.dataHandler.removeClient(client); } return; } if (client !== undefined) { // Client already exists this.logger.ts("Client moved (".concat(client.channel.connection.id, " - ").concat(client.id, " - ").concat(client.properties.nickname, ")")); this.dataHandler.updateClient(__assign(__assign({}, client), { channel: newChannel })); } else { // Client does not exist // Client joined this.logger.ts("Client joined (".concat(data.payload.connectionId, " - ").concat(data.payload.clientId, " - ").concat(data.payload.properties.nickname, ")")); this.dataHandler.addClient({ id: data.payload.clientId, talkStatus: 0, channel: newChannel, properties: data.payload.properties, }); } } }; TS5MessageHandler.prototype.handleClientPropertiesUpdatedMessage = function (data) { var client = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId); if (client !== undefined) { this.dataHandler.updateClient(__assign(__assign({}, client), { properties: data.payload.properties })); } }; TS5MessageHandler.prototype.handleTalkStatusChangedMessage = function (data) { var client = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId); if (client !== undefined) { this.dataHandler.updateClient(__assign(__assign({}, client), { talkStatus: data.payload.status })); } // console.log(this.dataHandler.localConnections) // console.log(this.dataHandler.localChannels) // console.log(this.dataHandler.localClients) }; TS5MessageHandler.prototype.handleClientSelfPropertyUpdatedMessage = function (data) { var connection = this.dataHandler.getConnectionById(this.activeConnectionId); if (data.payload.flag == "inputHardware" || connection == undefined) { // sadly thats the only way to detect if a server is active or not this.setActiveConnection(data.payload.connectionId); } }; TS5MessageHandler.prototype.handleServerPropertiesUpdatedMessage = function (data) { var connection = this.dataHandler.getConnectionById(data.payload.connectionId); if (connection !== undefined) { // Update existing connection this.dataHandler.updateConnection(__assign(__assign({}, connection), { properties: data.payload.properties })); } }; TS5MessageHandler.prototype.handleConnectStatusChangedMessage = function (data) { if (data.payload.status === 0) { // Disconnected from server var connection = this.dataHandler.getConnectionById(data.payload.connectionId); if (connection !== undefined) { this.dataHandler.removeConnection(connection); } } // Status 1-3 are the connection steps (connecting, authenticating, etc.) (i guess) if (data.payload.status === 4) { // Connected to server this.dataHandler.addConnection({ id: data.payload.connectionId, clientId: data.payload.info.clientId, }); } }; TS5MessageHandler.prototype.handleChannelsMessage = function (data) { var _this = this; // Wait a bit for the connection to be added setTimeout(function () { var connection = _this.dataHandler.getConnectionById(data.payload.connectionId); if (connection !== undefined) { _this.parseChannelInfos(data.payload.info, connection); } }, 1000); }; TS5MessageHandler.prototype.handleChannelCreatedMessage = function (data) { var connection = this.dataHandler.getConnectionById(data.payload.connectionId); if (connection !== undefined) { this.dataHandler.addChannel({ id: data.payload.channelId, connection: connection, order: data.payload.properties.order, parentId: data.payload.parentId, properties: data.payload.properties, }); } }; return TS5MessageHandler; }()); exports.TS5MessageHandler = TS5MessageHandler;