fix: accept /collab WebSocket upgrade without trailing slash
Browse filesThe Hocuspocus provider connects to /collab (no trailing slash),
but the upgrade handler only matched /collab/. Also add debug
logging for incoming WS messages and proper error handling.
Made-with: Cursor
- backend/src/server.ts +13 -1
backend/src/server.ts
CHANGED
|
@@ -150,12 +150,24 @@ app.get("/api/auth/status", async (req, res) => {
|
|
| 150 |
const wss = new WebSocketServer({ noServer: true });
|
| 151 |
|
| 152 |
httpServer.on("upgrade", (req, socket, head) => {
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
| 154 |
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
const token = extractToken(req.headers.cookie);
|
| 156 |
hocuspocus.handleConnection(ws, req, { token });
|
| 157 |
});
|
| 158 |
} else {
|
|
|
|
| 159 |
socket.destroy();
|
| 160 |
}
|
| 161 |
});
|
|
|
|
| 150 |
const wss = new WebSocketServer({ noServer: true });
|
| 151 |
|
| 152 |
httpServer.on("upgrade", (req, socket, head) => {
|
| 153 |
+
const url = req.url || "";
|
| 154 |
+
// Hocuspocus v3 provider connects to /collab (no document name in path)
|
| 155 |
+
if (url === "/collab" || url.startsWith("/collab/") || url.startsWith("/collab?")) {
|
| 156 |
+
console.log(`[ws] upgrade request for ${url}`);
|
| 157 |
wss.handleUpgrade(req, socket, head, (ws) => {
|
| 158 |
+
ws.setMaxListeners(Infinity);
|
| 159 |
+
ws.on("error", (error) => {
|
| 160 |
+
console.error("[ws] socket error:", error.message);
|
| 161 |
+
});
|
| 162 |
+
ws.on("message", (data: Buffer, isBinary: boolean) => {
|
| 163 |
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data as any);
|
| 164 |
+
console.log(`[ws-debug] msg ${buf.length}B binary=${isBinary} first20=${buf.slice(0, 20).toString("hex")}`);
|
| 165 |
+
});
|
| 166 |
const token = extractToken(req.headers.cookie);
|
| 167 |
hocuspocus.handleConnection(ws, req, { token });
|
| 168 |
});
|
| 169 |
} else {
|
| 170 |
+
console.log(`[ws] rejected upgrade for ${url}`);
|
| 171 |
socket.destroy();
|
| 172 |
}
|
| 173 |
});
|