@eleven-am/pondsocket 0.1.21 → 0.1.23
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/index.d.ts +1 -0
- package/package.json +1 -1
- package/server/endpoint/endpoint.js +2 -2
- package/server/endpoint/endpoint.test.js +6 -6
- package/server/pondChannel/pondChannel.js +26 -2
- package/types.d.ts +18 -4
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -23,9 +23,9 @@ class Endpoint {
|
|
|
23
23
|
* @param channel - The channel to add
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
|
-
* endpoint.
|
|
26
|
+
* endpoint.addChannel('/chat', pondChannelInstance);
|
|
27
27
|
*/
|
|
28
|
-
|
|
28
|
+
addChannel(path, channel) {
|
|
29
29
|
const manager = channel._buildManager();
|
|
30
30
|
this._channels.push({ path,
|
|
31
31
|
manager });
|
|
@@ -157,8 +157,8 @@ describe('endpoint', () => {
|
|
|
157
157
|
},
|
|
158
158
|
});
|
|
159
159
|
});
|
|
160
|
-
endpoint.
|
|
161
|
-
endpoint.
|
|
160
|
+
endpoint.addChannel('/test/:room', testPond);
|
|
161
|
+
endpoint.addChannel('/socket/:room', socketPond);
|
|
162
162
|
yield (0, superwstest_1.default)(server)
|
|
163
163
|
.ws('/api/socket')
|
|
164
164
|
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
@@ -189,7 +189,7 @@ describe('endpoint', () => {
|
|
|
189
189
|
},
|
|
190
190
|
});
|
|
191
191
|
});
|
|
192
|
-
endpoint.
|
|
192
|
+
endpoint.addChannel('/test/:room', testPond);
|
|
193
193
|
const message = {
|
|
194
194
|
action: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
195
195
|
channelName: '/test/socket',
|
|
@@ -239,7 +239,7 @@ describe('endpoint', () => {
|
|
|
239
239
|
channel.onJoinRequest((_, res) => {
|
|
240
240
|
res.accept();
|
|
241
241
|
});
|
|
242
|
-
endpoint.
|
|
242
|
+
endpoint.addChannel('/test/:room', channel);
|
|
243
243
|
const message = {
|
|
244
244
|
action: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
245
245
|
channelName: '/test/socket',
|
|
@@ -291,7 +291,7 @@ describe('endpoint', () => {
|
|
|
291
291
|
status: 'online',
|
|
292
292
|
});
|
|
293
293
|
});
|
|
294
|
-
endpoint.
|
|
294
|
+
endpoint.addChannel('/test/:room', channel);
|
|
295
295
|
const message = {
|
|
296
296
|
action: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
297
297
|
channelName: '/test/socket',
|
|
@@ -332,7 +332,7 @@ describe('endpoint', () => {
|
|
|
332
332
|
channel.onJoinRequest((_, res) => {
|
|
333
333
|
res.send('TEST', { test: 'test' });
|
|
334
334
|
});
|
|
335
|
-
endpoint.
|
|
335
|
+
endpoint.addChannel('/test/:room', channel);
|
|
336
336
|
const message = {
|
|
337
337
|
action: endpoint_1.ClientActions.LEAVE_CHANNEL,
|
|
338
338
|
channelName: '/test/socket',
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PondChannel = void 0;
|
|
4
4
|
const joinRequest_1 = require("./joinRequest");
|
|
5
5
|
const joinResponse_1 = require("./joinResponse");
|
|
6
|
+
const enums_1 = require("../../enums");
|
|
6
7
|
const middleware_1 = require("../abstracts/middleware");
|
|
7
8
|
const channelEngine_1 = require("../channel/channelEngine");
|
|
8
9
|
class PondChannel {
|
|
@@ -17,7 +18,7 @@ class PondChannel {
|
|
|
17
18
|
* @example
|
|
18
19
|
* const pond = new PondChannelEngine();
|
|
19
20
|
* pond.onJoinRequest((request, response) => {
|
|
20
|
-
* if (request.
|
|
21
|
+
* if (request.user.assigns.admin)
|
|
21
22
|
* response.accept();
|
|
22
23
|
* else
|
|
23
24
|
* response.reject('You are not an admin', 403);
|
|
@@ -33,7 +34,7 @@ class PondChannel {
|
|
|
33
34
|
* @example
|
|
34
35
|
* pond.onEvent('echo', (request, response) => {
|
|
35
36
|
* response.send('echo', {
|
|
36
|
-
* message: request.payload,
|
|
37
|
+
* message: request.event.payload,
|
|
37
38
|
* });
|
|
38
39
|
* });
|
|
39
40
|
*/
|
|
@@ -45,6 +46,29 @@ class PondChannel {
|
|
|
45
46
|
next();
|
|
46
47
|
});
|
|
47
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* @desc Broadcasts a message to all users in a channel
|
|
51
|
+
* @param event - The event to broadcast
|
|
52
|
+
* @param payload - The payload to send
|
|
53
|
+
* @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
|
|
54
|
+
* @example
|
|
55
|
+
* pond.broadcast('echo', {
|
|
56
|
+
* message: 'Hello World',
|
|
57
|
+
* timestamp: Date.now(),
|
|
58
|
+
* channel: 'my_channel',
|
|
59
|
+
*});
|
|
60
|
+
*/
|
|
61
|
+
broadcast(event, payload, channelName) {
|
|
62
|
+
if (channelName) {
|
|
63
|
+
const channel = this._getChannel(channelName) || this._createChannel(channelName);
|
|
64
|
+
channel.sendMessage('channel', 'all_users', enums_1.ServerActions.SYSTEM, event, payload);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this._channels.forEach((channel) => {
|
|
68
|
+
channel.sendMessage('channel', 'all_users', enums_1.ServerActions.SYSTEM, event, payload);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
48
72
|
/**
|
|
49
73
|
* @desc Builds a PondChannelManager from the current engine
|
|
50
74
|
*/
|
package/types.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import internal from 'stream';
|
|
|
5
5
|
import { Express } from 'express';
|
|
6
6
|
import { WebSocketServer, WebSocket } from 'ws';
|
|
7
7
|
|
|
8
|
-
import { ClientActions, PresenceEventTypes } from './enums
|
|
8
|
+
import { ClientActions, PresenceEventTypes } from './enums';
|
|
9
9
|
|
|
10
10
|
export type PondPath = string | RegExp;
|
|
11
11
|
type NextFunction = () => void;
|
|
@@ -392,7 +392,7 @@ declare class PondChannel {
|
|
|
392
392
|
* @example
|
|
393
393
|
* const pond = new PondChannelEngine();
|
|
394
394
|
* pond.onJoinRequest((request, response) => {
|
|
395
|
-
* if (request.
|
|
395
|
+
* if (request.user.assigns.admin)
|
|
396
396
|
* response.accept();
|
|
397
397
|
* else
|
|
398
398
|
* response.reject('You are not an admin', 403);
|
|
@@ -407,11 +407,25 @@ declare class PondChannel {
|
|
|
407
407
|
* @example
|
|
408
408
|
* pond.onEvent('echo', (request, response) => {
|
|
409
409
|
* response.send('echo', {
|
|
410
|
-
* message: request.payload,
|
|
410
|
+
* message: request.event.payload,
|
|
411
411
|
* });
|
|
412
412
|
* });
|
|
413
413
|
*/
|
|
414
414
|
onEvent (event: PondPath, handler: (request: EventRequest, response: EventResponse) => void | Promise<void>): void;
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @desc Broadcasts a message to all users in a channel
|
|
418
|
+
* @param event - The event to broadcast
|
|
419
|
+
* @param payload - The payload to send
|
|
420
|
+
* @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
|
|
421
|
+
* @example
|
|
422
|
+
* pond.broadcast('echo', {
|
|
423
|
+
* message: 'Hello World',
|
|
424
|
+
* timestamp: Date.now(),
|
|
425
|
+
* channel: 'my_channel',
|
|
426
|
+
*});
|
|
427
|
+
*/
|
|
428
|
+
broadcast (event: string, payload: PondMessage, channelName?: string): void;
|
|
415
429
|
}
|
|
416
430
|
|
|
417
431
|
declare class ConnectionResponse {
|
|
@@ -446,7 +460,7 @@ export declare class Endpoint {
|
|
|
446
460
|
* @example
|
|
447
461
|
* endpoint.useChannel('/chat', pondChannelInstance);
|
|
448
462
|
*/
|
|
449
|
-
|
|
463
|
+
addChannel (path: PondPath, channel: PondChannel): void;
|
|
450
464
|
|
|
451
465
|
/**
|
|
452
466
|
* @desc List all clients connected to this endpoint
|