Skip to content

Commit

Permalink
core: fix socket.io endpoint path
Browse files Browse the repository at this point in the history
Socket.io will try to connect to `domain.com/socket.io` to establish
communication by default. This is problematic when the frontend is
served on an HTTP path that isn't `/`.

Update the path supplied to Socket.io to take the current location into
account.
  • Loading branch information
paul-marechal committed Mar 8, 2022
1 parent 40a7eb2 commit 047b25b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/core/src/browser/messaging/ws-connection-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// *****************************************************************************

import { injectable, interfaces, decorate, unmanaged } from 'inversify';
import { JsonRpcProxyFactory, JsonRpcProxy, Emitter, Event } from '../../common';
import { JsonRpcProxyFactory, JsonRpcProxy, Emitter, Event, Path } from '../../common';
import { WebSocketChannel } from '../../common/messaging/web-socket-channel';
import { Endpoint } from '../endpoint';
import { AbstractConnectionProvider } from '../../common/messaging/abstract-connection-provider';
Expand Down Expand Up @@ -91,11 +91,10 @@ export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebS
}

/**
* Creates a websocket URL to the current location
* @param path The handler to reach in the backend.
*/
protected createWebSocketUrl(path: string): string {
const endpoint = new Endpoint({ path });
return endpoint.getWebSocketUrl().toString();
return path;
}

protected createHttpWebSocketUrl(path: string): string {
Expand All @@ -108,6 +107,7 @@ export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebS
*/
protected createWebSocket(url: string): Socket {
return io(url, {
path: this.createSocketIoPath(url),
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax: 10000,
Expand All @@ -120,6 +120,10 @@ export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebS
});
}

protected createSocketIoPath(url: string): string | undefined {
return new Path(location.pathname).join('socket.io').toString();
}

protected fireSocketDidOpen(): void {
this.onSocketDidOpenEmitter.fire(undefined);
}
Expand Down

0 comments on commit 047b25b

Please sign in to comment.