Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 9 additions & 42 deletions client-src/clients/WebSocketClient.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
import { log } from "../utils/log.js";

/** @typedef {import("../index.js").EXPECTED_ANY} EXPECTED_ANY */

/**
* @implements {CommunicationClient}
*/
export default class WebSocketClient {
/**
* @param {string} url url to connect
*/
constructor(url) {
this.client = new WebSocket(url);
this.client.onerror = (error) => {
log.error(error);
};
}

/**
* @param {(...args: EXPECTED_ANY[]) => void} fn function
*/
onOpen(fn) {
this.client.onopen = fn;
}

/**
* @param {(...args: EXPECTED_ANY[]) => void} fn function
*/
onClose(fn) {
this.client.onclose = fn;
}

// call f with the message string as the first argument
/**
* @param {(...args: EXPECTED_ANY[]) => void} fn function
*/
onMessage(fn) {
this.client.onmessage = (err) => {
fn(err.data);
};
}
}
// Re-exported rather than implemented: webpack-dev-middleware ships the same
// transport, and one copy means one place for the things this one was missing —
// a `close()` (which the interface declares and this file never had), the guard
// that stops a queued event reporting after the caller closed, and resolving a
// relative or `http(s):` url for browsers whose `WebSocket` will not.
//
// `client.webSocketTransport` resolves to this path, so anything pointing at it
// keeps working.
export { default } from "webpack-dev-middleware/client/ws";
Loading
Loading