-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.d.ts
More file actions
96 lines (77 loc) · 2.55 KB
/
index.d.ts
File metadata and controls
96 lines (77 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Socket Base Client TypeScript Definitions
*/
export interface SocketBaseClientConfig {
/** WebSocket server URL (default: 'wss://api.socket-base.com') */
url?: string;
/** Project hash (required) */
hash: string;
/** JWT token (required) */
jwt: string;
/** Enable auto-reconnect (default: true) */
autoReconnect?: boolean;
/** Auto-connect on client creation (default: true) */
autoConnect?: boolean;
/** Maximum reconnect attempts (default: 5) */
maxReconnectAttempts?: number;
/** Reconnect delay in milliseconds (default: 1000) */
reconnectDelay?: number;
}
export interface JoinResult {
success: boolean;
room: string;
}
export interface LeaveResult {
success: boolean;
room: string;
}
export interface MessageEvent {
room: string;
payload: any;
from?: string;
timestamp?: number;
}
export interface BroadcastEvent {
payload: any;
from?: string;
}
export interface ErrorEvent {
type: string;
message?: string;
error?: Error;
raw?: string;
}
export type EventType = 'open' | 'close' | 'error' | 'message' | 'joined' | 'left' | 'broadcast';
export type EventCallback<T = any> = (data: T) => void;
export type UnsubscribeFunction = () => void;
export class SocketBaseClient {
constructor(config: SocketBaseClientConfig);
/** Connect to the WebSocket server */
connect(): Promise<void>;
/** Join a room */
join(room: string): Promise<JoinResult>;
/** Leave a room */
leave(room: string): Promise<LeaveResult>;
/** Send a message to a room */
send(room: string, payload: any): void;
/** Broadcast a message to all clients */
broadcast(payload: any): void;
/** Subscribe to events */
on(event: 'open', callback: EventCallback<Event>): UnsubscribeFunction;
on(event: 'close', callback: EventCallback<CloseEvent>): UnsubscribeFunction;
on(event: 'error', callback: EventCallback<ErrorEvent>): UnsubscribeFunction;
on(event: 'message', callback: EventCallback<MessageEvent>): UnsubscribeFunction;
on(event: 'joined', callback: EventCallback<{ room: string }>): UnsubscribeFunction;
on(event: 'left', callback: EventCallback<{ room: string }>): UnsubscribeFunction;
on(event: 'broadcast', callback: EventCallback<BroadcastEvent>): UnsubscribeFunction;
on(event: EventType, callback: EventCallback): UnsubscribeFunction;
/** Close the WebSocket connection */
close(): void;
/** Get connection status */
getConnectionStatus(): boolean;
}
/**
* Create a Socket Base client
*/
export function createClient(config: SocketBaseClientConfig): SocketBaseClient;
export default { createClient };