29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
/**
|
|
* Run the co-op relay.
|
|
*
|
|
* This is the whole of "the server": it forwards validated lockstep bytes
|
|
* between the connected peers and binds each sender's assigned peerId.
|
|
*
|
|
* Usage: node scripts/net-server.ts [port]
|
|
*/
|
|
import { pathToFileURL } from 'node:url'
|
|
import { acceptKey, decodeFrame, decodeFrames, encodeFrame, startRelay, type Relay, type RelayOptions } from './net-relay.ts'
|
|
|
|
export { acceptKey, decodeFrame, decodeFrames, encodeFrame, startRelay, type Relay, type RelayOptions }
|
|
|
|
const isMain = process.argv[1] !== undefined && import.meta.url === pathToFileURL(process.argv[1]).href
|
|
|
|
if (isMain) {
|
|
const port = Number(process.argv[2] ?? '8787')
|
|
const relay = await startRelay(Number.isFinite(port) && port >= 0 && port <= 65535 ? port : 8787)
|
|
console.log(`relay listening on ${relay.url}`)
|
|
console.log('open two pages with ?ws=' + relay.url + '&peer=0 and &peer=1')
|
|
|
|
const stop = async (): Promise<void> => {
|
|
await relay.close()
|
|
process.exit(0)
|
|
}
|
|
process.on('SIGINT', () => { void stop() })
|
|
process.on('SIGTERM', () => { void stop() })
|
|
}
|