UNPKG

@colyseus/uwebsockets-transport

Version:

<div align="center"> <a href="https://github.com/colyseus/colyseus"> <img src="media/logo.svg?raw=true" width="60%" height="300" /> </a> <br> <br> <a href="https://npmjs.com/package/colyseus"> <img src="https://img.shields.io/npm/dm/coly

140 lines (138 loc) 4.49 kB
"use strict"; 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); // packages/transport/uwebsockets-transport/src/uWebSocketClient.ts var uWebSocketClient_exports = {}; __export(uWebSocketClient_exports, { ReadyState: () => ReadyState, uWebSocketClient: () => uWebSocketClient, uWebSocketWrapper: () => uWebSocketWrapper }); module.exports = __toCommonJS(uWebSocketClient_exports); var import_events = __toESM(require("events"), 1); var import_uWebSockets = require("uWebSockets.js"); var import_core = require("@colyseus/core"); var uWebSocketWrapper = class extends import_events.default { constructor(ws) { super(); this.ws = ws; } }; var ReadyState = { CONNECTING: 0, OPEN: 1, CLOSING: 2, CLOSED: 3 }; var uWebSocketClient = class { constructor(id, _ref) { this.state = import_core.ClientState.JOINING; this.readyState = ReadyState.OPEN; this._enqueuedMessages = []; this.id = this.sessionId = id; this._ref = _ref; _ref.on("close", () => this.readyState = ReadyState.CLOSED); } get ref() { return this._ref; } set ref(_ref) { this._ref = _ref; this.readyState = ReadyState.OPEN; } 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' -> %O", 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.JOINED) { this._enqueuedMessages?.push(data); return; } this.raw(data, options); } raw(data, options, cb) { if (this.readyState !== ReadyState.OPEN) { return; } try { this._ref.ws.send(data, true, false); } catch (e) { this.readyState = ReadyState.CLOSED; } } error(code, message = "", cb) { this.raw(import_core.getMessageBytes[import_core.Protocol.ERROR](code, message)); if (cb) { process.nextTick(cb); } } leave(code, data) { if (this.readyState !== ReadyState.OPEN) { return; } this.readyState = ReadyState.CLOSING; if (code !== void 0) { this._ref.ws.end(code, data); } else { this._ref.ws.close(); } } 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 = { ReadyState, uWebSocketClient, uWebSocketWrapper });