@tldraw/sync-core
Version:
tldraw infinite canvas SDK (multiplayer sync).
46 lines (45 loc) • 1.4 kB
JavaScript
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);
}
}
export {
ServerSocketAdapter
};
//# sourceMappingURL=ServerSocketAdapter.mjs.map