@colyseus/ws-transport
Version:
```typescript import { Server } from "@colyseus/core"; import { WebSocketTransport } from "@colyseus/ws-transport";
101 lines (100 loc) • 3.6 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var WebSocketClient_exports = {};
__export(WebSocketClient_exports, {
WebSocketClient: () => WebSocketClient
});
module.exports = __toCommonJS(WebSocketClient_exports);
var import_ws = __toESM(require("ws"));
var import_core = require("@colyseus/core");
const SEND_OPTS = { binary: true };
class WebSocketClient {
constructor(id, ref) {
this.id = id;
this.ref = ref;
this.state = import_core.ClientState.JOINING;
this._enqueuedMessages = [];
this.sessionId = id;
}
sendBytes(type, bytes, options) {
(0, import_core.debugMessage)("send bytes(to %s): '%s' -> %j", this.sessionId, type, bytes);
this.enqueueRaw(
import_core.getMessageBytes.raw(import_core.Protocol.ROOM_DATA_BYTES, type, void 0, bytes),
options
);
}
send(messageOrType, messageOrOptions, options) {
(0, import_core.debugMessage)("send(to %s): '%s' -> %j", this.sessionId, messageOrType, messageOrOptions);
this.enqueueRaw(
import_core.getMessageBytes.raw(import_core.Protocol.ROOM_DATA, messageOrType, messageOrOptions),
options
);
}
enqueueRaw(data, options) {
if (options?.afterNextPatch) {
this._afterNextPatchQueue.push([this, [data]]);
return;
}
if (this.state === import_core.ClientState.JOINING) {
this._enqueuedMessages.push(data);
return;
}
this.raw(data, options);
}
raw(data, options, cb) {
if (this.ref.readyState !== import_ws.default.OPEN) {
return;
}
this.ref.send(data, SEND_OPTS, cb);
}
error(code, message = "", cb) {
this.raw(import_core.getMessageBytes[import_core.Protocol.ERROR](code, message), void 0, cb);
}
get readyState() {
return this.ref.readyState;
}
leave(code, data) {
this.ref.close(code, data);
}
close(code, data) {
import_core.logger.warn("DEPRECATION WARNING: use client.leave() instead of client.close()");
try {
throw new Error();
} catch (e) {
import_core.logger.info(e.stack);
}
this.leave(code, data);
}
toJSON() {
return { sessionId: this.sessionId, readyState: this.readyState };
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
WebSocketClient
});