@eleven-am/pondsocket-client 0.0.18 → 0.0.20
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/browser/client.js +15 -15
- package/browser/client.test.js +64 -12
- package/node/node.js +9 -7
- package/package.json +2 -2
package/browser/client.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _PondClient_instances, _PondClient_channels,
|
|
13
|
+
var _PondClient_instances, _PondClient_channels, _PondClient_createPublisher, _PondClient_handleAcknowledge, _PondClient_init;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
16
16
|
const channel_1 = require("../core/channel");
|
|
@@ -18,7 +18,6 @@ class PondClient {
|
|
|
18
18
|
constructor(endpoint, params = {}) {
|
|
19
19
|
_PondClient_instances.add(this);
|
|
20
20
|
_PondClient_channels.set(this, void 0);
|
|
21
|
-
_PondClient_disconnecting.set(this, void 0);
|
|
22
21
|
let address;
|
|
23
22
|
try {
|
|
24
23
|
address = new URL(endpoint);
|
|
@@ -27,7 +26,7 @@ class PondClient {
|
|
|
27
26
|
address = new URL(window.location.toString());
|
|
28
27
|
address.pathname = endpoint;
|
|
29
28
|
}
|
|
30
|
-
|
|
29
|
+
this._disconnecting = false;
|
|
31
30
|
const query = new URLSearchParams(params);
|
|
32
31
|
address.search = query.toString();
|
|
33
32
|
const protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
@@ -43,25 +42,23 @@ class PondClient {
|
|
|
43
42
|
/**
|
|
44
43
|
* @desc Connects to the server and returns the socket.
|
|
45
44
|
*/
|
|
46
|
-
connect(
|
|
47
|
-
|
|
45
|
+
connect() {
|
|
46
|
+
this._disconnecting = false;
|
|
48
47
|
const socket = new WebSocket(this._address.toString());
|
|
49
|
-
socket.onopen = () => {
|
|
50
|
-
backoff = 1;
|
|
51
|
-
this._connectionState.publish(true);
|
|
52
|
-
};
|
|
53
48
|
socket.onmessage = (message) => {
|
|
54
49
|
const data = JSON.parse(message.data);
|
|
55
|
-
|
|
50
|
+
const event = pondsocket_common_1.channelEventSchema.parse(data);
|
|
51
|
+
this._broadcaster.publish(event);
|
|
56
52
|
};
|
|
53
|
+
socket.onerror = () => socket.close();
|
|
57
54
|
socket.onclose = () => {
|
|
58
55
|
this._connectionState.publish(false);
|
|
59
|
-
if (
|
|
56
|
+
if (this._disconnecting) {
|
|
60
57
|
return;
|
|
61
58
|
}
|
|
62
59
|
setTimeout(() => {
|
|
63
|
-
this.connect(
|
|
64
|
-
},
|
|
60
|
+
this.connect();
|
|
61
|
+
}, 1000);
|
|
65
62
|
};
|
|
66
63
|
this._socket = socket;
|
|
67
64
|
}
|
|
@@ -78,7 +75,7 @@ class PondClient {
|
|
|
78
75
|
var _a;
|
|
79
76
|
Object.values(__classPrivateFieldGet(this, _PondClient_channels, "f")).forEach((channel) => channel.leave());
|
|
80
77
|
this._connectionState.publish(false);
|
|
81
|
-
|
|
78
|
+
this._disconnecting = true;
|
|
82
79
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
83
80
|
__classPrivateFieldSet(this, _PondClient_channels, {}, "f");
|
|
84
81
|
}
|
|
@@ -104,7 +101,7 @@ class PondClient {
|
|
|
104
101
|
return this._connectionState.subscribe(callback);
|
|
105
102
|
}
|
|
106
103
|
}
|
|
107
|
-
_PondClient_channels = new WeakMap(),
|
|
104
|
+
_PondClient_channels = new WeakMap(), _PondClient_instances = new WeakSet(), _PondClient_createPublisher = function _PondClient_createPublisher() {
|
|
108
105
|
return (message) => {
|
|
109
106
|
if (this._connectionState.value) {
|
|
110
107
|
this._socket.send(JSON.stringify(message));
|
|
@@ -120,6 +117,9 @@ _PondClient_channels = new WeakMap(), _PondClient_disconnecting = new WeakMap(),
|
|
|
120
117
|
if (message.event === pondsocket_common_1.Events.ACKNOWLEDGE) {
|
|
121
118
|
__classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_handleAcknowledge).call(this, message);
|
|
122
119
|
}
|
|
120
|
+
else if (message.event === pondsocket_common_1.Events.CONNECTION && message.action === pondsocket_common_1.ServerActions.CONNECT) {
|
|
121
|
+
this._connectionState.publish(true);
|
|
122
|
+
}
|
|
123
123
|
});
|
|
124
124
|
};
|
|
125
125
|
exports.default = PondClient;
|
package/browser/client.test.js
CHANGED
|
@@ -36,16 +36,70 @@ describe('PondClient', () => {
|
|
|
36
36
|
test('connect method should set up WebSocket events', () => {
|
|
37
37
|
pondClient.connect();
|
|
38
38
|
const mockWebSocket = pondClient['_socket'];
|
|
39
|
-
expect(mockWebSocket.onopen).toBeInstanceOf(Function);
|
|
40
39
|
expect(mockWebSocket.onmessage).toBeInstanceOf(Function);
|
|
41
40
|
expect(mockWebSocket.onclose).toBeInstanceOf(Function);
|
|
42
41
|
});
|
|
42
|
+
test('it should publish messages received from the server', () => {
|
|
43
|
+
pondClient.connect();
|
|
44
|
+
const mockWebSocket = pondClient['_socket'];
|
|
45
|
+
const broadcasterSpy = jest.spyOn(pondClient['_broadcaster'], 'publish');
|
|
46
|
+
mockWebSocket.onmessage({
|
|
47
|
+
data: JSON.stringify({
|
|
48
|
+
action: pondsocket_common_1.ServerActions.SYSTEM,
|
|
49
|
+
channelName: 'exampleChannel',
|
|
50
|
+
requestId: '123',
|
|
51
|
+
payload: {},
|
|
52
|
+
event: 'exampleEvent',
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
55
|
+
expect(broadcasterSpy).toHaveBeenCalledTimes(1);
|
|
56
|
+
expect(broadcasterSpy).toHaveBeenCalledWith({
|
|
57
|
+
action: pondsocket_common_1.ServerActions.SYSTEM,
|
|
58
|
+
channelName: 'exampleChannel',
|
|
59
|
+
requestId: '123',
|
|
60
|
+
payload: {},
|
|
61
|
+
event: 'exampleEvent',
|
|
62
|
+
});
|
|
63
|
+
broadcasterSpy.mockClear();
|
|
64
|
+
expect(() => {
|
|
65
|
+
mockWebSocket.onmessage({ data: 'invalid json' });
|
|
66
|
+
}).toThrow();
|
|
67
|
+
expect(broadcasterSpy).not.toHaveBeenCalled();
|
|
68
|
+
broadcasterSpy.mockClear();
|
|
69
|
+
expect(() => {
|
|
70
|
+
mockWebSocket.onmessage({ data: JSON.stringify({}) });
|
|
71
|
+
}).toThrow();
|
|
72
|
+
expect(broadcasterSpy).not.toHaveBeenCalled();
|
|
73
|
+
});
|
|
74
|
+
test('socket should only pass to publish state when acknowledged event is received', () => {
|
|
75
|
+
pondClient.connect();
|
|
76
|
+
const mockWebSocket = pondClient['_socket'];
|
|
77
|
+
const mockCallback = jest.fn();
|
|
78
|
+
pondClient.onConnectionChange(mockCallback);
|
|
79
|
+
expect(mockCallback).not.toHaveBeenCalled();
|
|
80
|
+
const acknowledgeEvent = {
|
|
81
|
+
event: pondsocket_common_1.Events.CONNECTION,
|
|
82
|
+
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
83
|
+
channelName: 'exampleChannel',
|
|
84
|
+
requestId: '123',
|
|
85
|
+
payload: {},
|
|
86
|
+
};
|
|
87
|
+
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
88
|
+
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
89
|
+
});
|
|
43
90
|
test('disconnect method should close the socket and leave all channels', () => {
|
|
44
91
|
const mockCallback = jest.fn();
|
|
45
92
|
pondClient.onConnectionChange(mockCallback);
|
|
46
93
|
pondClient.connect();
|
|
47
94
|
const mockWebSocket = pondClient['_socket'];
|
|
48
|
-
|
|
95
|
+
const acknowledgeEvent = {
|
|
96
|
+
event: pondsocket_common_1.Events.CONNECTION,
|
|
97
|
+
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
98
|
+
channelName: 'exampleChannel',
|
|
99
|
+
requestId: '123',
|
|
100
|
+
payload: {},
|
|
101
|
+
};
|
|
102
|
+
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
49
103
|
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
50
104
|
const channel = pondClient.createChannel('exampleChannel');
|
|
51
105
|
const spyOnLeave = jest.spyOn(channel, 'leave');
|
|
@@ -83,7 +137,14 @@ describe('PondClient', () => {
|
|
|
83
137
|
pondClient.connect();
|
|
84
138
|
const mockWebSocket = pondClient['_socket'];
|
|
85
139
|
const channel = pondClient.createChannel('exampleChannel');
|
|
86
|
-
|
|
140
|
+
const acknowledgeEvent = {
|
|
141
|
+
event: pondsocket_common_1.Events.CONNECTION,
|
|
142
|
+
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
143
|
+
channelName: 'exampleChannel',
|
|
144
|
+
requestId: '123',
|
|
145
|
+
payload: {},
|
|
146
|
+
};
|
|
147
|
+
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
87
148
|
channel.join();
|
|
88
149
|
expect(mockWebSocket.send).toHaveBeenCalledTimes(1);
|
|
89
150
|
const sentObject = mockWebSocket.send.mock.calls[0][0];
|
|
@@ -110,13 +171,4 @@ describe('PondClient', () => {
|
|
|
110
171
|
yield new Promise((resolve) => setTimeout(resolve, 2000));
|
|
111
172
|
expect(connectSpy).toHaveBeenCalledTimes(1);
|
|
112
173
|
}));
|
|
113
|
-
test('it should publish messages received from the server', () => {
|
|
114
|
-
pondClient.connect();
|
|
115
|
-
const mockWebSocket = pondClient['_socket'];
|
|
116
|
-
const broadcasterSpy = jest.spyOn(pondClient['_broadcaster'], 'publish');
|
|
117
|
-
mockWebSocket.onopen();
|
|
118
|
-
mockWebSocket.onmessage({ data: JSON.stringify({ event: 'exampleEvent' }) });
|
|
119
|
-
expect(broadcasterSpy).toHaveBeenCalledTimes(1);
|
|
120
|
-
expect(broadcasterSpy).toHaveBeenCalledWith({ event: 'exampleEvent' });
|
|
121
|
-
});
|
|
122
174
|
});
|
package/node/node.js
CHANGED
|
@@ -11,21 +11,23 @@ class PondClient extends client_1.default {
|
|
|
11
11
|
* @desc Connects to the server and returns the socket.
|
|
12
12
|
*/
|
|
13
13
|
connect(backoff = 1) {
|
|
14
|
+
this._disconnecting = false;
|
|
14
15
|
const socket = new WebSocket(this._address.toString());
|
|
15
|
-
socket.onopen = () =>
|
|
16
|
-
this._connectionState.publish(true);
|
|
17
|
-
};
|
|
16
|
+
socket.onopen = () => this._connectionState.publish(true);
|
|
18
17
|
socket.onmessage = (message) => {
|
|
19
18
|
const data = JSON.parse(message.data);
|
|
20
19
|
this._broadcaster.publish(data);
|
|
21
20
|
};
|
|
22
|
-
socket.onerror = () =>
|
|
21
|
+
socket.onerror = () => socket.close();
|
|
22
|
+
socket.onclose = () => {
|
|
23
23
|
this._connectionState.publish(false);
|
|
24
|
+
if (this._disconnecting) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
24
27
|
setTimeout(() => {
|
|
25
|
-
this.connect(
|
|
26
|
-
},
|
|
28
|
+
this.connect();
|
|
29
|
+
}, 1000);
|
|
27
30
|
};
|
|
28
|
-
this._socket = socket;
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
exports.default = PondClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"pipeline": "npm run test && npm run build && npm run push"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eleven-am/pondsocket-common": "^0.0.
|
|
32
|
+
"@eleven-am/pondsocket-common": "^0.0.20",
|
|
33
33
|
"websocket": "^1.0.34"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|