UNPKG

@tldraw/sync-core

Version:

tldraw infinite canvas SDK (multiplayer sync).

66 lines (65 loc) 2.36 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var ServerSocketAdapter_exports = {}; __export(ServerSocketAdapter_exports, { ServerSocketAdapter: () => ServerSocketAdapter }); module.exports = __toCommonJS(ServerSocketAdapter_exports); class ServerSocketAdapter { /** * Creates a new ServerSocketAdapter instance. * * opts - Configuration options for the adapter */ constructor(opts) { this.opts = opts; } /** * Checks if the underlying WebSocket connection is currently open and ready to send messages. * * @returns True if the connection is open (readyState === 1), false otherwise */ // eslint-disable-next-line no-restricted-syntax get isOpen() { return this.opts.ws.readyState === 1; } /** * Sends a sync protocol message to the connected client. The message is JSON stringified * before being sent through the WebSocket. If configured, the onBeforeSendMessage callback * is invoked before sending. * * @param msg - The sync protocol message to send */ // see TLRoomSocket for details on why this accepts a union and not just arrays sendMessage(msg) { const message = JSON.stringify(msg); this.opts.onBeforeSendMessage?.(msg, message); this.opts.ws.send(message); } /** * Closes the WebSocket connection with an optional close code and reason. * * @param code - Optional close code (default: 1000 for normal closure) * @param reason - Optional human-readable reason for closing */ close(code, reason) { this.opts.ws.close(code, reason); } } //# sourceMappingURL=ServerSocketAdapter.js.map