@x5e/gink
Version:
an eventually consistent database
38 lines • 1.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerConnection = void 0;
const AbstractConnection_1 = require("./AbstractConnection");
class ServerConnection extends AbstractConnection_1.AbstractConnection {
constructor(args) {
super();
const { onData, onClose, websocketConnection } = args;
this.onData = onData;
this.onClose = onClose;
this.websocketConnection = websocketConnection;
this.websocketConnection.on("message", this.onMessage.bind(this));
this.websocketConnection.on("close", this.onClose.bind(this));
this.websocketConnection.on("error", this.onClose.bind(this));
}
send(data) {
this.websocketConnection.sendBytes(Buffer.from(data));
}
close() {
this.websocketConnection.close();
}
onMessage(message) {
if (message.type === "utf8") {
console.error(`Received Text Message: ${message.utf8Data}`);
}
else if (message.type === "binary") {
this.onData(message.binaryData).catch((reason) => {
console.error("something went wrong receiving data\n", reason);
this.close();
});
}
}
get connected() {
return this.websocketConnection.connected;
}
}
exports.ServerConnection = ServerConnection;
//# sourceMappingURL=ServerConnection.js.map