shardy
Version:
Framework for online games and applications on Node.js
87 lines • 3.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connection = void 0;
const Transport_1 = require("./Transport");
const Logger_1 = require("./Logger");
const Tools_1 = require("./Tools");
const LOG_TAG = Tools_1.Tools.getTag(module);
const OPEN_EVENT = 'open';
const MESSAGE_EVENT = 'message';
const DATA_EVENT = 'data';
const ERROR_EVENT = 'error';
const CLOSE_EVENT = 'close';
var WebSocketCloseCode;
(function (WebSocketCloseCode) {
WebSocketCloseCode[WebSocketCloseCode["NotSet"] = 0] = "NotSet";
WebSocketCloseCode[WebSocketCloseCode["Normal"] = 1000] = "Normal";
WebSocketCloseCode[WebSocketCloseCode["Away"] = 1001] = "Away";
WebSocketCloseCode[WebSocketCloseCode["ProtocolError"] = 1002] = "ProtocolError";
WebSocketCloseCode[WebSocketCloseCode["UnsupportedData"] = 1003] = "UnsupportedData";
WebSocketCloseCode[WebSocketCloseCode["Undefined"] = 1004] = "Undefined";
WebSocketCloseCode[WebSocketCloseCode["NoStatus"] = 1005] = "NoStatus";
WebSocketCloseCode[WebSocketCloseCode["Abnormal"] = 1006] = "Abnormal";
WebSocketCloseCode[WebSocketCloseCode["InvalidData"] = 1007] = "InvalidData";
WebSocketCloseCode[WebSocketCloseCode["PolicyViolation"] = 1008] = "PolicyViolation";
WebSocketCloseCode[WebSocketCloseCode["TooBig"] = 1009] = "TooBig";
WebSocketCloseCode[WebSocketCloseCode["MandatoryExtension"] = 1010] = "MandatoryExtension";
WebSocketCloseCode[WebSocketCloseCode["ServerError"] = 1011] = "ServerError";
WebSocketCloseCode[WebSocketCloseCode["TlsHandshakeFailure"] = 1015] = "TlsHandshakeFailure";
})(WebSocketCloseCode || (WebSocketCloseCode = {}));
class Connection {
constructor(socket, type) {
this.socket = socket;
this.type = type;
this.onConnect = () => { };
this.onData = () => { };
this.onError = () => { };
this.onClose = () => { };
this.socket.on(this.type === Transport_1.TransportType.TCP ? DATA_EVENT : MESSAGE_EVENT, (data) => this.onData(data));
this.socket.on(ERROR_EVENT, (error) => this.onError(error));
this.socket.on(CLOSE_EVENT, () => this.onClose());
if (this.type === Transport_1.TransportType.WebSocket) {
this.socket.on(OPEN_EVENT, () => this.onConnect());
}
}
setLogger(log) {
this.log = log;
}
close() {
switch (this.type) {
case Transport_1.TransportType.TCP:
this.socket.end();
break;
case Transport_1.TransportType.WebSocket:
this.socket.close(WebSocketCloseCode.Normal);
break;
default:
break;
}
}
destroy() {
this.log.info(`[${LOG_TAG}] destroy`, Logger_1.LoggerScope.Debug);
switch (this.type) {
case Transport_1.TransportType.TCP:
this.socket.destroy();
break;
case Transport_1.TransportType.WebSocket:
this.socket.terminate();
break;
default:
break;
}
}
send(data) {
switch (this.type) {
case Transport_1.TransportType.TCP:
this.socket.write(data);
break;
case Transport_1.TransportType.WebSocket:
this.socket.send(data);
break;
default:
break;
}
}
}
exports.Connection = Connection;
//# sourceMappingURL=Connection.js.map