Skip to content

Commit ae59757

Browse files
committed
Support connections from subpaths
1 parent f262c79 commit ae59757

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/open-collaboration-protocol/src/transport/socket-io-transport.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ import { io, Socket } from 'socket.io-client';
1212
export const SocketIoTransportProvider: MessageTransportProvider = {
1313
id: 'socket.io',
1414
createTransport: (url, headers) => {
15+
const parsedUrl = new URL(url);
16+
let path = parsedUrl.pathname;
17+
if (path && !path.endsWith('/')) {
18+
path += '/';
19+
}
20+
// Path always ends in .../socket.io
21+
path += 'socket.io';
1522
const socket = io(url, {
23+
path,
1624
extraHeaders: headers
1725
});
1826
const transport = new SocketIoTransport(socket);
@@ -44,6 +52,7 @@ export class SocketIoTransport implements MessageTransport {
4452

4553
constructor(protected socket: Socket) {
4654
this.socket.on('disconnect', (_reason, _description) => {
55+
this.ready.reject();
4756
this.ready = new Deferred();
4857
// Give it 30 seconds to reconnect before firing the disconnect event
4958
this.disconnectTimeout = setTimeout(() => {
@@ -59,8 +68,19 @@ export class SocketIoTransport implements MessageTransport {
5968
}
6069
this.onReconnectEmitter.fire();
6170
});
62-
this.socket.on('error', () => this.onErrorEmitter.fire('Websocket connection closed unexpectedly.'));
63-
this.socket.on('connect', () => this.ready.resolve());
71+
const timeout = setTimeout(() => {
72+
this.onErrorEmitter.fire('Websocket connection timed out.');
73+
this.ready.reject();
74+
}, 30_000);
75+
this.socket.on('error', () => {
76+
this.onErrorEmitter.fire('Websocket connection closed unexpectedly.');
77+
this.ready.reject();
78+
clearTimeout(timeout);
79+
});
80+
this.socket.on('connect', () => {
81+
this.ready.resolve();
82+
clearTimeout(timeout);
83+
});
6484
}
6585

6686
async write(data: Uint8Array): Promise<void> {

packages/open-collaboration-server/src/collaboration-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export class CollaborationServer {
9494
methods: ['GET', 'POST']
9595
}
9696
});
97-
io.on('connection', async socket => {
97+
// Handle all namespaces (not just the default one)
98+
// This ensures that users accessing the server via a path still get connected
99+
io.of(/.*/).on('connection', async socket => {
98100
const headers = socket.request.headers as Record<string, string>;
99101
try {
100102
await this.connectChannel(headers, new SocketIoChannel(socket));

0 commit comments

Comments
 (0)