@eleven-am/pondsocket 0.1.162 → 0.1.163

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/lobby/lobby.js CHANGED
@@ -10,13 +10,15 @@ 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 _LobbyEngine_channels, _LobbyEngine_middleware, _LobbyEngine_leaveCallback, _LobbyEngine_parentEngine, _PondChannel_lobby;
13
+ var _LobbyEngine_instances, _LobbyEngine_channels, _LobbyEngine_middleware, _LobbyEngine_leaveCallback, _LobbyEngine_parentEngine, _LobbyEngine_performAction, _PondChannel_lobby;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.PondChannel = exports.LobbyEngine = void 0;
16
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
16
17
  const middleware_1 = require("../abstracts/middleware");
17
18
  const channel_1 = require("../channel/channel");
18
19
  class LobbyEngine {
19
20
  constructor(endpointEngine) {
21
+ _LobbyEngine_instances.add(this);
20
22
  _LobbyEngine_channels.set(this, void 0);
21
23
  _LobbyEngine_middleware.set(this, void 0);
22
24
  _LobbyEngine_leaveCallback.set(this, void 0);
@@ -99,9 +101,51 @@ class LobbyEngine {
99
101
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").add(newChannel);
100
102
  return newChannel;
101
103
  }
104
+ /**
105
+ * @desc Sends a message to all clients in the channel
106
+ * @param channelName - the name of the channel to send the message to
107
+ * @param event - the event to send
108
+ * @param payload - the payload to send
109
+ */
110
+ broadcast(channelName, event, payload) {
111
+ __classPrivateFieldGet(this, _LobbyEngine_instances, "m", _LobbyEngine_performAction).call(this, channelName, (channel) => {
112
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
113
+ });
114
+ }
115
+ /**
116
+ * @desc Sends a message to all clients in the channel except the client making the request
117
+ * @param channelName - the name of the channel to send the message to
118
+ * @param event - the event to send
119
+ * @param payload - the payload to send
120
+ */
121
+ broadcastFrom(channelName, event, payload) {
122
+ __classPrivateFieldGet(this, _LobbyEngine_instances, "m", _LobbyEngine_performAction).call(this, channelName, (channel) => {
123
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
124
+ });
125
+ }
126
+ /**
127
+ * @desc Sends a message to a set of clients in the channel
128
+ * @param channelName - the name of the channel to send the message to
129
+ * @param event - the event to send
130
+ * @param payload - the payload to send
131
+ * @param userIds - the ids of the clients to send the message to
132
+ */
133
+ broadcastTo(channelName, event, payload, userIds) {
134
+ const ids = Array.isArray(userIds) ? userIds : [userIds];
135
+ __classPrivateFieldGet(this, _LobbyEngine_instances, "m", _LobbyEngine_performAction).call(this, channelName, (channel) => {
136
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, ids, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
137
+ });
138
+ }
102
139
  }
103
140
  exports.LobbyEngine = LobbyEngine;
104
- _LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap(), _LobbyEngine_parentEngine = new WeakMap();
141
+ _LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap(), _LobbyEngine_parentEngine = new WeakMap(), _LobbyEngine_instances = new WeakSet(), _LobbyEngine_performAction = function _LobbyEngine_performAction(channelName, handler) {
142
+ const channel = this.getChannel(channelName) || this.createChannel(channelName);
143
+ const assigns = channel.getAssigns();
144
+ handler(channel);
145
+ if (Object.keys(assigns).length === 0) {
146
+ this.destroyChannel(channelName);
147
+ }
148
+ };
105
149
  class PondChannel {
106
150
  constructor(lobby) {
107
151
  _PondChannel_lobby.set(this, void 0);
@@ -120,6 +164,15 @@ class PondChannel {
120
164
  }
121
165
  return null;
122
166
  }
167
+ broadcast(channelName, event, payload) {
168
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcast(channelName, event, payload);
169
+ }
170
+ broadcastFrom(channelName, event, payload) {
171
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcastFrom(channelName, event, payload);
172
+ }
173
+ broadcastTo(channelName, event, payload, userIds) {
174
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcastTo(channelName, event, payload, userIds);
175
+ }
123
176
  }
124
177
  exports.PondChannel = PondChannel;
125
178
  _PondChannel_lobby = new WeakMap();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.162",
3
+ "version": "0.1.163",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -149,6 +149,31 @@ export declare class PondChannel<EventType extends PondEventMap = PondEventMap,
149
149
  * const channel = pond.getChannel('my_channel')!;
150
150
  */
151
151
  getChannel(channelName: string): Channel<EventType, PresenceType, AssignType> | null;
152
+
153
+ /**
154
+ * Broadcasts a message to all clients in the channel
155
+ * @param channelName - The name of the channel to broadcast to
156
+ * @param event - The event to send
157
+ * @param payload - The payload to send
158
+ */
159
+ broadcast (channelName: string, event: string, payload: PondMessage): void;
160
+
161
+ /**
162
+ * Broadcasts a message to all clients in the channel except the sender
163
+ * @param channelName - The name of the channel to broadcast to
164
+ * @param event - The event to send
165
+ * @param payload - The payload to send
166
+ */
167
+ broadcastFrom (channelName: string, event: string, payload: PondMessage): void;
168
+
169
+ /**
170
+ * Broadcasts a message to a specific set of clients
171
+ * @param channelName - The name of the channel to broadcast to
172
+ * @param event - The event to send
173
+ * @param payload - The payload to send
174
+ * @param userIds - The ids of the clients to send the message to
175
+ */
176
+ broadcastTo (channelName: string, event: string, payload: PondMessage, userIds: string | string[]): void;
152
177
  }
153
178
 
154
179
  export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {