@eleven-am/pondsocket 0.1.8 → 0.1.9
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/package.json +1 -2
- package/pondBase/baseClass.d.ts +3 -1
- package/pondBase/baseClass.js +51 -32
- package/pondBase/baseClass.test.js +34 -34
- package/pondBase/pondBase.js +95 -34
- package/pondBase/pondBase.test.js +67 -39
- package/pondBase/pubSub.d.ts +10 -5
- package/pondBase/pubSub.js +127 -57
- package/pondBase/pubSub.test.js +120 -106
- package/pondBase/simpleBase.d.ts +0 -5
- package/pondBase/simpleBase.js +166 -92
- package/pondBase/simpleBase.test.js +124 -66
- package/pondClient/channel.d.ts +19 -19
- package/pondClient/channel.js +79 -100
- package/pondClient/socket.d.ts +13 -14
- package/pondClient/socket.js +58 -89
- package/pondSocket/channel.js +174 -121
- package/pondSocket/channel.test.js +89 -78
- package/pondSocket/channelMiddleWare.js +43 -16
- package/pondSocket/endpoint.js +188 -96
- package/pondSocket/endpoint.test.js +641 -479
- package/pondSocket/pondChannel.js +142 -89
- package/pondSocket/pondChannel.test.js +40 -40
- package/pondSocket/pondResponse.js +87 -52
- package/pondSocket/pondSocket.js +55 -35
- package/pondSocket/server.test.js +151 -88
- package/pondSocket/socketMiddleWare.js +16 -14
package/pondClient/channel.d.ts
CHANGED
|
@@ -1,34 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {ClientMessage, PondAssigns, PondMessage, PondPresence, ServerMessage} from "../pondSocket";
|
|
2
|
+
import {Broadcast, Subscription} from "../pondBase";
|
|
3
3
|
|
|
4
4
|
export declare type ChannelParams = PondAssigns;
|
|
5
5
|
|
|
6
6
|
export declare class Channel {
|
|
7
|
-
readonly channel: string;
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
constructor(name: string, receiver: Broadcast<ServerMessage, void>, broadcaster: Broadcast<ClientMessage, void>, params?: ChannelParams);
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* @desc Connects to the channel.
|
|
13
12
|
*/
|
|
14
|
-
join():
|
|
13
|
+
join(): void;
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* @desc Disconnects from the channel.
|
|
18
17
|
*/
|
|
19
18
|
leave(): void;
|
|
20
19
|
|
|
21
|
-
/**
|
|
22
|
-
* @desc Monitors the presence state of the channel.
|
|
23
|
-
* @param callback - The callback to call when the presence state changes.
|
|
24
|
-
*/
|
|
25
|
-
onPresenceUpdate(callback: (presence: PondPresence[]) => void): Subscription;
|
|
26
|
-
|
|
27
20
|
/**
|
|
28
21
|
* @desc Monitors the channel for messages.
|
|
22
|
+
* @param event - The event to monitor.
|
|
29
23
|
* @param callback - The callback to call when a message is received.
|
|
30
24
|
*/
|
|
31
|
-
onMessage(
|
|
25
|
+
onMessage(event: string, callback: (message: PondMessage) => void): Subscription;
|
|
32
26
|
|
|
33
27
|
/**
|
|
34
28
|
* @desc Broadcasts a message to the channel, including yourself.
|
|
@@ -44,6 +38,14 @@ export declare class Channel {
|
|
|
44
38
|
*/
|
|
45
39
|
broadcastFrom(event: string, payload: PondMessage): void;
|
|
46
40
|
|
|
41
|
+
/**
|
|
42
|
+
* @desc Sends a message to specific clients in the channel.
|
|
43
|
+
* @param event - The event to send.
|
|
44
|
+
* @param payload - The message to send.
|
|
45
|
+
* @param recipient - The clients to send the message to.
|
|
46
|
+
*/
|
|
47
|
+
sendMessage(event: string, payload: PondMessage, recipient: string[]): void;
|
|
48
|
+
|
|
47
49
|
/**
|
|
48
50
|
* @desc Updates the presence state of the current client in the channel.
|
|
49
51
|
* @param presence - The presence state to update.
|
|
@@ -51,16 +53,14 @@ export declare class Channel {
|
|
|
51
53
|
updatePresence(presence: PondPresence): void;
|
|
52
54
|
|
|
53
55
|
/**
|
|
54
|
-
* @desc
|
|
55
|
-
* @param
|
|
56
|
-
* @param payload - The message to send.
|
|
57
|
-
* @param recipient - The clients to send the message to.
|
|
56
|
+
* @desc Monitors the presence state of the channel.
|
|
57
|
+
* @param callback - The callback to call when the presence state changes.
|
|
58
58
|
*/
|
|
59
|
-
|
|
59
|
+
onPresence(callback: (change: PondPresence | null, presence: PondPresence[]) => void): Subscription;
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* @desc
|
|
62
|
+
* @desc Monitors the connection state of the channel.
|
|
63
63
|
* @param callback - The callback to call when the connection state changes.
|
|
64
64
|
*/
|
|
65
|
-
onConnectionChange(callback: (connected: boolean) => void):
|
|
65
|
+
onConnectionChange(callback: (connected: boolean) => void): Subscription;
|
|
66
66
|
}
|
package/pondClient/channel.js
CHANGED
|
@@ -1,77 +1,72 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Channel = void 0;
|
|
4
|
-
var rxjs_1 = require("rxjs");
|
|
5
|
-
var operators_1 = require("rxjs/operators");
|
|
6
4
|
var pondSocket_1 = require("../pondSocket");
|
|
7
5
|
var pondBase_1 = require("../pondBase");
|
|
8
6
|
var Channel = /** @class */ (function () {
|
|
9
|
-
function Channel(
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
7
|
+
function Channel(name, receiver, broadcaster, params) {
|
|
8
|
+
var _this = this;
|
|
9
|
+
this._name = name;
|
|
10
|
+
this._joinParams = params || {};
|
|
11
|
+
this._broadcaster = broadcaster;
|
|
12
|
+
this._connection = new pondBase_1.Subject(false);
|
|
13
|
+
this._receiver = new pondBase_1.Broadcast();
|
|
14
|
+
this._subscription = receiver.subscribe(function (data) {
|
|
15
|
+
if (data.channelName === name) {
|
|
16
|
+
_this._receiver.publish(data);
|
|
17
|
+
_this._connection.publish(true);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
this._presence = new pondBase_1.Subject({
|
|
21
|
+
change: null,
|
|
22
|
+
presence: []
|
|
23
|
+
});
|
|
17
24
|
}
|
|
18
|
-
Object.defineProperty(Channel.prototype, "isActive", {
|
|
19
|
-
get: function () {
|
|
20
|
-
return this._connectedSubject.value;
|
|
21
|
-
},
|
|
22
|
-
enumerable: false,
|
|
23
|
-
configurable: true
|
|
24
|
-
});
|
|
25
25
|
/**
|
|
26
26
|
* @desc Connects to the channel.
|
|
27
27
|
*/
|
|
28
28
|
Channel.prototype.join = function () {
|
|
29
29
|
var _this = this;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
else if (message.event === "KICKED_FROM_CHANNEL")
|
|
41
|
-
_this.leave();
|
|
30
|
+
var joinMessage = {
|
|
31
|
+
action: pondSocket_1.ClientActions.JOIN_CHANNEL,
|
|
32
|
+
channelName: this._name,
|
|
33
|
+
event: pondSocket_1.ClientActions.JOIN_CHANNEL,
|
|
34
|
+
payload: this._joinParams
|
|
35
|
+
};
|
|
36
|
+
this._receiver.subscribe(function (data) {
|
|
37
|
+
if (data.action === pondSocket_1.ServerActions.PRESENCE) {
|
|
38
|
+
_this._presence.publish(data.payload);
|
|
39
|
+
}
|
|
42
40
|
});
|
|
43
|
-
this.
|
|
44
|
-
return this;
|
|
41
|
+
this._broadcaster.publish(joinMessage);
|
|
45
42
|
};
|
|
46
43
|
/**
|
|
47
44
|
* @desc Disconnects from the channel.
|
|
48
45
|
*/
|
|
49
46
|
Channel.prototype.leave = function () {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
this._subscriptions.push(sub);
|
|
63
|
-
return sub;
|
|
47
|
+
var leaveMessage = {
|
|
48
|
+
action: pondSocket_1.ClientActions.LEAVE_CHANNEL,
|
|
49
|
+
channelName: this._name,
|
|
50
|
+
event: pondSocket_1.ClientActions.LEAVE_CHANNEL,
|
|
51
|
+
payload: {}
|
|
52
|
+
};
|
|
53
|
+
this._broadcaster.publish(leaveMessage);
|
|
54
|
+
this._connection.publish(false);
|
|
55
|
+
this._subscription.unsubscribe();
|
|
56
|
+
this._connection.clear();
|
|
57
|
+
this._receiver.clear();
|
|
58
|
+
this._presence.clear();
|
|
64
59
|
};
|
|
65
60
|
/**
|
|
66
61
|
* @desc Monitors the channel for messages.
|
|
62
|
+
* @param event - The event to monitor.
|
|
67
63
|
* @param callback - The callback to call when a message is received.
|
|
68
64
|
*/
|
|
69
|
-
Channel.prototype.onMessage = function (callback) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return sub;
|
|
65
|
+
Channel.prototype.onMessage = function (event, callback) {
|
|
66
|
+
return this._receiver.subscribe(function (data) {
|
|
67
|
+
if (data.action === pondSocket_1.ServerActions.MESSAGE && data.event === event)
|
|
68
|
+
callback(data.payload);
|
|
69
|
+
});
|
|
75
70
|
};
|
|
76
71
|
/**
|
|
77
72
|
* @desc Broadcasts a message to the channel, including yourself.
|
|
@@ -80,12 +75,9 @@ var Channel = /** @class */ (function () {
|
|
|
80
75
|
*/
|
|
81
76
|
Channel.prototype.broadcast = function (event, payload) {
|
|
82
77
|
var message = {
|
|
83
|
-
channelName: this.
|
|
84
|
-
payload: payload,
|
|
85
|
-
event: event,
|
|
86
|
-
action: pondSocket_1.ClientActions.BROADCAST
|
|
78
|
+
action: pondSocket_1.ClientActions.BROADCAST, channelName: this._name, payload: payload, event: event
|
|
87
79
|
};
|
|
88
|
-
this.
|
|
80
|
+
this._broadcaster.publish(message);
|
|
89
81
|
};
|
|
90
82
|
/**
|
|
91
83
|
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
@@ -94,24 +86,9 @@ var Channel = /** @class */ (function () {
|
|
|
94
86
|
*/
|
|
95
87
|
Channel.prototype.broadcastFrom = function (event, payload) {
|
|
96
88
|
var message = {
|
|
97
|
-
channelName: this.
|
|
98
|
-
payload: payload,
|
|
99
|
-
event: event,
|
|
100
|
-
action: pondSocket_1.ClientActions.BROADCAST_FROM
|
|
89
|
+
action: pondSocket_1.ClientActions.BROADCAST_FROM, channelName: this._name, payload: payload, event: event
|
|
101
90
|
};
|
|
102
|
-
this.
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* @desc Updates the presence state of the current client in the channel.
|
|
106
|
-
* @param presence - The presence state to update.
|
|
107
|
-
*/
|
|
108
|
-
Channel.prototype.updatePresence = function (presence) {
|
|
109
|
-
this._socket.next({
|
|
110
|
-
action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
|
|
111
|
-
channelName: this.channel,
|
|
112
|
-
event: "PRESENCE",
|
|
113
|
-
payload: presence
|
|
114
|
-
});
|
|
91
|
+
this._broadcaster.publish(message);
|
|
115
92
|
};
|
|
116
93
|
/**
|
|
117
94
|
* @desc Sends a message to specific clients in the channel.
|
|
@@ -120,43 +97,45 @@ var Channel = /** @class */ (function () {
|
|
|
120
97
|
* @param recipient - The clients to send the message to.
|
|
121
98
|
*/
|
|
122
99
|
Channel.prototype.sendMessage = function (event, payload, recipient) {
|
|
123
|
-
var addresses = Array.isArray(recipient) ? recipient : [recipient];
|
|
124
100
|
var message = {
|
|
125
|
-
|
|
101
|
+
action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER,
|
|
102
|
+
channelName: this._name,
|
|
126
103
|
payload: payload,
|
|
127
104
|
event: event,
|
|
128
|
-
addresses:
|
|
129
|
-
action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER
|
|
105
|
+
addresses: recipient
|
|
130
106
|
};
|
|
131
|
-
this.
|
|
107
|
+
this._broadcaster.publish(message);
|
|
132
108
|
};
|
|
133
109
|
/**
|
|
134
|
-
* @desc
|
|
135
|
-
* @param
|
|
110
|
+
* @desc Updates the presence state of the current client in the channel.
|
|
111
|
+
* @param presence - The presence state to update.
|
|
136
112
|
*/
|
|
137
|
-
Channel.prototype.
|
|
138
|
-
var
|
|
139
|
-
|
|
140
|
-
|
|
113
|
+
Channel.prototype.updatePresence = function (presence) {
|
|
114
|
+
var message = {
|
|
115
|
+
action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
|
|
116
|
+
channelName: this._name,
|
|
117
|
+
payload: presence,
|
|
118
|
+
event: pondSocket_1.ClientActions.UPDATE_PRESENCE
|
|
119
|
+
};
|
|
120
|
+
this._broadcaster.publish(message);
|
|
141
121
|
};
|
|
142
122
|
/**
|
|
143
|
-
* @desc
|
|
144
|
-
* @
|
|
123
|
+
* @desc Monitors the presence state of the channel.
|
|
124
|
+
* @param callback - The callback to call when the presence state changes.
|
|
145
125
|
*/
|
|
146
|
-
Channel.prototype.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
});
|
|
159
|
-
return observable;
|
|
126
|
+
Channel.prototype.onPresence = function (callback) {
|
|
127
|
+
return this._presence.subscribe(function (data) {
|
|
128
|
+
callback(data.change, data.presence);
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* @desc Monitors the connection state of the channel.
|
|
133
|
+
* @param callback - The callback to call when the connection state changes.
|
|
134
|
+
*/
|
|
135
|
+
Channel.prototype.onConnectionChange = function (callback) {
|
|
136
|
+
return this._connection.subscribe(function (data) {
|
|
137
|
+
callback(data);
|
|
138
|
+
});
|
|
160
139
|
};
|
|
161
140
|
return Channel;
|
|
162
141
|
}());
|
package/pondClient/socket.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Channel, ChannelParams} from "./channel";
|
|
2
|
-
import {
|
|
2
|
+
import {PondMessage} from "../pondSocket";
|
|
3
3
|
|
|
4
4
|
declare type PondParams = {
|
|
5
5
|
[key: string]: string;
|
|
@@ -11,32 +11,31 @@ export declare class PondClient {
|
|
|
11
11
|
constructor(endpoint: string, params?: PondParams);
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* @desc
|
|
14
|
+
* @desc Returns the current state of the socket.
|
|
15
15
|
*/
|
|
16
|
-
|
|
16
|
+
getState(): PondState;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* @desc
|
|
19
|
+
* @desc Connects to the server and returns the socket.
|
|
20
20
|
*/
|
|
21
|
-
|
|
21
|
+
connect(backoff?: number): void;
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* @desc
|
|
25
|
-
* @param channel - The name of the channel.
|
|
26
|
-
* @param params - The params to send to the server.
|
|
24
|
+
* @desc Disconnects the socket from the server.
|
|
27
25
|
*/
|
|
28
|
-
|
|
26
|
+
disconnect(): void;
|
|
29
27
|
|
|
30
28
|
/**
|
|
31
29
|
* @desc An event that is triggered when the socket receives a message.
|
|
30
|
+
* @param event - The event to subscribe to.
|
|
32
31
|
* @param callback - The callback to be called when the event is triggered.
|
|
33
32
|
*/
|
|
34
|
-
onMessage(
|
|
33
|
+
onMessage(event: string, callback: (message: PondMessage) => void): import("../pondBase").Subscription;
|
|
35
34
|
|
|
36
35
|
/**
|
|
37
|
-
* @desc
|
|
36
|
+
* @desc Creates a channel with the given name and params.
|
|
37
|
+
* @param name - The name of the channel.
|
|
38
|
+
* @param params - The params to send to the server.
|
|
38
39
|
*/
|
|
39
|
-
|
|
40
|
+
createChannel(name: string, params?: ChannelParams): Channel;
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
-
export {};
|
package/pondClient/socket.js
CHANGED
|
@@ -1,47 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.PondClient = void 0;
|
|
20
|
-
var webSocket_1 = require("rxjs/webSocket");
|
|
21
|
-
var rxjs_1 = require("rxjs");
|
|
22
4
|
var channel_1 = require("./channel");
|
|
23
|
-
var operators_1 = require("rxjs/operators");
|
|
24
5
|
var pondBase_1 = require("../pondBase");
|
|
6
|
+
var pondSocket_1 = require("../pondSocket");
|
|
25
7
|
var PondClient = /** @class */ (function () {
|
|
26
8
|
function PondClient(endpoint, params) {
|
|
27
|
-
this.socketState = "CLOSED";
|
|
28
|
-
/**
|
|
29
|
-
* @desc A retry strategy for the socket.
|
|
30
|
-
* @param maxTries - The maximum number of retries.
|
|
31
|
-
* @param ms - The number of milliseconds to wait before retrying.
|
|
32
|
-
*/
|
|
33
|
-
this._retryStrategy = function (maxTries, ms) {
|
|
34
|
-
return (0, rxjs_1.pipe)((0, operators_1.retryWhen)(function (attempts) {
|
|
35
|
-
var observableForRetries = (0, rxjs_1.zip)((0, rxjs_1.range)(1, maxTries), attempts)
|
|
36
|
-
.pipe((0, operators_1.map)(function (_a) {
|
|
37
|
-
var _b = __read(_a, 1), elemFromRange = _b[0];
|
|
38
|
-
return elemFromRange;
|
|
39
|
-
}), (0, operators_1.map)(function (i) { return i * i; }), (0, rxjs_1.switchMap)(function (i) { return (0, rxjs_1.timer)(i * ms); }));
|
|
40
|
-
var observableForFailure = (0, rxjs_1.throwError)(new Error("Could not connect to server"))
|
|
41
|
-
.pipe((0, rxjs_1.materialize)(), (0, rxjs_1.delay)(1000), (0, rxjs_1.dematerialize)());
|
|
42
|
-
return (0, rxjs_1.concat)(observableForRetries, observableForFailure);
|
|
43
|
-
}));
|
|
44
|
-
};
|
|
45
9
|
var address;
|
|
46
10
|
try {
|
|
47
11
|
address = new URL(endpoint);
|
|
@@ -56,76 +20,81 @@ var PondClient = /** @class */ (function () {
|
|
|
56
20
|
if (address.protocol !== "wss:" && address.protocol !== "ws:")
|
|
57
21
|
address.protocol = protocol;
|
|
58
22
|
this.address = address;
|
|
59
|
-
this.
|
|
23
|
+
this._socketState = "CLOSED";
|
|
24
|
+
this._channels = {};
|
|
25
|
+
this._broadcaster = new pondBase_1.Broadcast();
|
|
26
|
+
this._receiver = new pondBase_1.Broadcast();
|
|
60
27
|
}
|
|
61
|
-
/**
|
|
62
|
-
* @desc Connects to the server and returns the socket.
|
|
63
|
-
*/
|
|
64
|
-
PondClient.prototype.connect = function () {
|
|
65
|
-
var _this = this;
|
|
66
|
-
if (this.socketState !== "CLOSED")
|
|
67
|
-
return;
|
|
68
|
-
this.socketState = "CONNECTING";
|
|
69
|
-
var socket = (0, webSocket_1.webSocket)({
|
|
70
|
-
url: this.address.toString(),
|
|
71
|
-
openObserver: {
|
|
72
|
-
next: function () {
|
|
73
|
-
_this.socketState = "OPEN";
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
closeObserver: {
|
|
77
|
-
next: function () {
|
|
78
|
-
_this.socketState = "CLOSED";
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
closingObserver: {
|
|
82
|
-
next: function () {
|
|
83
|
-
_this.socketState = "CLOSING";
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
this.socket = socket;
|
|
88
|
-
this.subscription = socket.pipe(this._retryStrategy(100, 1000)).subscribe();
|
|
89
|
-
return this;
|
|
90
|
-
};
|
|
91
28
|
/**
|
|
92
29
|
* @desc Returns the current state of the socket.
|
|
93
30
|
*/
|
|
94
31
|
PondClient.prototype.getState = function () {
|
|
95
|
-
return this.
|
|
32
|
+
return this._socketState;
|
|
96
33
|
};
|
|
97
34
|
/**
|
|
98
|
-
* @desc
|
|
99
|
-
* @param channel - The name of the channel.
|
|
100
|
-
* @param params - The params to send to the server.
|
|
35
|
+
* @desc Connects to the server and returns the socket.
|
|
101
36
|
*/
|
|
102
|
-
PondClient.prototype.
|
|
37
|
+
PondClient.prototype.connect = function (backoff) {
|
|
103
38
|
var _this = this;
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
39
|
+
if (backoff === void 0) { backoff = 1; }
|
|
40
|
+
var socket = new WebSocket(this.address.toString());
|
|
41
|
+
var sub = this._receiver.subscribe(function (message) {
|
|
42
|
+
socket.send(JSON.stringify(message));
|
|
43
|
+
});
|
|
44
|
+
socket.onopen = function () {
|
|
45
|
+
_this._socketState = "OPEN";
|
|
46
|
+
};
|
|
47
|
+
socket.onclose = function () {
|
|
48
|
+
_this._socketState = "CLOSED";
|
|
49
|
+
sub.unsubscribe();
|
|
50
|
+
};
|
|
51
|
+
socket.onmessage = function (message) {
|
|
52
|
+
var data = JSON.parse(message.data);
|
|
53
|
+
_this._broadcaster.publish(data);
|
|
54
|
+
};
|
|
55
|
+
socket.onerror = function () {
|
|
56
|
+
_this._socketState = "CLOSED";
|
|
57
|
+
sub.unsubscribe();
|
|
58
|
+
setTimeout(function () {
|
|
59
|
+
_this.connect(backoff * 2);
|
|
60
|
+
}, backoff * 1000);
|
|
61
|
+
};
|
|
62
|
+
this._socket = socket;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* @desc Disconnects the socket from the server.
|
|
66
|
+
*/
|
|
67
|
+
PondClient.prototype.disconnect = function () {
|
|
68
|
+
var _a;
|
|
69
|
+
Object.values(this._channels).forEach(function (channel) { return channel.leave(); });
|
|
70
|
+
this._socketState = "CLOSED";
|
|
71
|
+
this._broadcaster.clear();
|
|
72
|
+
this._receiver.clear();
|
|
73
|
+
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
74
|
+
this._channels = {};
|
|
107
75
|
};
|
|
108
76
|
/**
|
|
109
77
|
* @desc An event that is triggered when the socket receives a message.
|
|
78
|
+
* @param event - The event to subscribe to.
|
|
110
79
|
* @param callback - The callback to be called when the event is triggered.
|
|
111
80
|
*/
|
|
112
|
-
PondClient.prototype.onMessage = function (callback) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
callback(data.event, data.payload);
|
|
81
|
+
PondClient.prototype.onMessage = function (event, callback) {
|
|
82
|
+
return this._broadcaster.subscribe(function (data) {
|
|
83
|
+
if (data.action === pondSocket_1.ServerActions.MESSAGE && data.event === event)
|
|
84
|
+
callback(data.payload);
|
|
117
85
|
});
|
|
118
86
|
};
|
|
119
87
|
/**
|
|
120
|
-
* @desc
|
|
88
|
+
* @desc Creates a channel with the given name and params.
|
|
89
|
+
* @param name - The name of the channel.
|
|
90
|
+
* @param params - The params to send to the server.
|
|
121
91
|
*/
|
|
122
|
-
PondClient.prototype.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
this.channels = new pondBase_1.PondBase();
|
|
92
|
+
PondClient.prototype.createChannel = function (name, params) {
|
|
93
|
+
if (this._channels[name])
|
|
94
|
+
return this._channels[name];
|
|
95
|
+
var channel = new channel_1.Channel(name, this._broadcaster, this._receiver, params);
|
|
96
|
+
this._channels[name] = channel;
|
|
97
|
+
return channel;
|
|
129
98
|
};
|
|
130
99
|
return PondClient;
|
|
131
100
|
}());
|