@eleven-am/pondsocket 0.1.23 → 0.1.24

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/client/channel.js CHANGED
@@ -57,6 +57,17 @@ class Channel {
57
57
  }
58
58
  });
59
59
  }
60
+ /**
61
+ * @desc Monitors the channel for messages.
62
+ * @param callback - The callback to call when a message is received.
63
+ */
64
+ onMessageEvent(callback) {
65
+ return this._receiver.subscribe((data) => {
66
+ if (data.action === enums_1.ServerActions.BROADCAST && data.channelName === this._name) {
67
+ return callback(data.event, data.payload);
68
+ }
69
+ });
70
+ }
60
71
  /**
61
72
  * @desc Monitors the connection state of the channel.
62
73
  * @param callback - The callback to call when the connection state changes.
package/client.js CHANGED
@@ -18,7 +18,7 @@ class PondClient {
18
18
  if (address.protocol !== 'wss:' && address.protocol !== 'ws:') {
19
19
  address.protocol = protocol;
20
20
  }
21
- this.address = address;
21
+ this._address = address;
22
22
  this._channels = {};
23
23
  this._broadcaster = new subjectUtils_1.SimpleSubject();
24
24
  this._connectionState = new subjectUtils_1.SimpleBehaviorSubject('CLOSED');
@@ -27,7 +27,7 @@ class PondClient {
27
27
  * @desc Connects to the server and returns the socket.
28
28
  */
29
29
  connect(backoff = 1) {
30
- const socket = new WebSocket(this.address.toString());
30
+ const socket = new WebSocket(this._address.toString());
31
31
  socket.onopen = () => {
32
32
  this._connectionState.publish('OPEN');
33
33
  };
package/node.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ // eslint-disable-next-line import/no-unresolved
2
+ import { PondSocketClient } from './types';
3
+
4
+ export default PondSocketClient;
package/node.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ws_1 = require("ws");
7
+ const client_1 = __importDefault(require("./client"));
8
+ class PondClient extends client_1.default {
9
+ /**
10
+ * @desc Connects to the server and returns the socket.
11
+ */
12
+ connect(backoff = 1) {
13
+ const socket = new ws_1.WebSocket(this._address.toString());
14
+ socket.onopen = () => {
15
+ this._connectionState.publish('OPEN');
16
+ };
17
+ socket.onmessage = (message) => {
18
+ const data = JSON.parse(message.data);
19
+ this._broadcaster.publish(data);
20
+ };
21
+ socket.onerror = () => {
22
+ this._connectionState.publish('CLOSED');
23
+ setTimeout(() => {
24
+ this.connect(backoff * 2);
25
+ }, backoff * 1000);
26
+ };
27
+ this._socket = socket;
28
+ }
29
+ }
30
+ exports.default = PondClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.23",
3
+ "version": "0.1.24",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const superwstest_1 = __importDefault(require("superwstest"));
16
16
  const endpoint_1 = require("./endpoint");
17
+ const enums_1 = require("../../enums");
17
18
  const pondChannel_1 = require("../pondChannel/pondChannel");
18
19
  const pondSocket_1 = require("../server/pondSocket");
19
20
  const createPondSocket = () => {
@@ -93,6 +94,7 @@ describe('endpoint', () => {
93
94
  .expectJson({
94
95
  event: 'Hello',
95
96
  channelName: 'SERVER',
97
+ action: enums_1.ServerActions.SYSTEM,
96
98
  payload: {
97
99
  room: 'socket',
98
100
  },
@@ -100,6 +102,7 @@ describe('endpoint', () => {
100
102
  .expectJson({
101
103
  event: 'TEST',
102
104
  channelName: 'SERVER',
105
+ action: enums_1.ServerActions.BROADCAST,
103
106
  payload: {
104
107
  message: 'Hello everyone',
105
108
  },
@@ -112,6 +115,7 @@ describe('endpoint', () => {
112
115
  .expectJson({
113
116
  event: 'Hello',
114
117
  channelName: 'SERVER',
118
+ action: enums_1.ServerActions.SYSTEM,
115
119
  payload: {
116
120
  room: 'secondSocket',
117
121
  },
@@ -119,6 +123,7 @@ describe('endpoint', () => {
119
123
  .expectJson({
120
124
  event: 'TEST',
121
125
  channelName: 'SERVER',
126
+ action: enums_1.ServerActions.BROADCAST,
122
127
  payload: {
123
128
  message: 'Hello everyone',
124
129
  },
@@ -134,6 +139,7 @@ describe('endpoint', () => {
134
139
  event: 'TEST',
135
140
  payload: {},
136
141
  };
142
+ console.log(JSON.stringify(message, null, 2));
137
143
  const { socket, server, createPondChannel } = createPondSocket();
138
144
  expect(server).toBeDefined();
139
145
  const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
@@ -209,6 +215,7 @@ describe('endpoint', () => {
209
215
  .expectJson({
210
216
  event: 'error',
211
217
  channelName: 'ENDPOINT',
218
+ action: enums_1.ServerActions.ERROR,
212
219
  payload: {
213
220
  message: 'GatewayEngine: Channel /socket/socket does not exist',
214
221
  },
@@ -254,6 +261,7 @@ describe('endpoint', () => {
254
261
  .expectJson({
255
262
  event: 'error_channel',
256
263
  channelName: '/test/socket',
264
+ action: enums_1.ServerActions.ERROR,
257
265
  payload: {
258
266
  message: 'Unauthorized request',
259
267
  code: 403,
@@ -264,11 +272,13 @@ describe('endpoint', () => {
264
272
  payload: {},
265
273
  event: 'TEST',
266
274
  channelName: '/test/socket',
275
+ action: enums_1.ServerActions.BROADCAST,
267
276
  })
268
277
  .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST3', action: endpoint_1.ClientActions.BROADCAST }))
269
278
  .expectJson({
270
279
  event: 'error_channel',
271
280
  channelName: '/test/socket',
281
+ action: enums_1.ServerActions.ERROR,
272
282
  payload: {
273
283
  message: 'choke on my balls',
274
284
  code: 403,
@@ -303,10 +313,10 @@ describe('endpoint', () => {
303
313
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
304
314
  .sendJson(message)
305
315
  .expectJson({
306
- event: 'presence_change',
316
+ event: enums_1.PresenceEventTypes.JOIN,
307
317
  channelName: '/test/socket',
318
+ action: enums_1.ServerActions.PRESENCE,
308
319
  payload: {
309
- type: 'join',
310
320
  changed: {
311
321
  status: 'online',
312
322
  },
@@ -346,6 +356,7 @@ describe('endpoint', () => {
346
356
  .expectJson({
347
357
  event: 'error',
348
358
  channelName: 'ENDPOINT',
359
+ action: enums_1.ServerActions.ERROR,
349
360
  payload: {
350
361
  message: 'GatewayEngine: Channel /test/socket does not exist',
351
362
  },
@@ -355,6 +366,7 @@ describe('endpoint', () => {
355
366
  .expectJson({
356
367
  event: 'TEST',
357
368
  channelName: '/test/socket',
369
+ action: enums_1.ServerActions.SYSTEM,
358
370
  payload: {
359
371
  test: 'test',
360
372
  },
@@ -386,6 +398,7 @@ describe('endpoint', () => {
386
398
  .expectJson({
387
399
  event: 'TEST',
388
400
  channelName: 'SERVER',
401
+ action: enums_1.ServerActions.SYSTEM,
389
402
  payload: {
390
403
  test: 'test',
391
404
  },
@@ -397,6 +410,7 @@ describe('endpoint', () => {
397
410
  .expectJson({
398
411
  event: 'TEST',
399
412
  channelName: 'SERVER',
413
+ action: enums_1.ServerActions.SYSTEM,
400
414
  payload: {
401
415
  test: 'test',
402
416
  },
package/types.d.ts CHANGED
@@ -131,6 +131,12 @@ export declare class Channel {
131
131
  */
132
132
  public onMessage(event: string, callback: (message: PondMessage) => void): Unsubscribe;
133
133
 
134
+ /**
135
+ * @desc Monitors the channel for messages.
136
+ * @param callback - The callback to call when a message is received.
137
+ */
138
+ public onMessageEvent(callback: (event: string, message: PondMessage) => void): Unsubscribe;
139
+
134
140
  /**
135
141
  * @desc Monitors the connection state of the channel.
136
142
  * @param callback - The callback to call when the connection state changes.
@@ -182,7 +188,6 @@ export declare class Channel {
182
188
  */
183
189
  public isConnected(): boolean;
184
190
 
185
-
186
191
  /**
187
192
  * @desc check is the channel has been closed.
188
193
  */
@@ -200,7 +205,7 @@ export declare class Channel {
200
205
  public onUsersChange(callback: (users: PondPresence[]) => void): Unsubscribe;
201
206
  }
202
207
 
203
- export declare class PondSocketClient {
208
+ declare class PondSocketClient {
204
209
  constructor(endpoint: string, params?: Record<string, any>);
205
210
 
206
211
  /**
@@ -451,7 +456,7 @@ declare class ConnectionResponse {
451
456
  send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
452
457
  }
453
458
 
454
- export declare class Endpoint {
459
+ declare class Endpoint {
455
460
  /**
456
461
  * @desc Adds a new PondChannel to this path on this endpoint
457
462
  * @param path - The path to add the channel to
@@ -486,7 +491,7 @@ export declare class Endpoint {
486
491
  closeConnection (clientIds: string | string[]): void;
487
492
  }
488
493
 
489
- export declare class PondSocket {
494
+ declare class PondSocket {
490
495
  constructor (server?: HTTPServer, socketServer?: WebSocketServer);
491
496
 
492
497
  /**
@@ -532,5 +537,5 @@ export declare class PondSocket {
532
537
  * @param app - The Express app to be used by the server
533
538
  * @constructor
534
539
  */
535
- export declare const PondSocketFromExpress: (app: Express) => PondSocketExpressApp;
540
+ declare const PondSocketFromExpress: (app: Express) => PondSocketExpressApp;
536
541