import { emitter } from '@amatiasq/emitter'; import { SocketTransport } from '../transport.js'; import { JsonConnection } from './JsonConnection.js'; /** * The server side of the JSON layer: hand it each incoming socket and it hands * you back a `JsonConnection`. * * **It does not listen on anything.** This was `NiceSocketServer`, which * `extend`ed the `ws` server and took an `http.Server` in its constructor. Now * the caller owns the listening socket, which is what lets this run on Bun, Deno * and Workers as well as Node — see `SocketTransport`. * * const server = new JsonServer(); * server.onConnection(client => client.send({ hello: true })); * * // Node, with `ws`: * wss.on('connection', ws => server.accept(fromEventEmitter(ws))); */ export class JsonServer { private readonly emitConnection = emitter>(); readonly onConnection = this.emitConnection.subscribe; accept(transport: SocketTransport) { const connection = new JsonConnection(transport); this.emitConnection(connection); return connection; } }