Skip to content

Commit fe72989

Browse files
committed
Server.ts: use WebSocketServer
1 parent 36e5d36 commit fe72989

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

src/Server.ts

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import debug from "debug"
22
import express from "express"
3-
import expressWs from "express-ws"
4-
import WebSocket from "isomorphic-ws"
3+
import expressWs, { Application } from "express-ws"
4+
import { WebSocket, WebSocketServer } from "isomorphic-ws"
55
import { pack, unpack } from "msgpackr"
6-
import { Server as HttpServer, Socket } from "net"
6+
import { Socket as HttpSocket } from "net"
77
import pkg from "../package.json" assert { type: "json" }
88
import { EventEmitter } from "./lib/EventEmitter.js"
99
import { deduplicate } from "./lib/deduplicate.js"
@@ -20,8 +20,9 @@ type ServerEvents = {
2020
ready: () => void
2121
close: () => void
2222
error: (payload: { error: Error; data: Uint8Array }) => void
23-
introductionConnection: (userName: UserName) => void
23+
introduction: (userName: UserName) => void
2424
}
25+
2526
/**
2627
* This server provides two services:
2728
*
@@ -56,63 +57,65 @@ export class Server extends EventEmitter<ServerEvents> {
5657
/**
5758
* Keep these references for cleanup
5859
*/
59-
private httpServer?: HttpServer
60-
private httpSockets: Socket[] = []
60+
private socket: WebSocketServer
61+
private app: Application
62+
private sockets = new Set<WebSocket>()
6163

6264
public log: debug.Debugger
6365

6466
constructor({ port = 8080 } = {}) {
6567
super()
68+
this.port = port
69+
this.app = expressWs(express()).app
70+
this.socket = new WebSocketServer({ noServer: true })
71+
6672
this.log = debug(`lf:relay:${port}`)
6773
this.log("version", version)
68-
this.port = port
6974
}
7075

7176
// SERVER
7277

7378
listen({ silent = false }: ListenOptions = {}) {
7479
return new Promise<void>((resolve, reject) => {
75-
// Allow hitting this server from a browser as a sanity check
76-
app.get("/", (_, res) => res.send(logoPage).end())
77-
78-
// Introduction request
79-
app.ws("/introduction/:userName", (ws, { params: { userName } }) => {
80-
this.log("received introduction request", userName)
81-
this.openIntroductionConnection(ws, userName)
82-
})
83-
84-
// Connection request
85-
app.ws(
86-
"/connection/:A/:B/:documentId",
87-
(ws, { params: { A, B, documentId } }) => {
88-
this.log("received connection request", A, B)
89-
this.openConnection({ socket: ws, A, B, documentId })
90-
}
91-
)
80+
this.app
81+
// Allow hitting this server from a browser as a sanity check
82+
.get("/", (_, res) => res.send(logoPage).end())
83+
84+
// Introduction request
85+
.ws("/introduction/:userName", (ws, { params: { userName } }) => {
86+
this.log("received introduction request", userName)
87+
this.openIntroductionConnection(ws, userName)
88+
this.sockets.add(ws)
89+
})
90+
91+
// Connection request
92+
.ws(
93+
"/connection/:A/:B/:documentId",
94+
(ws, { params: { A, B, documentId } }) => {
95+
this.log("received connection request", A, B)
96+
this.openConnection({ socket: ws, A, B, documentId })
97+
this.sockets.add(ws)
98+
}
99+
)
92100

93-
this.httpServer = app
94101
.listen(this.port, () => {
95102
if (!silent)
96103
console.log(`🐟 ⯁ Listening at http://localhost:${this.port}`)
97104
this.emit("ready")
98105
resolve()
99106
})
100-
.on("connection", socket => {
101-
// keep track of sockets for cleanup
102-
this.httpSockets.push(socket)
103-
})
107+
108+
.on("error", reject)
104109
})
105110
}
106111

107112
close() {
108-
this.log("attempting httpServer.close")
109-
this.httpSockets.forEach(socket => {
110-
socket.end()
111-
socket.destroy()
112-
})
113-
return this.httpServer?.close(() => {
114-
this.emit("close")
113+
this.sockets.forEach(socket => {
114+
socket.removeAllListeners()
115+
socket.close()
116+
socket.terminate()
115117
})
118+
return this.app.removeAllListeners()
116119
}
117120

118121
// DISCOVERY
@@ -123,7 +126,7 @@ export class Server extends EventEmitter<ServerEvents> {
123126
socket.on("message", this.handleIntroductionRequest(userName))
124127
socket.on("close", this.closeIntroductionConnection(userName))
125128

126-
this.emit("introductionConnection", userName)
129+
this.emit("introduction", userName)
127130
}
128131

129132
private handleIntroductionRequest =
@@ -268,7 +271,7 @@ const tryParse = <T>(s: Uint8Array): T | Error => {
268271

269272
const { version } = pkg
270273

271-
const { app } = expressWs(express())
274+
// const { app } = expressWs(express())
272275

273276
const logoPage = `
274277
<body style="background:black; display:flex; justify-content:center; align-items:center">

0 commit comments

Comments
 (0)