Add server runtime and hello handshake
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
:
|
||||
import { WebSocketServer, WebSocket, RawData } from "ws";
|
||||
import type { YonexusServerConfig } from "./config.js";
|
||||
import type { ClientRecord } from "./persistence.js";
|
||||
@@ -16,11 +15,14 @@ export interface ServerTransport {
|
||||
start(): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
send(identifier: string, message: string): boolean;
|
||||
sendToConnection(connection: ClientConnection, message: string): boolean;
|
||||
broadcast(message: string): void;
|
||||
closeConnection(identifier: string, code?: number, reason?: string): boolean;
|
||||
registerConnection(identifier: string, ws: WebSocket): boolean;
|
||||
markAuthenticated(identifier: string): boolean;
|
||||
}
|
||||
|
||||
export type MessageHandler = (identifier: string | null, message: string) => void;
|
||||
export type MessageHandler = (connection: ClientConnection, message: string) => void;
|
||||
export type ConnectionHandler = (identifier: string | null, ws: WebSocket) => void;
|
||||
export type DisconnectionHandler = (identifier: string | null, code: number, reason: Buffer) => void;
|
||||
|
||||
@@ -107,12 +109,17 @@ export class YonexusServerTransport implements ServerTransport {
|
||||
|
||||
send(identifier: string, message: string): boolean {
|
||||
const conn = this._connections.get(identifier);
|
||||
if (!conn || !conn.isAuthenticated) {
|
||||
if (!conn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conn.ws.readyState === WebSocket.OPEN) {
|
||||
conn.ws.send(message);
|
||||
|
||||
return this.sendToConnection(conn, message);
|
||||
}
|
||||
|
||||
sendToConnection(connection: ClientConnection, message: string): boolean {
|
||||
const { ws } = connection;
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(message);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -120,8 +127,8 @@ export class YonexusServerTransport implements ServerTransport {
|
||||
|
||||
broadcast(message: string): void {
|
||||
for (const conn of this._connections.values()) {
|
||||
if (conn.isAuthenticated && conn.ws.readyState === WebSocket.OPEN) {
|
||||
conn.ws.send(message);
|
||||
if (conn.isAuthenticated) {
|
||||
this.sendToConnection(conn, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,7 +138,7 @@ export class YonexusServerTransport implements ServerTransport {
|
||||
if (!conn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
conn.ws.close(code, reason);
|
||||
this._connections.delete(identifier);
|
||||
return true;
|
||||
@@ -188,12 +195,14 @@ export class YonexusServerTransport implements ServerTransport {
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.options.onMessage(identifier, message);
|
||||
|
||||
const connection = identifier ? this._connections.get(identifier) ?? tempConn : tempConn;
|
||||
this.options.onMessage(connection, message);
|
||||
});
|
||||
|
||||
ws.on("close", (code: number, reason: Buffer) => {
|
||||
this.tempConnections.delete(ws);
|
||||
|
||||
|
||||
// Find and remove from registered connections
|
||||
for (const [id, conn] of this._connections) {
|
||||
if (conn.ws === ws) {
|
||||
@@ -204,7 +213,7 @@ export class YonexusServerTransport implements ServerTransport {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (this.options.onDisconnect) {
|
||||
this.options.onDisconnect(null, code, reason);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user