@eleven-am/pondsocket 0.1.139 → 0.1.142
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/channel/channel.js +81 -60
- package/channel/channel.test.js +86 -61
- package/channel/eventRequest.test.js +3 -3
- package/channel/eventResponse.js +36 -61
- package/channel/eventResponse.test.js +31 -53
- package/endpoint/endpoint.test.js +30 -13
- package/endpoint/endpointEngine.js +321 -0
- 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 -22
- 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 +9 -6
- package/types.d.ts +200 -138
- package/abstracts/abstractResponse.js +0 -9
- package/endpoint/endpoint.js +0 -239
package/server/pondSocket.js
CHANGED
|
@@ -17,7 +17,7 @@ const http_1 = require("http");
|
|
|
17
17
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
18
18
|
const ws_1 = require("ws");
|
|
19
19
|
const middleware_1 = require("../abstracts/middleware");
|
|
20
|
-
const
|
|
20
|
+
const endpointEngine_1 = require("../endpoint/endpointEngine");
|
|
21
21
|
const response_1 = require("../endpoint/response");
|
|
22
22
|
const matcher_1 = require("../matcher/matcher");
|
|
23
23
|
class PondSocket {
|
|
@@ -62,7 +62,7 @@ class PondSocket {
|
|
|
62
62
|
* })
|
|
63
63
|
*/
|
|
64
64
|
createEndpoint(path, handler) {
|
|
65
|
-
const endpoint = new
|
|
65
|
+
const endpoint = new endpointEngine_1.EndpointEngine(this);
|
|
66
66
|
__classPrivateFieldGet(this, _PondSocket_middleware, "f").use((req, socket, next) => {
|
|
67
67
|
const event = (0, matcher_1.parseAddress)(path, req.address);
|
|
68
68
|
if (event) {
|
|
@@ -72,7 +72,7 @@ class PondSocket {
|
|
|
72
72
|
}
|
|
73
73
|
return next();
|
|
74
74
|
});
|
|
75
|
-
return endpoint;
|
|
75
|
+
return new endpointEngine_1.Endpoint(endpoint);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
exports.PondSocket = PondSocket;
|
|
@@ -82,7 +82,7 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
|
|
|
82
82
|
socket.isAlive = true;
|
|
83
83
|
});
|
|
84
84
|
});
|
|
85
|
-
|
|
85
|
+
return setInterval(() => {
|
|
86
86
|
__classPrivateFieldGet(this, _PondSocket_socketServer, "f").clients.forEach((socket) => {
|
|
87
87
|
if (socket.isAlive === false) {
|
|
88
88
|
return socket.terminate();
|
|
@@ -91,12 +91,15 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
|
|
|
91
91
|
socket.ping();
|
|
92
92
|
});
|
|
93
93
|
}, 30000);
|
|
94
|
-
__classPrivateFieldGet(this, _PondSocket_socketServer, "f").on('close', () => clearInterval(interval));
|
|
95
94
|
}, _PondSocket_init = function _PondSocket_init() {
|
|
96
|
-
__classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_manageHeartbeat).call(this);
|
|
95
|
+
const timeout = __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_manageHeartbeat).call(this);
|
|
97
96
|
__classPrivateFieldGet(this, _PondSocket_server, "f").on('error', (error) => {
|
|
97
|
+
clearInterval(timeout);
|
|
98
98
|
throw new Error(error.message);
|
|
99
99
|
});
|
|
100
|
+
__classPrivateFieldGet(this, _PondSocket_server, "f").on('close', () => {
|
|
101
|
+
clearInterval(timeout);
|
|
102
|
+
});
|
|
100
103
|
__classPrivateFieldGet(this, _PondSocket_server, "f").on('upgrade', (req, socket, head) => {
|
|
101
104
|
const clientId = req.headers['sec-websocket-key'];
|
|
102
105
|
const request = {
|
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
|
|
|
@@ -44,96 +45,102 @@ export declare class EventResponse<EventType extends PondEventMap = PondEventMap
|
|
|
44
45
|
hasResponded: boolean;
|
|
45
46
|
|
|
46
47
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @param assigns -
|
|
48
|
+
* Assigns data to the client.
|
|
49
|
+
* @param {PondAssigns} assigns - The data to assign to the client.
|
|
50
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
49
51
|
*/
|
|
50
|
-
|
|
52
|
+
assign(assigns: AssignType): EventResponse<EventType, PresenceType, AssignType>;
|
|
51
53
|
|
|
52
54
|
/**
|
|
53
|
-
* @desc
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
56
|
-
* @
|
|
55
|
+
* @desc Emits a direct message to the client.
|
|
56
|
+
* @param {string} event - The event name.
|
|
57
|
+
* @param {PondMessage} payload - The payload to send.
|
|
58
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
57
59
|
*/
|
|
58
|
-
|
|
60
|
+
reply<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
|
|
59
61
|
|
|
60
62
|
/**
|
|
61
|
-
* @desc
|
|
62
|
-
* @param event -
|
|
63
|
-
* @param payload -
|
|
64
|
-
* @
|
|
63
|
+
* @desc Sends a message to all clients in the channel.
|
|
64
|
+
* @param {string} event - The event to send.
|
|
65
|
+
* @param {PondMessage} payload - The payload to send.
|
|
66
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
65
67
|
*/
|
|
66
|
-
|
|
68
|
+
broadcast<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
|
|
67
69
|
|
|
68
70
|
/**
|
|
69
|
-
* @desc
|
|
70
|
-
* @param event -
|
|
71
|
-
* @param payload -
|
|
72
|
-
* @
|
|
71
|
+
* @desc Sends a message to all clients in the channel except the client making the request.
|
|
72
|
+
* @param {string} event - The event to send.
|
|
73
|
+
* @param {PondMessage} payload - The payload to send.
|
|
74
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
73
75
|
*/
|
|
74
|
-
|
|
76
|
+
broadcastFrom<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent]): EventResponse<EventType, PresenceType, AssignType>;
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
|
-
* @desc Sends a message to
|
|
78
|
-
* @param event -
|
|
79
|
-
* @param payload -
|
|
79
|
+
* @desc Sends a message to a set of clients in the channel.
|
|
80
|
+
* @param {string} event - The event to send.
|
|
81
|
+
* @param {PondMessage} payload - The payload to send.
|
|
82
|
+
* @param {string | string[]} userIds - The ids of the clients to send the message to.
|
|
83
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
80
84
|
*/
|
|
81
|
-
|
|
85
|
+
broadcastTo<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent], userIds: string | string[]): EventResponse<EventType, PresenceType, AssignType>;
|
|
82
86
|
|
|
83
87
|
/**
|
|
84
|
-
* @desc
|
|
85
|
-
* @param
|
|
86
|
-
* @param
|
|
88
|
+
* @desc Tracks a user's presence in the channel.
|
|
89
|
+
* @param {PondPresence} presence - The initial presence data.
|
|
90
|
+
* @param {string} userId - The id of the user to track.
|
|
91
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
87
92
|
*/
|
|
88
|
-
|
|
93
|
+
trackPresence(presence: PresenceType, userId?: string): EventResponse<EventType, PresenceType, AssignType>;
|
|
89
94
|
|
|
90
95
|
/**
|
|
91
|
-
* @desc
|
|
92
|
-
* @param
|
|
93
|
-
* @param
|
|
94
|
-
* @
|
|
96
|
+
* @desc Updates a user's presence in the channel.
|
|
97
|
+
* @param {PondPresence} presence - The updated presence data.
|
|
98
|
+
* @param {string} userId - The id of the user to update.
|
|
99
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
95
100
|
*/
|
|
96
|
-
|
|
101
|
+
updatePresence(presence: PresenceType, userId?: string): EventResponse;
|
|
97
102
|
|
|
98
103
|
/**
|
|
99
|
-
* @desc
|
|
100
|
-
* @param
|
|
101
|
-
* @
|
|
104
|
+
* @desc Removes a user's presence from the channel.
|
|
105
|
+
* @param {string} userId - The id of the user to remove.
|
|
106
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
102
107
|
*/
|
|
103
|
-
|
|
108
|
+
removePresence(userId?: string): EventResponse;
|
|
104
109
|
|
|
105
110
|
/**
|
|
106
|
-
* @desc
|
|
107
|
-
* @param
|
|
108
|
-
* @
|
|
111
|
+
* @desc Subscribes the client to a channel.
|
|
112
|
+
* @param {string} channel - The channel to subscribe to.
|
|
113
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
109
114
|
*/
|
|
110
|
-
|
|
115
|
+
subscribeTo(channel: string): EventResponse;
|
|
111
116
|
|
|
112
117
|
/**
|
|
113
|
-
* @desc
|
|
114
|
-
* @param
|
|
118
|
+
* @desc Unsubscribes the client from a channel.
|
|
119
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
120
|
+
* @returns {EventResponse} - The EventResponse instance for chaining.
|
|
115
121
|
*/
|
|
116
|
-
|
|
122
|
+
unsubscribeFrom(channel: string): EventResponse;
|
|
117
123
|
|
|
118
124
|
/**
|
|
119
|
-
* @desc Evicts a user from the channel
|
|
120
|
-
* @param reason -
|
|
121
|
-
* @param userId -
|
|
125
|
+
* @desc Evicts a user from the channel.
|
|
126
|
+
* @param {string} reason - The reason for the eviction.
|
|
127
|
+
* @param {string} userId - The id of the user to evict.
|
|
122
128
|
*/
|
|
123
|
-
evictUser
|
|
129
|
+
evictUser(reason: string, userId?: string): void;
|
|
124
130
|
|
|
125
131
|
/**
|
|
126
|
-
* @desc Closes the channel from the server side for all clients
|
|
127
|
-
* @param reason -
|
|
132
|
+
* @desc Closes the channel from the server side for all clients.
|
|
133
|
+
* @param {string} reason - The reason for closing the channel.
|
|
128
134
|
*/
|
|
129
|
-
closeChannel
|
|
135
|
+
closeChannel(reason: string): void;
|
|
130
136
|
}
|
|
131
137
|
|
|
132
138
|
export declare class Endpoint {
|
|
133
139
|
/**
|
|
134
140
|
* @desc Adds a new PondChannel to this path on this endpoint
|
|
135
|
-
* @param path - The path to add the channel to
|
|
136
|
-
* @param handler - The handler to use to authenticate the client
|
|
141
|
+
* @param {PondPath<string>} path - The path to add the channel to
|
|
142
|
+
* @param {ChannelHandler} handler - The handler to use to authenticate the client
|
|
143
|
+
* @returns {PondChannel} - The PondChannel instance for the new channel
|
|
137
144
|
*
|
|
138
145
|
* @example
|
|
139
146
|
* const channel = endpoint.createChannel('/chat', (request, response) => {
|
|
@@ -144,20 +151,26 @@ export declare class Endpoint {
|
|
|
144
151
|
* response.reject('You are not an admin', 403);
|
|
145
152
|
* });
|
|
146
153
|
*/
|
|
147
|
-
createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>
|
|
154
|
+
createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>(path: PondPath<Path>, handler: (request: JoinRequest<Path, PresenceType, AssignType>, response: JoinResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): PondChannel<EventType, PresenceType, AssignType>;
|
|
148
155
|
|
|
149
156
|
/**
|
|
150
157
|
* @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
|
|
158
|
+
* @param {string} event - The event to broadcast.
|
|
159
|
+
* @param {PondMessage} payload - The payload to broadcast.
|
|
160
|
+
* @returns {void}
|
|
153
161
|
*/
|
|
154
|
-
broadcast
|
|
162
|
+
broadcast(event: string, payload: PondMessage): void;
|
|
155
163
|
|
|
156
164
|
/**
|
|
157
165
|
* @desc Closes specific clients connected to this endpoint
|
|
158
|
-
* @param clientIds - The
|
|
166
|
+
* @param {string | string[]} clientIds - The ids of the clients to close.
|
|
159
167
|
*/
|
|
160
|
-
closeConnection
|
|
168
|
+
closeConnection(clientIds: string | string[]): void;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @desc Returns all the clients connected to this endpoint
|
|
172
|
+
*/
|
|
173
|
+
getClients(): WebSocket[];
|
|
161
174
|
}
|
|
162
175
|
|
|
163
176
|
export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
@@ -169,76 +182,82 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
|
|
|
169
182
|
/**
|
|
170
183
|
* @desc Gets the current assign data for the channel.
|
|
171
184
|
*/
|
|
172
|
-
getAssigns
|
|
185
|
+
getAssigns(): Record<string, AssignType>;
|
|
173
186
|
|
|
174
187
|
/**
|
|
175
188
|
* @desc Gets the current presence data for the channel.
|
|
176
189
|
*/
|
|
177
|
-
getPresences
|
|
190
|
+
getPresences(): Record<string, PresenceType>;
|
|
178
191
|
|
|
179
192
|
/**
|
|
180
193
|
* @desc Gets the assign date for a specific user.
|
|
181
|
-
* @param userId - The id of the user to get the
|
|
194
|
+
* @param {string} userId - The id of the user to get the data for.
|
|
182
195
|
*/
|
|
183
|
-
getUserData
|
|
196
|
+
getUserData(userId: string): AssignType | null;
|
|
184
197
|
|
|
185
198
|
/**
|
|
186
199
|
* @desc Broadcasts a message to every client in the channel,
|
|
187
|
-
* @param event - The event to send.
|
|
188
|
-
* @param payload - The message to send.
|
|
200
|
+
* @param {string} event - The event to send.
|
|
201
|
+
* @param {PondMessage} payload - The message to send.
|
|
189
202
|
*/
|
|
190
|
-
|
|
203
|
+
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): void;
|
|
191
204
|
|
|
192
205
|
/**
|
|
193
206
|
* @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.
|
|
207
|
+
* @param {string} userId - The id of the user to exclude from the broadcast.
|
|
208
|
+
* @param {string} event - The event to send.
|
|
209
|
+
* @param {PondMessage} payload - The message to send.
|
|
197
210
|
*/
|
|
198
|
-
|
|
211
|
+
broadcastFrom<Key extends keyof EventType>(userId: string, event: Key, payload: EventType[Key]): void;
|
|
199
212
|
|
|
200
213
|
/**
|
|
201
214
|
* @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.
|
|
205
|
-
*/
|
|
206
|
-
sendToUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
|
|
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.
|
|
215
|
+
* @param {string | string[]} clientIds - The id of the client to send the message to.
|
|
216
|
+
* @param {string} event - The event to send.
|
|
217
|
+
* @param {PondMessage} payload - The message to send.
|
|
213
218
|
*/
|
|
214
|
-
|
|
219
|
+
broadcastTo<Key extends keyof EventType>(clientIds: string | string[], event: Key, payload: EventType[Key]): void;
|
|
215
220
|
|
|
216
221
|
/**
|
|
217
222
|
* @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.
|
|
223
|
+
* @param {string} userId - The id of the user to ban.
|
|
224
|
+
* @param {string} reason - The reason for the ban.
|
|
220
225
|
*/
|
|
221
|
-
evictUser
|
|
226
|
+
evictUser(userId: string, reason?: string): void;
|
|
222
227
|
|
|
223
228
|
/**
|
|
224
229
|
* @desc tracks a user's presence in the channel
|
|
225
|
-
* @param userId - the id of the user to track
|
|
226
|
-
* @param presence - the presence
|
|
230
|
+
* @param {string} userId - the id of the user to track
|
|
231
|
+
* @param {PondPresence} presence - the presence data to track
|
|
227
232
|
*/
|
|
228
|
-
trackPresence
|
|
233
|
+
trackPresence(userId: string, presence: PresenceType): void;
|
|
229
234
|
|
|
230
235
|
/**
|
|
231
236
|
* @desc removes a user's presence from the channel
|
|
232
|
-
* @param userId - the id of the user to remove
|
|
237
|
+
* @param {string} userId - the id of the user to remove
|
|
233
238
|
*/
|
|
234
|
-
removePresence
|
|
239
|
+
removePresence(userId: string): void;
|
|
235
240
|
|
|
236
241
|
/**
|
|
237
242
|
* @desc updates a user's presence in the channel
|
|
238
|
-
* @param userId - the id of the user to update
|
|
239
|
-
* @param presence - the
|
|
243
|
+
* @param {string} userId - the id of the user to update
|
|
244
|
+
* @param {PondPresence} presence - the presence data to update
|
|
245
|
+
*/
|
|
246
|
+
updatePresence(userId: string, presence: PresenceType): void;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @desc Subscribes a user to a channel.
|
|
250
|
+
* @param {string} userId - The id of the user.
|
|
251
|
+
* @param {string} channel - The channel to subscribe to.
|
|
252
|
+
*/
|
|
253
|
+
subscribeTo(userId: string, channel: string): void;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* @desc Unsubscribes a user from a channel.
|
|
257
|
+
* @param {string} userId - The id of the user.
|
|
258
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
240
259
|
*/
|
|
241
|
-
|
|
260
|
+
unsubscribeFrom(userId: string, channel: string): void;
|
|
242
261
|
}
|
|
243
262
|
|
|
244
263
|
export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
|
|
@@ -256,53 +275,73 @@ export declare class JoinResponse<EventType extends PondEventMap = PondEventMap,
|
|
|
256
275
|
hasResponded: boolean;
|
|
257
276
|
|
|
258
277
|
/**
|
|
259
|
-
* @desc
|
|
260
|
-
* @param assigns -
|
|
278
|
+
* @desc Assigns data to the client
|
|
279
|
+
* @param {PondAssigns} assigns - The data to assign
|
|
280
|
+
*/
|
|
281
|
+
assign(assigns: AssignType): JoinResponse;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @desc Accepts the join request to the channel.
|
|
285
|
+
* @returns {JoinResponse} - The JoinResponse instance for chaining.
|
|
261
286
|
*/
|
|
262
|
-
accept
|
|
287
|
+
accept(): JoinResponse;
|
|
263
288
|
|
|
264
289
|
/**
|
|
265
290
|
* @desc Rejects the request and optionally assigns data to the client
|
|
266
|
-
* @param message -
|
|
267
|
-
* @param errorCode -
|
|
291
|
+
* @param {string} message - The error message
|
|
292
|
+
* @param {number} errorCode - The error code
|
|
268
293
|
*/
|
|
269
|
-
|
|
294
|
+
decline(message?: string, errorCode?: number): JoinResponse;
|
|
270
295
|
|
|
271
296
|
/**
|
|
272
297
|
* @desc Emits a direct message to the client
|
|
273
|
-
* @param event -
|
|
274
|
-
* @param payload -
|
|
275
|
-
* @param assigns -
|
|
298
|
+
* @param {string} event - The event name
|
|
299
|
+
* @param {PondMessage} payload - The payload to send
|
|
300
|
+
* @param {PondAssigns} assigns - The data to assign to the client
|
|
276
301
|
*/
|
|
277
|
-
|
|
302
|
+
reply<Key extends keyof EventType>(event: Key, payload: EventType[Key], assigns?: AssignType): JoinResponse;
|
|
278
303
|
|
|
279
304
|
/**
|
|
280
305
|
* @desc Emits a message to all clients in the channel
|
|
281
|
-
* @param event -
|
|
282
|
-
* @param payload -
|
|
306
|
+
* @param {string} event - The event name
|
|
307
|
+
* @param {PondMessage} payload - The payload to send
|
|
283
308
|
*/
|
|
284
|
-
broadcast
|
|
309
|
+
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinResponse;
|
|
285
310
|
|
|
286
311
|
/**
|
|
287
312
|
* @desc Emits a message to all clients in the channel except the sender
|
|
288
313
|
* @param event - the event name
|
|
289
314
|
* @param payload - the payload to send
|
|
290
315
|
*/
|
|
291
|
-
|
|
316
|
+
broadcastFrom<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinResponse;
|
|
292
317
|
|
|
293
318
|
/**
|
|
294
319
|
* @desc Emits a message to a specific set of clients
|
|
295
|
-
* @param event -
|
|
296
|
-
* @param payload
|
|
297
|
-
* @param userIds -
|
|
320
|
+
* @param {string} event - The event name
|
|
321
|
+
* @param {PondMessage} payload - The payload to send
|
|
322
|
+
* @param {string | string[]} userIds - The ids of the clients to send the message to
|
|
298
323
|
*/
|
|
299
|
-
|
|
324
|
+
broadcastTo<Key extends keyof EventType>(event: Key, payload: EventType[Key], userIds: string | string[]): JoinResponse;
|
|
300
325
|
|
|
301
326
|
/**
|
|
302
327
|
* @desc tracks the presence of a client
|
|
303
|
-
* @param presence - the presence data to track
|
|
328
|
+
* @param {PondPresence} presence - the presence data to track
|
|
329
|
+
*/
|
|
330
|
+
trackPresence(presence: PresenceType): JoinResponse;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* @desc Subscribes the client to a channel.
|
|
334
|
+
* @param {string} channel - The channel to subscribe to.
|
|
335
|
+
* @returns {JoinResponse} - The JoinResponse instance for chaining.
|
|
336
|
+
*/
|
|
337
|
+
subscribeTo(channel: string): JoinResponse;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @desc Unsubscribes the client from a channel.
|
|
341
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
342
|
+
* @returns {JoinResponse} - The JoinResponse instance for chaining.
|
|
304
343
|
*/
|
|
305
|
-
|
|
344
|
+
unsubscribeFrom(channel: string): JoinResponse;
|
|
306
345
|
}
|
|
307
346
|
|
|
308
347
|
export declare class ConnectionResponse {
|
|
@@ -312,28 +351,47 @@ export declare class ConnectionResponse {
|
|
|
312
351
|
hasResponded: boolean;
|
|
313
352
|
|
|
314
353
|
/**
|
|
315
|
-
* @desc
|
|
316
|
-
* @param assigns -
|
|
354
|
+
* @desc Assigns data to the user.
|
|
355
|
+
* @param {PondAssigns} assigns - The data to assign.
|
|
356
|
+
* @returns {ConnectionResponse} - The ConnectionResponse instance for chaining.
|
|
357
|
+
*/
|
|
358
|
+
assign(assigns: PondAssigns): ConnectionResponse;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @desc Accepts the connection request to the endpoint.
|
|
317
362
|
*/
|
|
318
|
-
accept
|
|
363
|
+
accept(): void;
|
|
319
364
|
|
|
320
365
|
/**
|
|
321
366
|
* @desc Rejects the request with the given error message
|
|
322
|
-
* @param message -
|
|
323
|
-
* @param errorCode -
|
|
367
|
+
* @param {string} message - The error message
|
|
368
|
+
* @param {number} errorCode - The error code
|
|
324
369
|
*/
|
|
325
|
-
|
|
370
|
+
decline(message?: string, errorCode?: number): void;
|
|
326
371
|
|
|
327
372
|
/**
|
|
328
373
|
* @desc Emits a direct message to the client
|
|
329
|
-
* @param event -
|
|
330
|
-
* @param payload -
|
|
331
|
-
|
|
374
|
+
* @param {string} event - The event name
|
|
375
|
+
* @param {PondMessage} payload - The payload to send
|
|
376
|
+
*/
|
|
377
|
+
reply(event: string, payload: PondMessage): ConnectionResponse;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* @desc Subscribes the client to a channel.
|
|
381
|
+
* @param {string} channel - The channel to subscribe to.
|
|
382
|
+
* @returns {ConnectionResponse} - The JoinResponse instance for chaining.
|
|
332
383
|
*/
|
|
333
|
-
|
|
384
|
+
subscribeTo(channel: string): ConnectionResponse;
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* @desc Unsubscribes the client from a channel.
|
|
388
|
+
* @param {string} channel - The channel to unsubscribe from.
|
|
389
|
+
* @returns {ConnectionResponse} - The JoinResponse instance for chaining.
|
|
390
|
+
*/
|
|
391
|
+
unsubscribeFrom(channel: string): ConnectionResponse;
|
|
334
392
|
}
|
|
335
393
|
|
|
336
|
-
export declare class PondChannel
|
|
394
|
+
export declare class PondChannel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
337
395
|
/**
|
|
338
396
|
* @desc Handles an event request made by a user
|
|
339
397
|
* @param event - The event to listen for
|
|
@@ -345,7 +403,7 @@ export declare class PondChannel <EventType extends PondEventMap = PondEventMap,
|
|
|
345
403
|
* });
|
|
346
404
|
* });
|
|
347
405
|
*/
|
|
348
|
-
onEvent<Event extends string>
|
|
406
|
+
onEvent<Event extends string>(event: PondPath<Event>, handler: (request: EventRequest<Event, PresenceType, AssignType>, response: EventResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): void;
|
|
349
407
|
|
|
350
408
|
/**
|
|
351
409
|
* @desc Broadcasts a message to all users in a channel
|
|
@@ -359,35 +417,39 @@ export declare class PondChannel <EventType extends PondEventMap = PondEventMap,
|
|
|
359
417
|
* channel: 'my_channel',
|
|
360
418
|
*});
|
|
361
419
|
*/
|
|
362
|
-
broadcast
|
|
420
|
+
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key], channelName?: string): void;
|
|
363
421
|
|
|
364
422
|
/**
|
|
365
423
|
* @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
|
|
366
|
-
* @param callback - The callback to execute when a user leaves
|
|
424
|
+
* @param {LeaveCallback} callback - The callback to execute when a user leaves
|
|
425
|
+
* @example
|
|
426
|
+
* pond.onLeave((event) => {
|
|
427
|
+
* // Clean up resources
|
|
428
|
+
* });
|
|
367
429
|
*/
|
|
368
|
-
|
|
430
|
+
onLeave(callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
|
|
369
431
|
|
|
370
432
|
/**
|
|
371
433
|
* @desc Gets a channel by name
|
|
372
|
-
* @param channelName - The name of the channel to get
|
|
434
|
+
* @param {string} channelName - The name of the channel to get
|
|
373
435
|
*/
|
|
374
|
-
|
|
436
|
+
getChannel(channelName: string): Channel<EventType, PresenceType, AssignType>;
|
|
375
437
|
}
|
|
376
438
|
|
|
377
439
|
export declare class PondSocket {
|
|
378
|
-
constructor
|
|
440
|
+
constructor(server?: HTTPServer, socketServer?: WebSocketServer);
|
|
379
441
|
|
|
380
442
|
/**
|
|
381
443
|
* @desc Specifies the port to listen on
|
|
382
444
|
* @param args - the arguments to pass to the server
|
|
383
445
|
*/
|
|
384
|
-
listen
|
|
446
|
+
listen(...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
|
|
385
447
|
|
|
386
448
|
/**
|
|
387
449
|
* @desc Closes the server
|
|
388
450
|
* @param callback - the callback to call when the server is closed
|
|
389
451
|
*/
|
|
390
|
-
close
|
|
452
|
+
close(callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
|
|
391
453
|
|
|
392
454
|
/**
|
|
393
455
|
* @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
|
|
@@ -405,5 +467,5 @@ export declare class PondSocket {
|
|
|
405
467
|
* });
|
|
406
468
|
* })
|
|
407
469
|
*/
|
|
408
|
-
createEndpoint<Path extends string>
|
|
470
|
+
createEndpoint<Path extends string>(path: PondPath<Path>, handler: (request: IncomingConnection<Path>, response: ConnectionResponse) => void | Promise<void>): Endpoint;
|
|
409
471
|
}
|