Skip to content

Commit

Permalink
Remove check for known subprotocol
Browse files Browse the repository at this point in the history
  • Loading branch information
bryfox committed Jan 30, 2025
1 parent 9cce3a0 commit 75e165b
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions typescript/ws-protocol/src/FoxgloveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ type EventTypes = {

const textEncoder = new TextEncoder();

/**
* A client to make it easier to interact with the Foxglove WebSocket protocol:
* https://github.com/foxglove/ws-protocol/blob/main/docs/spec.md.
*
* You must provide the underlying websocket client (an implementation of `IWebSocket`) and that
* client must advertise a subprotocol which is compatible with the ws-protocol spec (e.g.
* "foxglove.websocket.v1").
*/
export default class FoxgloveClient {
static SUPPORTED_SUBPROTOCOL = "foxglove.websocket.v1";

#emitter = new EventEmitter<EventTypes>();
#ws: IWebSocket;
#nextSubscriptionId = 0;
Expand All @@ -62,12 +68,6 @@ export default class FoxgloveClient {
this.#reconnect();
}

/**
* A list subprotocols supported by this client. Typically, this will agree with the subprotocols
* advertised by the provided `IWebSocket` implementation.
*/
protected supportedSubprotocols: string[] = [FoxgloveClient.SUPPORTED_SUBPROTOCOL];

on<E extends EventEmitter.EventNames<EventTypes>>(
name: E,
listener: EventEmitter.EventListener<EventTypes, E>,
Expand All @@ -87,13 +87,6 @@ export default class FoxgloveClient {
this.#emitter.emit("error", event.error ?? new Error("WebSocket error"));
};
this.#ws.onopen = (_event) => {
if (!this.supportedSubprotocols.includes(this.#ws.protocol)) {
const protocolDesc =
this.supportedSubprotocols.length === 1
? `subprotocol '${this.supportedSubprotocols[0]}'`
: `a subprotocol from ['${this.supportedSubprotocols.join("', '")}']`;
throw new Error(`Expected ${protocolDesc}; got '${this.#ws.protocol}'`);
}
this.#emitter.emit("open");
};
this.#ws.onmessage = (event: MessageEvent<ArrayBuffer | string>) => {
Expand Down

0 comments on commit 75e165b

Please sign in to comment.