Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"client-src/**/*",
"client/**/*.js",
"dist/**/*",
"tests/fixtures/**/*",
"tests/**/*",
"scripts/**/*",
],
},
}
41 changes: 41 additions & 0 deletions client-src/clients/SockJSClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* The following code is modified based on
* https://github.com/webpack/webpack-dev-server
*
* MIT Licensed
* Author Tobias Koppers @sokra
* Copyright (c) JS Foundation and other contributors
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
*/

import SockJS from '../modules/sockjs-client/index.js';
import { CommunicationClient } from '../type.js';
import { log } from '../utils/log.js';

export default class SockJSClient implements CommunicationClient {
sock: WebSocket;
constructor(url: string) {
// SockJS requires `http` and `https` protocols
this.sock = new SockJS(
url.replace(/^ws:/i, 'http:').replace(/^wss:/i, 'https:'),
);
this.sock.onerror = (error) => {
log.error(error);
};
}

onOpen(fn: (...args: unknown[]) => void) {
this.sock.onopen = fn;
}

onClose(fn: (...args: unknown[]) => void) {
this.sock.onclose = fn;
}

// call f with the message string as the first argument
onMessage(fn: (...args: unknown[]) => void) {
this.sock.onmessage = (err) => {
fn(err.data);
};
}
}
38 changes: 38 additions & 0 deletions client-src/clients/WebSocketClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* The following code is modified based on
* https://github.com/webpack/webpack-dev-server
*
* MIT Licensed
* Author Tobias Koppers @sokra
* Copyright (c) JS Foundation and other contributors
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
*/

import { CommunicationClient } from '../type.js';
import { log } from '../utils/log.js';

export default class WebSocketClient implements CommunicationClient {
private client: WebSocket;

constructor(url: string) {
this.client = new WebSocket(url);
this.client.onerror = (error: Event) => {
log.error(error);
};
}

onOpen(fn: (...args: unknown[]) => void): void {
this.client.onopen = fn;
}

onClose(fn: (...args: unknown[]) => void): void {
this.client.onclose = fn;
}

// call fn with the message string as the first argument
onMessage(fn: (...args: unknown[]) => void): void {
this.client.onmessage = (event: MessageEvent<string>) => {
fn(event.data);
};
}
}
Loading
Loading