import r from "race-events";
import WebSocket from "ws";
type WSMessage = { target: WebSocket; type: "message"; data: [WebSocket.Data] };
type WSClose = { target: WebSocket; type: "close"; data: [number, string] };
type WSError = { target: WebSocket; type: "error"; data: [Error] };
const wss = new WebSocket.Server();
wss.on("connection", async function(ws) {
try {
let a = await r<WSMessage>([ws, ["message"], ["close", "error"]]);
console.log(a.type, a.target, a.data);
let b = await r<WSMessage | WSClose | WSError>([ws, ["message", "close", "error"]]);
} catch (e) {
console.error("threw", e);
}
});