@eleven-am/pondsocket-client 0.0.17 → 0.0.19
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 +13 -13
- package/browser/client.test.js +40 -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,24 +42,22 @@ 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
|
-
this._connectionState.publish(true);
|
|
51
|
-
};
|
|
52
48
|
socket.onmessage = (message) => {
|
|
53
49
|
const data = JSON.parse(message.data);
|
|
54
50
|
this._broadcaster.publish(data);
|
|
55
51
|
};
|
|
52
|
+
socket.onerror = () => socket.close();
|
|
56
53
|
socket.onclose = () => {
|
|
57
54
|
this._connectionState.publish(false);
|
|
58
|
-
if (
|
|
55
|
+
if (this._disconnecting) {
|
|
59
56
|
return;
|
|
60
57
|
}
|
|
61
58
|
setTimeout(() => {
|
|
62
|
-
this.connect(
|
|
63
|
-
},
|
|
59
|
+
this.connect();
|
|
60
|
+
}, 1000);
|
|
64
61
|
};
|
|
65
62
|
this._socket = socket;
|
|
66
63
|
}
|
|
@@ -77,7 +74,7 @@ class PondClient {
|
|
|
77
74
|
var _a;
|
|
78
75
|
Object.values(__classPrivateFieldGet(this, _PondClient_channels, "f")).forEach((channel) => channel.leave());
|
|
79
76
|
this._connectionState.publish(false);
|
|
80
|
-
|
|
77
|
+
this._disconnecting = true;
|
|
81
78
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
82
79
|
__classPrivateFieldSet(this, _PondClient_channels, {}, "f");
|
|
83
80
|
}
|
|
@@ -103,7 +100,7 @@ class PondClient {
|
|
|
103
100
|
return this._connectionState.subscribe(callback);
|
|
104
101
|
}
|
|
105
102
|
}
|
|
106
|
-
_PondClient_channels = new WeakMap(),
|
|
103
|
+
_PondClient_channels = new WeakMap(), _PondClient_instances = new WeakSet(), _PondClient_createPublisher = function _PondClient_createPublisher() {
|
|
107
104
|
return (message) => {
|
|
108
105
|
if (this._connectionState.value) {
|
|
109
106
|
this._socket.send(JSON.stringify(message));
|
|
@@ -119,6 +116,9 @@ _PondClient_channels = new WeakMap(), _PondClient_disconnecting = new WeakMap(),
|
|
|
119
116
|
if (message.event === pondsocket_common_1.Events.ACKNOWLEDGE) {
|
|
120
117
|
__classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_handleAcknowledge).call(this, message);
|
|
121
118
|
}
|
|
119
|
+
else if (message.event === pondsocket_common_1.Events.CONNECTION && message.action === pondsocket_common_1.ServerActions.CONNECT) {
|
|
120
|
+
this._connectionState.publish(true);
|
|
121
|
+
}
|
|
122
122
|
});
|
|
123
123
|
};
|
|
124
124
|
exports.default = PondClient;
|
package/browser/client.test.js
CHANGED
|
@@ -36,16 +36,46 @@ 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({ data: JSON.stringify({ event: 'exampleEvent' }) });
|
|
47
|
+
expect(broadcasterSpy).toHaveBeenCalledTimes(1);
|
|
48
|
+
expect(broadcasterSpy).toHaveBeenCalledWith({ event: 'exampleEvent' });
|
|
49
|
+
});
|
|
50
|
+
test('socket should only pass to publish state when acknowledged event is received', () => {
|
|
51
|
+
pondClient.connect();
|
|
52
|
+
const mockWebSocket = pondClient['_socket'];
|
|
53
|
+
const mockCallback = jest.fn();
|
|
54
|
+
pondClient.onConnectionChange(mockCallback);
|
|
55
|
+
expect(mockCallback).not.toHaveBeenCalled();
|
|
56
|
+
const acknowledgeEvent = {
|
|
57
|
+
event: pondsocket_common_1.Events.CONNECTION,
|
|
58
|
+
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
59
|
+
channelName: 'exampleChannel',
|
|
60
|
+
requestId: '123',
|
|
61
|
+
payload: {},
|
|
62
|
+
};
|
|
63
|
+
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
64
|
+
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
65
|
+
});
|
|
43
66
|
test('disconnect method should close the socket and leave all channels', () => {
|
|
44
67
|
const mockCallback = jest.fn();
|
|
45
68
|
pondClient.onConnectionChange(mockCallback);
|
|
46
69
|
pondClient.connect();
|
|
47
70
|
const mockWebSocket = pondClient['_socket'];
|
|
48
|
-
|
|
71
|
+
const acknowledgeEvent = {
|
|
72
|
+
event: pondsocket_common_1.Events.CONNECTION,
|
|
73
|
+
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
74
|
+
channelName: 'exampleChannel',
|
|
75
|
+
requestId: '123',
|
|
76
|
+
payload: {},
|
|
77
|
+
};
|
|
78
|
+
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
49
79
|
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
50
80
|
const channel = pondClient.createChannel('exampleChannel');
|
|
51
81
|
const spyOnLeave = jest.spyOn(channel, 'leave');
|
|
@@ -83,7 +113,14 @@ describe('PondClient', () => {
|
|
|
83
113
|
pondClient.connect();
|
|
84
114
|
const mockWebSocket = pondClient['_socket'];
|
|
85
115
|
const channel = pondClient.createChannel('exampleChannel');
|
|
86
|
-
|
|
116
|
+
const acknowledgeEvent = {
|
|
117
|
+
event: pondsocket_common_1.Events.CONNECTION,
|
|
118
|
+
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
119
|
+
channelName: 'exampleChannel',
|
|
120
|
+
requestId: '123',
|
|
121
|
+
payload: {},
|
|
122
|
+
};
|
|
123
|
+
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
87
124
|
channel.join();
|
|
88
125
|
expect(mockWebSocket.send).toHaveBeenCalledTimes(1);
|
|
89
126
|
const sentObject = mockWebSocket.send.mock.calls[0][0];
|
|
@@ -110,13 +147,4 @@ describe('PondClient', () => {
|
|
|
110
147
|
yield new Promise((resolve) => setTimeout(resolve, 2000));
|
|
111
148
|
expect(connectSpy).toHaveBeenCalledTimes(1);
|
|
112
149
|
}));
|
|
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
150
|
});
|
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.19",
|
|
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.18",
|
|
33
33
|
"websocket": "^1.0.34"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|