@eleven-am/pondsocket 0.1.141 → 0.1.143
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -15
- package/abstracts/abstractRequest.js +1 -2
- package/abstracts/abstractRequest.test.js +2 -5
- package/channel/channel.js +118 -97
- package/channel/channel.test.js +86 -61
- package/channel/eventRequest.test.js +3 -3
- package/channel/eventResponse.js +37 -61
- package/channel/eventResponse.test.js +31 -53
- package/endpoint/endpoint.js +182 -100
- package/endpoint/endpoint.test.js +30 -13
- package/endpoint/response.js +48 -14
- package/lobby/JoinRequest.test.js +17 -6
- package/lobby/JoinResponse.test.js +18 -23
- package/lobby/joinResponse.js +70 -31
- package/lobby/lobby.js +18 -53
- package/package.json +8 -8
- package/presence/presence.js +1 -1
- package/presence/presenceEngine.test.js +1 -4
- package/schema.js +0 -1
- package/server/pondSocket.js +8 -5
- package/types.d.ts +296 -227
- package/abstracts/abstractResponse.js +0 -9
package/types.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { Server as HTTPServer } from 'http';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
IncomingConnection,
|
|
5
|
+
JoinParams,
|
|
6
6
|
PondAssigns,
|
|
7
7
|
PondEvent,
|
|
8
|
-
PondPath,
|
|
9
|
-
JoinParams,
|
|
10
8
|
PondEventMap,
|
|
11
|
-
|
|
9
|
+
PondMessage,
|
|
10
|
+
PondPath,
|
|
11
|
+
PondPresence,
|
|
12
|
+
UserData,
|
|
12
13
|
} from '@eleven-am/pondsocket-common';
|
|
13
|
-
import { WebSocketServer } from 'ws';
|
|
14
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
14
15
|
|
|
15
16
|
export * from '@eleven-am/pondsocket-common';
|
|
16
17
|
|
|
@@ -21,143 +22,126 @@ export interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, Pres
|
|
|
21
22
|
|
|
22
23
|
export type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
|
|
23
24
|
|
|
24
|
-
export
|
|
25
|
-
event: PondEvent<Path>;
|
|
26
|
-
|
|
27
|
-
channelName: string;
|
|
28
|
-
|
|
29
|
-
assigns: Record<string, AssignType>;
|
|
30
|
-
|
|
31
|
-
presence: Record<string, PresenceType>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
|
|
35
|
-
user: UserData<PresenceType, AssignType>;
|
|
36
|
-
|
|
37
|
-
channel: Channel;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
41
|
-
/**
|
|
42
|
-
* @desc Whether the server has responded to the request
|
|
43
|
-
*/
|
|
44
|
-
hasResponded: boolean;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @desc Accepts the request and optionally assigns data to the client
|
|
48
|
-
* @param assigns - the data to assign to the client
|
|
49
|
-
*/
|
|
50
|
-
accept (assigns?: AssignType): EventResponse;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @desc Rejects the request and optionally assigns data to the client
|
|
54
|
-
* @param message - the error message
|
|
55
|
-
* @param errorCode - the error code
|
|
56
|
-
* @param assigns - the data to assign to the client
|
|
57
|
-
*/
|
|
58
|
-
reject (message?: string, errorCode?: number, assigns?: AssignType): EventResponse;
|
|
25
|
+
export type RequestHandler<Request, Response> = (request: Request, response: Response) => void | Promise<void>;
|
|
59
26
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* @param event - the event name
|
|
63
|
-
* @param payload - the payload to send
|
|
64
|
-
* @param assigns - the data to assign to the client
|
|
65
|
-
*/
|
|
66
|
-
send (event: string, payload: PondMessage, assigns?: AssignType): void;
|
|
27
|
+
export declare class PondSocket {
|
|
28
|
+
constructor(server?: HTTPServer, socketServer?: WebSocketServer);
|
|
67
29
|
|
|
68
30
|
/**
|
|
69
|
-
* @desc
|
|
70
|
-
* @param
|
|
71
|
-
* @param payload - the payload to send
|
|
72
|
-
* @param assigns - the data to assign to the client
|
|
31
|
+
* @desc Specifies the port to listen on
|
|
32
|
+
* @param args - the arguments to pass to the server
|
|
73
33
|
*/
|
|
74
|
-
|
|
34
|
+
listen(...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
|
|
75
35
|
|
|
76
36
|
/**
|
|
77
|
-
* @desc
|
|
78
|
-
* @param
|
|
79
|
-
* @param payload - the payload to send
|
|
37
|
+
* @desc Closes the server
|
|
38
|
+
* @param callback - the callback to call when the server is closed
|
|
80
39
|
*/
|
|
81
|
-
|
|
40
|
+
close(callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
|
|
82
41
|
|
|
83
42
|
/**
|
|
84
|
-
* @desc
|
|
85
|
-
* @param
|
|
86
|
-
* @param
|
|
43
|
+
* @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
|
|
44
|
+
* @param path - the pattern to accept || can also be a regex
|
|
45
|
+
* @param handler - the handler function to authenticate the socket
|
|
46
|
+
* @example
|
|
47
|
+
* const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
48
|
+
* const token = req.query.token;
|
|
49
|
+
* if (!token)
|
|
50
|
+
* return res.reject('No token provided');
|
|
51
|
+
* res.accept({
|
|
52
|
+
* assign: {
|
|
53
|
+
* token
|
|
54
|
+
* }
|
|
55
|
+
* });
|
|
56
|
+
* })
|
|
87
57
|
*/
|
|
88
|
-
|
|
58
|
+
createEndpoint<Path extends string>(path: PondPath<Path>, handler: RequestHandler<IncomingConnection<Path>, ConnectionResponse>): Endpoint;
|
|
59
|
+
}
|
|
89
60
|
|
|
61
|
+
export declare class Endpoint {
|
|
90
62
|
/**
|
|
91
|
-
* @desc
|
|
92
|
-
* @param
|
|
93
|
-
* @param
|
|
94
|
-
* @
|
|
63
|
+
* @desc Adds a new PondChannel to this path on this endpoint
|
|
64
|
+
* @param {PondPath<string>} path - The path to add the channel to
|
|
65
|
+
* @param {RequestHandler} handler - The handler to use to authenticate the client
|
|
66
|
+
* @returns {PondChannel} - The PondChannel instance for the new channel
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* const channel = endpoint.createChannel('/chat', (request, response) => {
|
|
70
|
+
* if (request.user.assigns.admin)
|
|
71
|
+
* response.accept();
|
|
72
|
+
*
|
|
73
|
+
* else
|
|
74
|
+
* response.reject('You are not an admin', 403);
|
|
75
|
+
* });
|
|
95
76
|
*/
|
|
96
|
-
|
|
77
|
+
createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>(path: PondPath<Path>, handler: RequestHandler<JoinRequest<Path, PresenceType, AssignType>, JoinResponse<EventType, PresenceType, AssignType>>): PondChannel<EventType, PresenceType, AssignType>;
|
|
97
78
|
|
|
98
79
|
/**
|
|
99
|
-
* @desc
|
|
100
|
-
* @param
|
|
101
|
-
* @param
|
|
80
|
+
* @desc Broadcasts a message to all clients connected to this endpoint
|
|
81
|
+
* @param {string} event - The event to broadcast.
|
|
82
|
+
* @param {PondMessage} payload - The payload to broadcast.
|
|
83
|
+
* @returns {void}
|
|
102
84
|
*/
|
|
103
|
-
|
|
85
|
+
broadcast(event: string, payload: PondMessage): void;
|
|
104
86
|
|
|
105
87
|
/**
|
|
106
|
-
* @desc
|
|
107
|
-
* @param
|
|
108
|
-
* @param userId - the id of the user to update
|
|
88
|
+
* @desc Closes specific clients connected to this endpoint
|
|
89
|
+
* @param {string | string[]} clientIds - The ids of the clients to close.
|
|
109
90
|
*/
|
|
110
|
-
|
|
91
|
+
closeConnection(clientIds: string | string[]): void;
|
|
111
92
|
|
|
112
93
|
/**
|
|
113
|
-
* @desc
|
|
114
|
-
* @param userId - the id of the user to remove
|
|
94
|
+
* @desc Returns all the clients connected to this endpoint
|
|
115
95
|
*/
|
|
116
|
-
|
|
96
|
+
getClients(): WebSocket[];
|
|
97
|
+
}
|
|
117
98
|
|
|
99
|
+
export declare class PondChannel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
118
100
|
/**
|
|
119
|
-
* @desc
|
|
120
|
-
* @param
|
|
121
|
-
* @param
|
|
101
|
+
* @desc Handles an event request made by a user
|
|
102
|
+
* @param {PondPath<string>} event - The event to handle
|
|
103
|
+
* @param {RequestHandler} handler - The handlers to execute when the event is received
|
|
104
|
+
* @example
|
|
105
|
+
* pond.onEvent('echo', (request, response) => {
|
|
106
|
+
* response.reply('echo', {
|
|
107
|
+
* message: request.event.payload,
|
|
108
|
+
* });
|
|
109
|
+
* });
|
|
122
110
|
*/
|
|
123
|
-
|
|
111
|
+
onEvent<Event extends string>(event: PondPath<Event>, handler: RequestHandler<EventRequest<Event, PresenceType, AssignType>, EventResponse<EventType, PresenceType, AssignType>>): void;
|
|
124
112
|
|
|
125
113
|
/**
|
|
126
|
-
* @desc
|
|
127
|
-
* @param
|
|
114
|
+
* @desc Broadcasts a message to all users in a channel
|
|
115
|
+
* @param event - The event to broadcast
|
|
116
|
+
* @param payload - The payload to send
|
|
117
|
+
* @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
|
|
118
|
+
* @example
|
|
119
|
+
* pond.broadcast('echo', {
|
|
120
|
+
* message: 'Hello World',
|
|
121
|
+
* timestamp: Date.now(),
|
|
122
|
+
* channel: 'my_channel',
|
|
123
|
+
*});
|
|
128
124
|
*/
|
|
129
|
-
|
|
130
|
-
}
|
|
125
|
+
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key], channelName?: string): void;
|
|
131
126
|
|
|
132
|
-
export declare class Endpoint {
|
|
133
127
|
/**
|
|
134
|
-
* @desc
|
|
135
|
-
* @param
|
|
136
|
-
* @param handler - The handler to use to authenticate the client
|
|
137
|
-
*
|
|
128
|
+
* @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
|
|
129
|
+
* @param {LeaveCallback} callback - The callback to execute when a user leaves
|
|
138
130
|
* @example
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* response.accept();
|
|
142
|
-
*
|
|
143
|
-
* else
|
|
144
|
-
* response.reject('You are not an admin', 403);
|
|
131
|
+
* pond.onLeave((event) => {
|
|
132
|
+
* // Clean up resources
|
|
145
133
|
* });
|
|
146
134
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* @desc Broadcasts a message to all clients connected to this endpoint
|
|
151
|
-
* @param event - The event to broadcast
|
|
152
|
-
* @param payload - The payload to broadcast
|
|
153
|
-
*/
|
|
154
|
-
broadcast (event: string, payload: PondMessage): void;
|
|
135
|
+
onLeave(callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
|
|
155
136
|
|
|
156
137
|
/**
|
|
157
|
-
* @desc
|
|
158
|
-
* @param
|
|
138
|
+
* @desc Gets a channel by name
|
|
139
|
+
* @param {string} channelName - The name of the channel to get
|
|
140
|
+
* @returns {Channel} - The channel instance
|
|
141
|
+
* @example
|
|
142
|
+
* const channel = pond.getChannel('my_channel')!;
|
|
159
143
|
*/
|
|
160
|
-
|
|
144
|
+
getChannel(channelName: string): Channel<EventType, PresenceType, AssignType> | null;
|
|
161
145
|
}
|
|
162
146
|
|
|
163
147
|
export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
@@ -169,76 +153,92 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
|
|
|
169
153
|
/**
|
|
170
154
|
* @desc Gets the current assign data for the channel.
|
|
171
155
|
*/
|
|
172
|
-
getAssigns
|
|
156
|
+
getAssigns(): Record<string, AssignType>;
|
|
173
157
|
|
|
174
158
|
/**
|
|
175
159
|
* @desc Gets the current presence data for the channel.
|
|
176
160
|
*/
|
|
177
|
-
getPresences
|
|
161
|
+
getPresences(): Record<string, PresenceType>;
|
|
178
162
|
|
|
179
163
|
/**
|
|
180
164
|
* @desc Gets the assign date for a specific user.
|
|
181
|
-
* @param userId - The id of the user to get the
|
|
165
|
+
* @param {string} userId - The id of the user to get the data for.
|
|
182
166
|
*/
|
|
183
|
-
getUserData
|
|
167
|
+
getUserData(userId: string): AssignType | null;
|
|
184
168
|
|
|
185
169
|
/**
|
|
186
170
|
* @desc Broadcasts a message to every client in the channel,
|
|
187
|
-
* @param event - The event to send.
|
|
188
|
-
* @param payload - The message to send.
|
|
171
|
+
* @param {string} event - The event to send.
|
|
172
|
+
* @param {PondMessage} payload - The message to send.
|
|
189
173
|
*/
|
|
190
|
-
|
|
174
|
+
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): void;
|
|
191
175
|
|
|
192
176
|
/**
|
|
193
177
|
* @desc Broadcasts a message to every client in the channel except the sender,
|
|
194
|
-
* @param userId - The id of the user to
|
|
195
|
-
* @param event - The event to send.
|
|
196
|
-
* @param payload - The message to send.
|
|
178
|
+
* @param {string} userId - The id of the user to exclude from the broadcast.
|
|
179
|
+
* @param {string} event - The event to send.
|
|
180
|
+
* @param {PondMessage} payload - The message to send.
|
|
197
181
|
*/
|
|
198
|
-
|
|
182
|
+
broadcastFrom<Key extends keyof EventType>(userId: string, event: Key, payload: EventType[Key]): void;
|
|
199
183
|
|
|
200
184
|
/**
|
|
201
185
|
* @desc Sends a message to a specific client in the channel.
|
|
202
|
-
* @param
|
|
203
|
-
* @param event - The event to send.
|
|
204
|
-
* @param payload - The message to send.
|
|
186
|
+
* @param {string | string[]} clientIds - The id of the client to send the message to.
|
|
187
|
+
* @param {string} event - The event to send.
|
|
188
|
+
* @param {PondMessage} payload - The message to send.
|
|
205
189
|
*/
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* @desc Sends a message to specific clients in the channel.
|
|
210
|
-
* @param userIds - The ids of the users to send the message to.
|
|
211
|
-
* @param event - The event to send.
|
|
212
|
-
* @param payload - The message to send.
|
|
213
|
-
*/
|
|
214
|
-
sendToUsers <Key extends keyof EventType> (userIds: string[], event: Key, payload: EventType[Key]): void;
|
|
190
|
+
broadcastTo<Key extends keyof EventType>(clientIds: string | string[], event: Key, payload: EventType[Key]): void;
|
|
215
191
|
|
|
216
192
|
/**
|
|
217
193
|
* @desc Bans a user from the channel.
|
|
218
|
-
* @param userId - The id of the user to ban.
|
|
219
|
-
* @param reason - The reason for the ban.
|
|
194
|
+
* @param {string} userId - The id of the user to ban.
|
|
195
|
+
* @param {string} reason - The reason for the ban.
|
|
220
196
|
*/
|
|
221
|
-
evictUser
|
|
197
|
+
evictUser(userId: string, reason?: string): void;
|
|
222
198
|
|
|
223
199
|
/**
|
|
224
200
|
* @desc tracks a user's presence in the channel
|
|
225
|
-
* @param userId - the id of the user to track
|
|
226
|
-
* @param presence - the presence
|
|
201
|
+
* @param {string} userId - the id of the user to track
|
|
202
|
+
* @param {PondPresence} presence - the presence data to track
|
|
227
203
|
*/
|
|
228
|
-
trackPresence
|
|
204
|
+
trackPresence(userId: string, presence: PresenceType): void;
|
|
229
205
|
|
|
230
206
|
/**
|
|
231
207
|
* @desc removes a user's presence from the channel
|
|
232
|
-
* @param userId - the id of the user to remove
|
|
208
|
+
* @param {string} userId - the id of the user to remove
|
|
233
209
|
*/
|
|
234
|
-
removePresence
|
|
210
|
+
removePresence(userId: string): void;
|
|
235
211
|
|
|
236
212
|
/**
|
|
237
213
|
* @desc updates a user's presence in the channel
|
|
238
|
-
* @param userId - the id of the user to update
|
|
239
|
-
* @param presence - the
|
|
214
|
+
* @param {string} userId - the id of the user to update
|
|
215
|
+
* @param {PondPresence} presence - the presence data to update
|
|
216
|
+
*/
|
|
217
|
+
updatePresence(userId: string, presence: PresenceType): void;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @desc Subscribes a user to a channel.
|
|
221
|
+
* @param {string} userId - The id of the user.
|
|
222
|
+
* @param {string} channel - The channel to subscribe to.
|
|
240
223
|
*/
|
|
241
|
-
|
|
224
|
+
subscribeTo(userId: string, channel: string): void;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @desc Unsubscribes a user from a channel.
|
|
228
|
+
* @param {string} userId - The id of the user.
|
|
229
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
230
|
+
*/
|
|
231
|
+
unsubscribeFrom(userId: string, channel: string): void;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
|
|
235
|
+
event: PondEvent<Path>;
|
|
236
|
+
|
|
237
|
+
channelName: string;
|
|
238
|
+
|
|
239
|
+
assigns: Record<string, AssignType>;
|
|
240
|
+
|
|
241
|
+
presence: Record<string, PresenceType>;
|
|
242
242
|
}
|
|
243
243
|
|
|
244
244
|
export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
|
|
@@ -249,6 +249,59 @@ export declare class JoinRequest<Path extends string, PresenceType extends PondP
|
|
|
249
249
|
channel: Channel;
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
+
export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
|
|
253
|
+
user: UserData<PresenceType, AssignType>;
|
|
254
|
+
|
|
255
|
+
channel: Channel;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export declare class ConnectionResponse {
|
|
259
|
+
/**
|
|
260
|
+
* @desc Whether the server has responded to the request
|
|
261
|
+
*/
|
|
262
|
+
hasResponded: boolean;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @desc Assigns data to the user.
|
|
266
|
+
* @param {PondAssigns} assigns - The data to assign.
|
|
267
|
+
* @returns {ConnectionResponse} - The ConnectionResponse instance for chaining.
|
|
268
|
+
*/
|
|
269
|
+
assign(assigns: PondAssigns): ConnectionResponse;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* @desc Accepts the connection request to the endpoint.
|
|
273
|
+
*/
|
|
274
|
+
accept(): void;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @desc Rejects the request with the given error message
|
|
278
|
+
* @param {string} message - The error message
|
|
279
|
+
* @param {number} errorCode - The error code
|
|
280
|
+
*/
|
|
281
|
+
decline(message?: string, errorCode?: number): void;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @desc Emits a direct message to the client
|
|
285
|
+
* @param {string} event - The event name
|
|
286
|
+
* @param {PondMessage} payload - The payload to send
|
|
287
|
+
*/
|
|
288
|
+
reply(event: string, payload: PondMessage): ConnectionResponse;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* @desc Subscribes the client to a channel.
|
|
292
|
+
* @param {string} channel - The channel to subscribe to.
|
|
293
|
+
* @returns {ConnectionResponse} - The JoinResponse instance for chaining.
|
|
294
|
+
*/
|
|
295
|
+
subscribeTo(channel: string): ConnectionResponse;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* @desc Unsubscribes the client from a channel.
|
|
299
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
300
|
+
* @returns {ConnectionResponse} - The JoinResponse instance for chaining.
|
|
301
|
+
*/
|
|
302
|
+
unsubscribeFrom(channel: string): ConnectionResponse;
|
|
303
|
+
}
|
|
304
|
+
|
|
252
305
|
export declare class JoinResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
253
306
|
/**
|
|
254
307
|
* @desc Whether the server has responded to the request
|
|
@@ -256,154 +309,170 @@ export declare class JoinResponse<EventType extends PondEventMap = PondEventMap,
|
|
|
256
309
|
hasResponded: boolean;
|
|
257
310
|
|
|
258
311
|
/**
|
|
259
|
-
* @desc
|
|
260
|
-
* @param assigns -
|
|
312
|
+
* @desc Assigns data to the client
|
|
313
|
+
* @param {PondAssigns} assigns - The data to assign
|
|
314
|
+
*/
|
|
315
|
+
assign(assigns: AssignType): JoinResponse;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* @desc Accepts the join request to the channel.
|
|
319
|
+
* @returns {JoinResponse} - The JoinResponse instance for chaining.
|
|
261
320
|
*/
|
|
262
|
-
accept
|
|
321
|
+
accept(): JoinResponse;
|
|
263
322
|
|
|
264
323
|
/**
|
|
265
324
|
* @desc Rejects the request and optionally assigns data to the client
|
|
266
|
-
* @param message -
|
|
267
|
-
* @param errorCode -
|
|
325
|
+
* @param {string} message - The error message
|
|
326
|
+
* @param {number} errorCode - The error code
|
|
268
327
|
*/
|
|
269
|
-
|
|
328
|
+
decline(message?: string, errorCode?: number): JoinResponse;
|
|
270
329
|
|
|
271
330
|
/**
|
|
272
331
|
* @desc Emits a direct message to the client
|
|
273
|
-
* @param event -
|
|
274
|
-
* @param payload -
|
|
275
|
-
* @param assigns -
|
|
332
|
+
* @param {string} event - The event name
|
|
333
|
+
* @param {PondMessage} payload - The payload to send
|
|
334
|
+
* @param {PondAssigns} assigns - The data to assign to the client
|
|
276
335
|
*/
|
|
277
|
-
|
|
336
|
+
reply<Key extends keyof EventType>(event: Key, payload: EventType[Key], assigns?: AssignType): JoinResponse;
|
|
278
337
|
|
|
279
338
|
/**
|
|
280
339
|
* @desc Emits a message to all clients in the channel
|
|
281
|
-
* @param event -
|
|
282
|
-
* @param payload -
|
|
340
|
+
* @param {string} event - The event name
|
|
341
|
+
* @param {PondMessage} payload - The payload to send
|
|
283
342
|
*/
|
|
284
|
-
broadcast
|
|
343
|
+
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinResponse;
|
|
285
344
|
|
|
286
345
|
/**
|
|
287
346
|
* @desc Emits a message to all clients in the channel except the sender
|
|
288
347
|
* @param event - the event name
|
|
289
348
|
* @param payload - the payload to send
|
|
290
349
|
*/
|
|
291
|
-
|
|
350
|
+
broadcastFrom<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinResponse;
|
|
292
351
|
|
|
293
352
|
/**
|
|
294
353
|
* @desc Emits a message to a specific set of clients
|
|
295
|
-
* @param event -
|
|
296
|
-
* @param payload
|
|
297
|
-
* @param userIds -
|
|
354
|
+
* @param {string} event - The event name
|
|
355
|
+
* @param {PondMessage} payload - The payload to send
|
|
356
|
+
* @param {string | string[]} userIds - The ids of the clients to send the message to
|
|
298
357
|
*/
|
|
299
|
-
|
|
358
|
+
broadcastTo<Key extends keyof EventType>(event: Key, payload: EventType[Key], userIds: string | string[]): JoinResponse;
|
|
300
359
|
|
|
301
360
|
/**
|
|
302
361
|
* @desc tracks the presence of a client
|
|
303
|
-
* @param presence - the presence data to track
|
|
362
|
+
* @param {PondPresence} presence - the presence data to track
|
|
304
363
|
*/
|
|
305
|
-
trackPresence
|
|
364
|
+
trackPresence(presence: PresenceType): JoinResponse;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @desc Subscribes the client to a channel.
|
|
368
|
+
* @param {string} channel - The channel to subscribe to.
|
|
369
|
+
* @returns {JoinResponse} - The JoinResponse instance for chaining.
|
|
370
|
+
*/
|
|
371
|
+
subscribeTo(channel: string): JoinResponse;
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @desc Unsubscribes the client from a channel.
|
|
375
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
376
|
+
* @returns {JoinResponse} - The JoinResponse instance for chaining.
|
|
377
|
+
*/
|
|
378
|
+
unsubscribeFrom(channel: string): JoinResponse;
|
|
306
379
|
}
|
|
307
380
|
|
|
308
|
-
export declare class
|
|
381
|
+
export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
309
382
|
/**
|
|
310
383
|
* @desc Whether the server has responded to the request
|
|
311
384
|
*/
|
|
312
385
|
hasResponded: boolean;
|
|
313
386
|
|
|
314
387
|
/**
|
|
315
|
-
* @desc
|
|
316
|
-
* @param assigns -
|
|
388
|
+
* @desc Assigns data to the client.
|
|
389
|
+
* @param {PondAssigns} assigns - The data to assign to the client.
|
|
390
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
317
391
|
*/
|
|
318
|
-
|
|
392
|
+
assign(assigns: AssignType): EventResponse<EventType, PresenceType, AssignType>;
|
|
319
393
|
|
|
320
394
|
/**
|
|
321
|
-
* @desc
|
|
322
|
-
* @param
|
|
323
|
-
* @param
|
|
395
|
+
* @desc Emits a direct message to the client.
|
|
396
|
+
* @param {string} event - The event name.
|
|
397
|
+
* @param {PondMessage} payload - The payload to send.
|
|
398
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
324
399
|
*/
|
|
325
|
-
|
|
400
|
+
reply<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
|
|
326
401
|
|
|
327
402
|
/**
|
|
328
|
-
* @desc
|
|
329
|
-
* @param event -
|
|
330
|
-
* @param payload -
|
|
331
|
-
* @
|
|
403
|
+
* @desc Sends a message to all clients in the channel.
|
|
404
|
+
* @param {string} event - The event to send.
|
|
405
|
+
* @param {PondMessage} payload - The payload to send.
|
|
406
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
332
407
|
*/
|
|
333
|
-
|
|
334
|
-
}
|
|
408
|
+
broadcast<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
|
|
335
409
|
|
|
336
|
-
export declare class PondChannel <EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
337
410
|
/**
|
|
338
|
-
* @desc
|
|
339
|
-
* @param event - The event to
|
|
340
|
-
* @param
|
|
341
|
-
* @
|
|
342
|
-
* pond.onEvent('echo', (request, response) => {
|
|
343
|
-
* response.send('echo', {
|
|
344
|
-
* message: request.event.payload,
|
|
345
|
-
* });
|
|
346
|
-
* });
|
|
411
|
+
* @desc Sends a message to all clients in the channel except the client making the request.
|
|
412
|
+
* @param {string} event - The event to send.
|
|
413
|
+
* @param {PondMessage} payload - The payload to send.
|
|
414
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
347
415
|
*/
|
|
348
|
-
|
|
416
|
+
broadcastFrom<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent]): EventResponse<EventType, PresenceType, AssignType>;
|
|
349
417
|
|
|
350
418
|
/**
|
|
351
|
-
* @desc
|
|
352
|
-
* @param event - The event to
|
|
353
|
-
* @param payload - The payload to send
|
|
354
|
-
* @param
|
|
355
|
-
* @
|
|
356
|
-
* pond.broadcast('echo', {
|
|
357
|
-
* message: 'Hello World',
|
|
358
|
-
* timestamp: Date.now(),
|
|
359
|
-
* channel: 'my_channel',
|
|
360
|
-
*});
|
|
419
|
+
* @desc Sends a message to a set of clients in the channel.
|
|
420
|
+
* @param {string} event - The event to send.
|
|
421
|
+
* @param {PondMessage} payload - The payload to send.
|
|
422
|
+
* @param {string | string[]} userIds - The ids of the clients to send the message to.
|
|
423
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
361
424
|
*/
|
|
362
|
-
|
|
425
|
+
broadcastTo<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent], userIds: string | string[]): EventResponse<EventType, PresenceType, AssignType>;
|
|
363
426
|
|
|
364
427
|
/**
|
|
365
|
-
* @desc
|
|
366
|
-
* @param
|
|
428
|
+
* @desc Tracks a user's presence in the channel.
|
|
429
|
+
* @param {PondPresence} presence - The initial presence data.
|
|
430
|
+
* @param {string} userId - The id of the user to track.
|
|
431
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
367
432
|
*/
|
|
368
|
-
|
|
433
|
+
trackPresence(presence: PresenceType, userId?: string): EventResponse<EventType, PresenceType, AssignType>;
|
|
369
434
|
|
|
370
435
|
/**
|
|
371
|
-
* @desc
|
|
372
|
-
* @param
|
|
436
|
+
* @desc Updates a user's presence in the channel.
|
|
437
|
+
* @param {PondPresence} presence - The updated presence data.
|
|
438
|
+
* @param {string} userId - The id of the user to update.
|
|
439
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
373
440
|
*/
|
|
374
|
-
|
|
375
|
-
}
|
|
441
|
+
updatePresence(presence: PresenceType, userId?: string): EventResponse;
|
|
376
442
|
|
|
377
|
-
|
|
378
|
-
|
|
443
|
+
/**
|
|
444
|
+
* @desc Removes a user's presence from the channel.
|
|
445
|
+
* @param {string} userId - The id of the user to remove.
|
|
446
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
447
|
+
*/
|
|
448
|
+
removePresence(userId?: string): EventResponse;
|
|
379
449
|
|
|
380
450
|
/**
|
|
381
|
-
* @desc
|
|
382
|
-
* @param
|
|
451
|
+
* @desc Subscribes the client to a channel.
|
|
452
|
+
* @param {string} channel - The channel to subscribe to.
|
|
453
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
383
454
|
*/
|
|
384
|
-
|
|
455
|
+
subscribeTo(channel: string): EventResponse;
|
|
385
456
|
|
|
386
457
|
/**
|
|
387
|
-
* @desc
|
|
388
|
-
* @param
|
|
458
|
+
* @desc Unsubscribes the client from a channel.
|
|
459
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
460
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
389
461
|
*/
|
|
390
|
-
|
|
462
|
+
unsubscribeFrom(channel: string): EventResponse;
|
|
391
463
|
|
|
392
464
|
/**
|
|
393
|
-
* @desc
|
|
394
|
-
* @param
|
|
395
|
-
* @param
|
|
396
|
-
* @
|
|
397
|
-
* const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
398
|
-
* const token = req.query.token;
|
|
399
|
-
* if (!token)
|
|
400
|
-
* return res.reject('No token provided');
|
|
401
|
-
* res.accept({
|
|
402
|
-
* assign: {
|
|
403
|
-
* token
|
|
404
|
-
* }
|
|
405
|
-
* });
|
|
406
|
-
* })
|
|
465
|
+
* @desc Evicts a user from the channel.
|
|
466
|
+
* @param {string} reason - The reason for the eviction.
|
|
467
|
+
* @param {string} userId - The id of the user to evict.
|
|
468
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
407
469
|
*/
|
|
408
|
-
|
|
470
|
+
evictUser(reason: string, userId?: string): EventResponse;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* @desc Closes the channel from the server side for all clients.
|
|
474
|
+
* @param {string} reason - The reason for closing the channel.
|
|
475
|
+
*/
|
|
476
|
+
closeChannel(reason: string): void;
|
|
409
477
|
}
|
|
478
|
+
|