@eleven-am/pondsocket 0.1.11 → 0.1.12
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/dist/client/channel.js +200 -0
- package/dist/client/index.d.ts +122 -0
- package/{pondClient/socket.js → dist/client/index.js} +30 -44
- package/dist/express/index.d.ts +36 -0
- package/dist/express/index.js +16 -0
- package/dist/index.d.ts +340 -0
- package/dist/index.js +4 -0
- package/dist/server/abstracts/abstractRequest.js +40 -0
- package/dist/server/abstracts/abstractRequest.test.js +41 -0
- package/dist/server/abstracts/abstractResponse.js +6 -0
- package/dist/server/abstracts/middleware.js +38 -0
- package/dist/server/abstracts/middleware.test.js +70 -0
- package/dist/server/channel/channelEngine.js +279 -0
- package/dist/server/channel/channelEngine.test.js +377 -0
- package/dist/server/channel/channelRequest.test.js +29 -0
- package/dist/server/channel/channelResponse.test.js +134 -0
- package/dist/server/channel/eventRequest.js +18 -0
- package/dist/server/channel/eventResponse.js +141 -0
- package/dist/server/endpoint/connectionResponse.js +50 -0
- package/dist/server/endpoint/endpoint.js +269 -0
- package/dist/server/endpoint/endpoint.test.js +406 -0
- package/dist/server/endpoint/endpointResponse.test.js +43 -0
- package/dist/server/pondChannel/joinRequest.js +29 -0
- package/dist/server/pondChannel/joinResponse.js +96 -0
- package/dist/server/pondChannel/pondChannel.js +161 -0
- package/dist/server/pondChannel/pondChannelResponse.test.js +108 -0
- package/dist/server/presence/presenceEngine.js +112 -0
- package/dist/server/presence/presenceEngine.test.js +104 -0
- package/dist/server/server/pondSocket.js +122 -0
- package/{pondSocket → dist/server/server}/server.test.js +7 -21
- package/{pondBase/baseClass.js → dist/server/utils/matchPattern.js} +33 -43
- package/{pondBase/baseClass.test.js → dist/server/utils/matchPattern.test.js} +16 -25
- package/dist/server/utils/subjectUtils.js +68 -0
- package/package.json +28 -12
- package/.eslintrc.js +0 -28
- package/base.d.ts +0 -1
- package/base.js +0 -17
- package/client.d.ts +0 -1
- package/client.js +0 -17
- package/index.d.ts +0 -1
- package/index.js +0 -17
- package/jest.config.js +0 -11
- package/pondBase/baseClass.d.ts +0 -55
- package/pondBase/enums.d.ts +0 -9
- package/pondBase/enums.js +0 -14
- package/pondBase/index.d.ts +0 -6
- package/pondBase/index.js +0 -22
- package/pondBase/pondBase.d.ts +0 -41
- package/pondBase/pondBase.js +0 -60
- package/pondBase/pondBase.test.js +0 -101
- package/pondBase/pubSub.d.ts +0 -82
- package/pondBase/pubSub.js +0 -158
- package/pondBase/pubSub.test.js +0 -332
- package/pondBase/simpleBase.d.ts +0 -126
- package/pondBase/simpleBase.js +0 -211
- package/pondBase/simpleBase.test.js +0 -153
- package/pondBase/types.d.ts +0 -2
- package/pondBase/types.js +0 -2
- package/pondClient/channel.d.ts +0 -77
- package/pondClient/channel.js +0 -167
- package/pondClient/index.d.ts +0 -2
- package/pondClient/index.js +0 -18
- package/pondClient/socket.d.ts +0 -41
- package/pondSocket/channel.d.ts +0 -129
- package/pondSocket/channel.js +0 -287
- package/pondSocket/channel.test.js +0 -377
- package/pondSocket/channelMiddleWare.d.ts +0 -28
- package/pondSocket/channelMiddleWare.js +0 -36
- package/pondSocket/endpoint.d.ts +0 -90
- package/pondSocket/endpoint.js +0 -320
- package/pondSocket/endpoint.test.js +0 -490
- package/pondSocket/enums.d.ts +0 -19
- package/pondSocket/enums.js +0 -25
- package/pondSocket/index.d.ts +0 -7
- package/pondSocket/index.js +0 -23
- package/pondSocket/pondChannel.d.ts +0 -79
- package/pondSocket/pondChannel.js +0 -211
- package/pondSocket/pondChannel.test.js +0 -430
- package/pondSocket/pondResponse.d.ts +0 -25
- package/pondSocket/pondResponse.js +0 -120
- package/pondSocket/pondSocket.d.ts +0 -47
- package/pondSocket/pondSocket.js +0 -94
- package/pondSocket/socketMiddleWare.d.ts +0 -6
- package/pondSocket/socketMiddleWare.js +0 -32
- package/pondSocket/types.d.ts +0 -74
- package/pondSocket/types.js +0 -2
- package/socket.d.ts +0 -1
- package/socket.js +0 -17
- package/tsconfig.eslint.json +0 -5
- package/tsconfig.json +0 -90
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Channel = void 0;
|
|
4
|
+
const channelEngine_1 = require("../server/channel/channelEngine");
|
|
5
|
+
const endpoint_1 = require("../server/endpoint/endpoint");
|
|
6
|
+
const presenceEngine_1 = require("../server/presence/presenceEngine");
|
|
7
|
+
const subjectUtils_1 = require("../server/utils/subjectUtils");
|
|
8
|
+
class Channel {
|
|
9
|
+
constructor(publisher, clientState, name, receiver, params = {}) {
|
|
10
|
+
this._name = name;
|
|
11
|
+
this._queue = [];
|
|
12
|
+
this._finished = false;
|
|
13
|
+
this._joinParams = params;
|
|
14
|
+
this._publisher = publisher;
|
|
15
|
+
this._clientState = clientState;
|
|
16
|
+
this._receiver = new subjectUtils_1.SimpleSubject();
|
|
17
|
+
this._joinState = new subjectUtils_1.SimpleBehaviorSubject(false);
|
|
18
|
+
this._presence = new subjectUtils_1.SimpleBehaviorSubject([]);
|
|
19
|
+
this._presenceSub = this._init();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @desc Connects to the channel.
|
|
23
|
+
*/
|
|
24
|
+
join() {
|
|
25
|
+
if (this._finished) {
|
|
26
|
+
throw new Error('This channel has been closed');
|
|
27
|
+
}
|
|
28
|
+
const joinMessage = {
|
|
29
|
+
action: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
30
|
+
channelName: this._name,
|
|
31
|
+
event: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
32
|
+
payload: this._joinParams,
|
|
33
|
+
};
|
|
34
|
+
this._publish(joinMessage);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @desc Disconnects from the channel.
|
|
38
|
+
*/
|
|
39
|
+
leave() {
|
|
40
|
+
const leaveMessage = {
|
|
41
|
+
action: endpoint_1.ClientActions.LEAVE_CHANNEL,
|
|
42
|
+
channelName: this._name,
|
|
43
|
+
event: endpoint_1.ClientActions.LEAVE_CHANNEL,
|
|
44
|
+
payload: {},
|
|
45
|
+
};
|
|
46
|
+
this._publish(leaveMessage);
|
|
47
|
+
this._finished = true;
|
|
48
|
+
this._presenceSub();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @desc Monitors the channel for messages.
|
|
52
|
+
* @param event - The event to monitor.
|
|
53
|
+
* @param callback - The callback to call when a message is received.
|
|
54
|
+
*/
|
|
55
|
+
onMessage(event, callback) {
|
|
56
|
+
return this._receiver.subscribe((data) => {
|
|
57
|
+
if (data.action === channelEngine_1.ServerActions.BROADCAST && data.event === event && data.channelName === this._name) {
|
|
58
|
+
return callback(data.payload);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @desc Monitors the connection state of the channel.
|
|
64
|
+
* @param callback - The callback to call when the connection state changes.
|
|
65
|
+
*/
|
|
66
|
+
onConnectionChange(callback) {
|
|
67
|
+
return this._joinState.subscribe((data) => {
|
|
68
|
+
callback(data);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @desc Detects when clients join the channel.
|
|
73
|
+
* @param callback - The callback to call when a client joins the channel.
|
|
74
|
+
*/
|
|
75
|
+
onJoin(callback) {
|
|
76
|
+
return this._receiver.subscribe((data) => {
|
|
77
|
+
if (data.action === channelEngine_1.ServerActions.PRESENCE && data.event === presenceEngine_1.PresenceEventTypes.JOIN && data.channelName === this._name) {
|
|
78
|
+
return callback(data.payload.changed);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @desc Detects when clients leave the channel.
|
|
84
|
+
* @param callback - The callback to call when a client leaves the channel.
|
|
85
|
+
*/
|
|
86
|
+
onLeave(callback) {
|
|
87
|
+
return this._receiver.subscribe((data) => {
|
|
88
|
+
if (data.action === channelEngine_1.ServerActions.PRESENCE && data.event === presenceEngine_1.PresenceEventTypes.LEAVE && data.channelName === this._name) {
|
|
89
|
+
return callback(data.payload.changed);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* @desc Detects when clients change their presence in the channel.
|
|
95
|
+
* @param callback - The callback to call when a client changes their presence in the channel.
|
|
96
|
+
*/
|
|
97
|
+
onPresenceChange(callback) {
|
|
98
|
+
return this._receiver.subscribe((data) => {
|
|
99
|
+
if (data.action === channelEngine_1.ServerActions.PRESENCE && data.event === presenceEngine_1.PresenceEventTypes.UPDATE && data.channelName === this._name) {
|
|
100
|
+
return callback(data.payload);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @desc Sends a message to specific clients in the channel.
|
|
106
|
+
* @param event - The event to send.
|
|
107
|
+
* @param payload - The message to send.
|
|
108
|
+
* @param recipient - The clients to send the message to.
|
|
109
|
+
*/
|
|
110
|
+
sendMessage(event, payload, recipient) {
|
|
111
|
+
this._send(event, payload, recipient);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
115
|
+
* @param event - The event to send.
|
|
116
|
+
* @param payload - The message to send.
|
|
117
|
+
*/
|
|
118
|
+
broadcastFrom(event, payload) {
|
|
119
|
+
this._send(event, payload, 'all_except_sender');
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @desc Broadcasts a message to the channel, including yourself.
|
|
123
|
+
* @param event - The event to send.
|
|
124
|
+
* @param payload - The message to send.
|
|
125
|
+
*/
|
|
126
|
+
broadcast(event, payload) {
|
|
127
|
+
this._send(event, payload);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @desc Gets the current connection state of the channel.
|
|
131
|
+
*/
|
|
132
|
+
isConnected() {
|
|
133
|
+
return this._joinState.value;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @desc check is the channel has been closed.
|
|
137
|
+
*/
|
|
138
|
+
hasClosed() {
|
|
139
|
+
return this._finished;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @desc Gets the current presence of the channel.
|
|
143
|
+
*/
|
|
144
|
+
getPresence() {
|
|
145
|
+
return this._presence.value;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @desc Monitors the presence of the channel.
|
|
149
|
+
* @param callback - The callback to call when the presence changes.
|
|
150
|
+
*/
|
|
151
|
+
onUsersChange(callback) {
|
|
152
|
+
return this._presence.subscribe((data) => {
|
|
153
|
+
callback(data);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
_send(event, payload, receivers = 'all_users') {
|
|
157
|
+
const message = {
|
|
158
|
+
action: endpoint_1.ClientActions.BROADCAST,
|
|
159
|
+
channelName: this._name,
|
|
160
|
+
event,
|
|
161
|
+
payload,
|
|
162
|
+
addresses: receivers,
|
|
163
|
+
};
|
|
164
|
+
this._publish(message);
|
|
165
|
+
}
|
|
166
|
+
_publish(data) {
|
|
167
|
+
if (!this._joinState.value || this._clientState.value !== 'OPEN') {
|
|
168
|
+
this._queue.push(data);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
this._publisher(data);
|
|
172
|
+
}
|
|
173
|
+
_init() {
|
|
174
|
+
const unsubStateChange = this._clientState.subscribe((state) => {
|
|
175
|
+
if (state === 'OPEN') {
|
|
176
|
+
const joinMessage = {
|
|
177
|
+
action: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
178
|
+
channelName: this._name,
|
|
179
|
+
event: endpoint_1.ClientActions.JOIN_CHANNEL,
|
|
180
|
+
payload: this._joinParams,
|
|
181
|
+
};
|
|
182
|
+
this._publisher(joinMessage);
|
|
183
|
+
this._queue.forEach((message) => {
|
|
184
|
+
this._publisher(message);
|
|
185
|
+
});
|
|
186
|
+
this._queue = [];
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const unsubPresence = this._receiver.subscribe((data) => {
|
|
190
|
+
if (data.action === channelEngine_1.ServerActions.PRESENCE && data.channelName === this._name) {
|
|
191
|
+
this._presence.publish(data.payload.presence);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return () => {
|
|
195
|
+
unsubStateChange();
|
|
196
|
+
unsubPresence();
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.Channel = Channel;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-unresolved
|
|
2
|
+
import { PondMessage, JoinParams, PondPresence } from '../index';
|
|
3
|
+
export type PondState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
|
|
4
|
+
type Unsubscribe = () => void;
|
|
5
|
+
|
|
6
|
+
export interface PresencePayload {
|
|
7
|
+
changed: PondPresence;
|
|
8
|
+
presence: PondPresence[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare class Channel {
|
|
12
|
+
/**
|
|
13
|
+
* @desc Connects to the channel.
|
|
14
|
+
*/
|
|
15
|
+
public join(): void;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @desc Disconnects from the channel.
|
|
19
|
+
*/
|
|
20
|
+
public leave(): void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @desc Monitors the channel for messages.
|
|
24
|
+
* @param event - The event to monitor.
|
|
25
|
+
* @param callback - The callback to call when a message is received.
|
|
26
|
+
*/
|
|
27
|
+
public onMessage(event: string, callback: (message: PondMessage) => void): Unsubscribe;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @desc Monitors the connection state of the channel.
|
|
31
|
+
* @param callback - The callback to call when the connection state changes.
|
|
32
|
+
*/
|
|
33
|
+
public onConnectionChange(callback: (connected: boolean) => void): Unsubscribe;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @desc Detects when clients join the channel.
|
|
37
|
+
* @param callback - The callback to call when a client joins the channel.
|
|
38
|
+
*/
|
|
39
|
+
public onJoin(callback: (presence: PondPresence) => void): Unsubscribe;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @desc Detects when clients leave the channel.
|
|
43
|
+
* @param callback - The callback to call when a client leaves the channel.
|
|
44
|
+
*/
|
|
45
|
+
public onLeave(callback: (presence: PondPresence) => void): Unsubscribe;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @desc Detects when clients change their presence in the channel.
|
|
49
|
+
* @param callback - The callback to call when a client changes their presence in the channel.
|
|
50
|
+
*/
|
|
51
|
+
public onPresenceChange(callback: (presence: PresencePayload) => void): Unsubscribe;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @desc Sends a message to specific clients in the channel.
|
|
55
|
+
* @param event - The event to send.
|
|
56
|
+
* @param payload - The message to send.
|
|
57
|
+
* @param recipient - The clients to send the message to.
|
|
58
|
+
*/
|
|
59
|
+
public sendMessage(event: string, payload: PondMessage, recipient: string[]): void;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
63
|
+
* @param event - The event to send.
|
|
64
|
+
* @param payload - The message to send.
|
|
65
|
+
*/
|
|
66
|
+
public broadcastFrom(event: string, payload: PondMessage): void;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @desc Broadcasts a message to the channel, including yourself.
|
|
70
|
+
* @param event - The event to send.
|
|
71
|
+
* @param payload - The message to send.
|
|
72
|
+
*/
|
|
73
|
+
public broadcast(event: string, payload: PondMessage): void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @desc Gets the current connection state of the channel.
|
|
77
|
+
*/
|
|
78
|
+
public isConnected(): boolean;
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @desc check is the channel has been closed.
|
|
83
|
+
*/
|
|
84
|
+
public hasClosed(): boolean;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @desc Gets the current presence of the channel.
|
|
88
|
+
*/
|
|
89
|
+
public getPresence(): PondPresence[];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @desc Monitors the presence of the channel.
|
|
93
|
+
* @param callback - The callback to call when the presence changes.
|
|
94
|
+
*/
|
|
95
|
+
public onUsersChange(callback: (users: PondPresence[]) => void): Unsubscribe;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default class PondClient {
|
|
99
|
+
constructor(endpoint: string, params?: Record<string, any>);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @desc Connects to the server and returns the socket.
|
|
103
|
+
*/
|
|
104
|
+
public connect(backoff?: number): void;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @desc Returns the current state of the socket.
|
|
108
|
+
*/
|
|
109
|
+
public getState(): PondState;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @desc Disconnects the socket.
|
|
113
|
+
*/
|
|
114
|
+
public disconnect(): void;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @desc Creates a channel with the given name and params.
|
|
118
|
+
* @param name - The name of the channel.
|
|
119
|
+
* @param params - The params to send to the server.
|
|
120
|
+
*/
|
|
121
|
+
public createChannel(name: string, params?: JoinParams): Channel;
|
|
122
|
+
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PondClient = void 0;
|
|
4
3
|
const channel_1 = require("./channel");
|
|
5
|
-
const
|
|
6
|
-
const pondBase_1 = require("../pondBase");
|
|
4
|
+
const subjectUtils_1 = require("../server/utils/subjectUtils");
|
|
7
5
|
class PondClient {
|
|
8
|
-
constructor(endpoint, params) {
|
|
6
|
+
constructor(endpoint, params = {}) {
|
|
9
7
|
let address;
|
|
10
8
|
try {
|
|
11
9
|
address = new URL(endpoint);
|
|
@@ -16,43 +14,29 @@ class PondClient {
|
|
|
16
14
|
}
|
|
17
15
|
const query = new URLSearchParams(params);
|
|
18
16
|
address.search = query.toString();
|
|
19
|
-
const protocol = address.protocol ===
|
|
20
|
-
if (address.protocol !==
|
|
17
|
+
const protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
18
|
+
if (address.protocol !== 'wss:' && address.protocol !== 'ws:') {
|
|
21
19
|
address.protocol = protocol;
|
|
20
|
+
}
|
|
22
21
|
this.address = address;
|
|
23
|
-
this._socketState = "CLOSED";
|
|
24
22
|
this._channels = {};
|
|
25
|
-
this._broadcaster = new
|
|
26
|
-
this.
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* @desc Returns the current state of the socket.
|
|
30
|
-
*/
|
|
31
|
-
getState() {
|
|
32
|
-
return this._socketState;
|
|
23
|
+
this._broadcaster = new subjectUtils_1.SimpleSubject();
|
|
24
|
+
this._connectionState = new subjectUtils_1.SimpleBehaviorSubject('CLOSED');
|
|
33
25
|
}
|
|
34
26
|
/**
|
|
35
27
|
* @desc Connects to the server and returns the socket.
|
|
36
28
|
*/
|
|
37
29
|
connect(backoff = 1) {
|
|
38
30
|
const socket = new WebSocket(this.address.toString());
|
|
39
|
-
const sub = this._receiver.subscribe((message) => {
|
|
40
|
-
socket.send(JSON.stringify(message));
|
|
41
|
-
});
|
|
42
31
|
socket.onopen = () => {
|
|
43
|
-
this.
|
|
44
|
-
};
|
|
45
|
-
socket.onclose = () => {
|
|
46
|
-
this._socketState = "CLOSED";
|
|
47
|
-
sub.unsubscribe();
|
|
32
|
+
this._connectionState.publish('OPEN');
|
|
48
33
|
};
|
|
49
34
|
socket.onmessage = (message) => {
|
|
50
35
|
const data = JSON.parse(message.data);
|
|
51
36
|
this._broadcaster.publish(data);
|
|
52
37
|
};
|
|
53
38
|
socket.onerror = () => {
|
|
54
|
-
this.
|
|
55
|
-
sub.unsubscribe();
|
|
39
|
+
this._connectionState.publish('CLOSED');
|
|
56
40
|
setTimeout(() => {
|
|
57
41
|
this.connect(backoff * 2);
|
|
58
42
|
}, backoff * 1000);
|
|
@@ -60,39 +44,41 @@ class PondClient {
|
|
|
60
44
|
this._socket = socket;
|
|
61
45
|
}
|
|
62
46
|
/**
|
|
63
|
-
* @desc
|
|
47
|
+
* @desc Returns the current state of the socket.
|
|
48
|
+
*/
|
|
49
|
+
getState() {
|
|
50
|
+
return this._connectionState.value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @desc Disconnects the socket.
|
|
64
54
|
*/
|
|
65
55
|
disconnect() {
|
|
66
56
|
var _a;
|
|
67
|
-
Object.values(this._channels).forEach(channel => channel.leave());
|
|
68
|
-
this._socketState = "CLOSED";
|
|
69
|
-
this._broadcaster.clear();
|
|
70
|
-
this._receiver.clear();
|
|
57
|
+
Object.values(this._channels).forEach((channel) => channel.leave());
|
|
71
58
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
72
59
|
this._channels = {};
|
|
73
60
|
}
|
|
74
|
-
/**
|
|
75
|
-
* @desc An event that is triggered when the socket receives a message.
|
|
76
|
-
* @param event - The event to subscribe to.
|
|
77
|
-
* @param callback - The callback to be called when the event is triggered.
|
|
78
|
-
*/
|
|
79
|
-
onMessage(event, callback) {
|
|
80
|
-
return this._broadcaster.subscribe(data => {
|
|
81
|
-
if (data.action === pondSocket_1.ServerActions.MESSAGE && data.event === event)
|
|
82
|
-
callback(data.payload);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
61
|
/**
|
|
86
62
|
* @desc Creates a channel with the given name and params.
|
|
87
63
|
* @param name - The name of the channel.
|
|
88
64
|
* @param params - The params to send to the server.
|
|
89
65
|
*/
|
|
90
66
|
createChannel(name, params) {
|
|
91
|
-
if (this._channels[name])
|
|
67
|
+
if (this._channels[name] && !this._channels[name].hasClosed()) {
|
|
92
68
|
return this._channels[name];
|
|
93
|
-
|
|
69
|
+
}
|
|
70
|
+
const publisher = this._createPublisher();
|
|
71
|
+
const channel = new channel_1.Channel(publisher, this._connectionState, name, this._broadcaster, params);
|
|
94
72
|
this._channels[name] = channel;
|
|
95
73
|
return channel;
|
|
96
74
|
}
|
|
75
|
+
_createPublisher() {
|
|
76
|
+
return (message) => {
|
|
77
|
+
var _a;
|
|
78
|
+
if (((_a = this._socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
79
|
+
this._socket.send(JSON.stringify(message));
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
97
83
|
}
|
|
98
|
-
exports.
|
|
84
|
+
exports.default = PondClient;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* eslint-disable import/no-unresolved */
|
|
2
|
+
import { Express } from 'express';
|
|
3
|
+
|
|
4
|
+
import { EndpointHandler, Endpoint, PondPath } from '../index';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
interface PondSocketExpressApp extends Express {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
|
|
12
|
+
* @param path - the pattern to accept || can also be a regex
|
|
13
|
+
* @param handler - the handler function to authenticate the socket
|
|
14
|
+
* @example
|
|
15
|
+
* const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
16
|
+
* const token = req.query.token;
|
|
17
|
+
* if (!token)
|
|
18
|
+
* return res.reject("No token provided");
|
|
19
|
+
* res.accept({
|
|
20
|
+
* assign: {
|
|
21
|
+
* token
|
|
22
|
+
* }
|
|
23
|
+
* });
|
|
24
|
+
* })
|
|
25
|
+
*/
|
|
26
|
+
upgrade(path: PondPath, handler: EndpointHandler): Endpoint;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @desc Creates a pond socket server
|
|
31
|
+
* @param app - The Express app to be used by the server
|
|
32
|
+
* @constructor
|
|
33
|
+
*/
|
|
34
|
+
declare const PondSocket: (app: Express) => PondSocketExpressApp;
|
|
35
|
+
|
|
36
|
+
export default PondSocket;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const http_1 = require("http");
|
|
4
|
+
const pondSocket_1 = require("../server/server/pondSocket");
|
|
5
|
+
/**
|
|
6
|
+
* @desc Creates a pond socket server
|
|
7
|
+
* @param app - The Express app to be used by the server
|
|
8
|
+
* @constructor
|
|
9
|
+
*/
|
|
10
|
+
const PondSocket = (app) => {
|
|
11
|
+
const server = (0, http_1.createServer)(app);
|
|
12
|
+
const pondSocket = new pondSocket_1.PondSocket(server);
|
|
13
|
+
app.upgrade = (path, handler) => pondSocket.createEndpoint(path, handler);
|
|
14
|
+
return app;
|
|
15
|
+
};
|
|
16
|
+
exports.default = PondSocket;
|