Refactor smartsocket implementation for improved WebSocket handling and message protocol
- Updated test files to use new testing library and reduced test cycles for efficiency. - Removed dependency on smartexpress and integrated direct WebSocket handling. - Enhanced Smartsocket and SmartsocketClient classes to support new message types and authentication flow. - Implemented a new message interface for structured communication between client and server. - Added external server support for smartserve with appropriate WebSocket hooks. - Improved connection management and error handling in SocketConnection and SocketRequest classes. - Cleaned up code and removed deprecated socket.io references in favor of native WebSocket.
This commit is contained in:
@@ -4,20 +4,43 @@ import type * as https from 'https';
|
||||
|
||||
export type { http, https };
|
||||
|
||||
// pushrocks scope
|
||||
import type * as typedserver from '@api.global/typedserver';
|
||||
// third party scope - ws types
|
||||
import type * as wsTypes from 'ws';
|
||||
|
||||
export type { typedserver };
|
||||
|
||||
// third party scope
|
||||
import type { Socket as ServerSocket, Server as ServerServer } from 'socket.io';
|
||||
import type { Socket as ClientSocket, connect as ClientIo } from 'socket.io-client';
|
||||
|
||||
export namespace socketIo {
|
||||
export type Socket = ServerSocket;
|
||||
export type Server = ServerServer;
|
||||
export namespace ws {
|
||||
export type WebSocket = wsTypes.WebSocket;
|
||||
export type WebSocketServer = wsTypes.WebSocketServer;
|
||||
export type RawData = wsTypes.RawData;
|
||||
}
|
||||
export namespace socketIoClient {
|
||||
export type Socket = ClientSocket;
|
||||
export type connect = typeof ClientIo;
|
||||
|
||||
// smartserve compatibility interface (for setExternalServer)
|
||||
// This mirrors the IWebSocketPeer interface from smartserve
|
||||
export interface ISmartserveWebSocketPeer {
|
||||
id: string;
|
||||
url: string;
|
||||
readyState: 0 | 1 | 2 | 3;
|
||||
protocol: string;
|
||||
extensions: string;
|
||||
send(data: string): void;
|
||||
sendBinary(data: Uint8Array | ArrayBuffer): void;
|
||||
close(code?: number, reason?: string): void;
|
||||
ping(data?: Uint8Array): void;
|
||||
terminate(): void;
|
||||
context: any;
|
||||
data: Map<string, unknown>;
|
||||
tags: Set<string>;
|
||||
}
|
||||
|
||||
export interface ISmartserveWebSocketMessage {
|
||||
type: 'text' | 'binary';
|
||||
text?: string;
|
||||
data?: Uint8Array;
|
||||
size: number;
|
||||
}
|
||||
|
||||
export interface ISmartserveWebSocketHooks {
|
||||
onOpen?: (peer: ISmartserveWebSocketPeer) => void | Promise<void>;
|
||||
onMessage?: (peer: ISmartserveWebSocketPeer, message: ISmartserveWebSocketMessage) => void | Promise<void>;
|
||||
onClose?: (peer: ISmartserveWebSocketPeer, code: number, reason: string) => void | Promise<void>;
|
||||
onError?: (peer: ISmartserveWebSocketPeer, error: Error) => void | Promise<void>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user