| 128 | * ``` |
| 129 | */ |
| 130 | export class ServerSocketAdapter<R extends UnknownRecord> implements TLRoomSocket<R> { |
| 131 | /** |
| 132 | * Creates a new ServerSocketAdapter instance. |
| 133 | * |
| 134 | * opts - Configuration options for the adapter |
| 135 | */ |
| 136 | constructor(public readonly opts: ServerSocketAdapterOptions<R>) {} |
| 137 | |
| 138 | /** |
| 139 | * Checks if the underlying WebSocket connection is currently open and ready to send messages. |
| 140 | * |
| 141 | * @returns True if the connection is open (readyState === 1), false otherwise |
| 142 | */ |
| 143 | // eslint-disable-next-line tldraw/no-setter-getter |
| 144 | get isOpen(): boolean { |
| 145 | return this.opts.ws.readyState === 1 // ready state open |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sends a sync protocol message to the connected client. The message is JSON stringified |
| 150 | * before being sent through the WebSocket. If configured, the onBeforeSendMessage callback |
| 151 | * is invoked before sending. |
| 152 | * |
| 153 | * @param msg - The sync protocol message to send |
| 154 | */ |
| 155 | // see TLRoomSocket for details on why this accepts a union and not just arrays |
| 156 | sendMessage(msg: TLSocketServerSentEvent<R>) { |
| 157 | const message = JSON.stringify(msg) |
| 158 | this.opts.onBeforeSendMessage?.(msg, message) |
| 159 | this.opts.ws.send(message) |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Closes the WebSocket connection with an optional close code and reason. |
| 164 | * |
| 165 | * @param code - Optional close code (default: 1000 for normal closure) |
| 166 | * @param reason - Optional human-readable reason for closing |
| 167 | */ |
| 168 | close(code?: number, reason?: string) { |
| 169 | this.opts.ws.close(code, reason) |
| 170 | } |
| 171 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…