6
6
use BeyondCode \LaravelWebSockets \Channels \PresenceChannel ;
7
7
use BeyondCode \LaravelWebSockets \Channels \PrivateChannel ;
8
8
use BeyondCode \LaravelWebSockets \Contracts \ChannelManager ;
9
+ use BeyondCode \LaravelWebSockets \Helpers ;
9
10
use Illuminate \Support \Str ;
10
11
use Ratchet \ConnectionInterface ;
11
12
use React \EventLoop \LoopInterface ;
12
- use React \Promise \FulfilledPromise ;
13
13
use React \Promise \PromiseInterface ;
14
14
use stdClass ;
15
15
@@ -104,7 +104,7 @@ public function getLocalConnections(): PromiseInterface
104
104
->values ()->collapse ()
105
105
->toArray ();
106
106
107
- return new FulfilledPromise ($ connections );
107
+ return Helpers:: createFulfilledPromise ($ connections );
108
108
}
109
109
110
110
/**
@@ -116,7 +116,7 @@ public function getLocalConnections(): PromiseInterface
116
116
*/
117
117
public function getLocalChannels ($ appId ): PromiseInterface
118
118
{
119
- return new FulfilledPromise (
119
+ return Helpers:: createFulfilledPromise (
120
120
$ this ->channels [$ appId ] ?? []
121
121
);
122
122
}
@@ -137,12 +137,12 @@ public function getGlobalChannels($appId): PromiseInterface
137
137
* Remove connection from all channels.
138
138
*
139
139
* @param \Ratchet\ConnectionInterface $connection
140
- * @return void
140
+ * @return PromiseInterface[bool]
141
141
*/
142
- public function unsubscribeFromAllChannels (ConnectionInterface $ connection )
142
+ public function unsubscribeFromAllChannels (ConnectionInterface $ connection ): PromiseInterface
143
143
{
144
144
if (! isset ($ connection ->app )) {
145
- return ;
145
+ return new FuilfilledPromise ( false ) ;
146
146
}
147
147
148
148
$ this ->getLocalChannels ($ connection ->app ->id )
@@ -162,6 +162,8 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection)
162
162
unset($ this ->channels [$ connection ->app ->id ]);
163
163
}
164
164
});
165
+
166
+ return Helpers::createFulfilledPromise (true );
165
167
}
166
168
167
169
/**
@@ -170,13 +172,15 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection)
170
172
* @param \Ratchet\ConnectionInterface $connection
171
173
* @param string $channelName
172
174
* @param stdClass $payload
173
- * @return void
175
+ * @return PromiseInterface[bool]
174
176
*/
175
- public function subscribeToChannel (ConnectionInterface $ connection , string $ channelName , stdClass $ payload )
177
+ public function subscribeToChannel (ConnectionInterface $ connection , string $ channelName , stdClass $ payload ): PromiseInterface
176
178
{
177
179
$ channel = $ this ->findOrCreate ($ connection ->app ->id , $ channelName );
178
180
179
- $ channel ->subscribe ($ connection , $ payload );
181
+ return Helpers::createFulfilledPromise (
182
+ $ channel ->subscribe ($ connection , $ payload )
183
+ );
180
184
}
181
185
182
186
/**
@@ -185,35 +189,39 @@ public function subscribeToChannel(ConnectionInterface $connection, string $chan
185
189
* @param \Ratchet\ConnectionInterface $connection
186
190
* @param string $channelName
187
191
* @param stdClass $payload
188
- * @return void
192
+ * @return PromiseInterface[bool]
189
193
*/
190
- public function unsubscribeFromChannel (ConnectionInterface $ connection , string $ channelName , stdClass $ payload )
194
+ public function unsubscribeFromChannel (ConnectionInterface $ connection , string $ channelName , stdClass $ payload ): PromiseInterface
191
195
{
192
196
$ channel = $ this ->findOrCreate ($ connection ->app ->id , $ channelName );
193
197
194
- $ channel ->unsubscribe ($ connection , $ payload );
198
+ return Helpers::createFulfilledPromise (
199
+ $ channel ->unsubscribe ($ connection , $ payload )
200
+ );
195
201
}
196
202
197
203
/**
198
- * Subscribe the connection to a specific channel.
204
+ * Subscribe the connection to a specific channel, returning
205
+ * a promise containing the amount of connections.
199
206
*
200
207
* @param string|int $appId
201
- * @return void
208
+ * @return PromiseInterface[int]
202
209
*/
203
- public function subscribeToApp ($ appId )
210
+ public function subscribeToApp ($ appId ): PromiseInterface
204
211
{
205
- //
212
+ return Helpers:: createFulfilledPromise ( 0 );
206
213
}
207
214
208
215
/**
209
- * Unsubscribe the connection from the channel.
216
+ * Unsubscribe the connection from the channel, returning
217
+ * a promise containing the amount of connections after decrement.
210
218
*
211
219
* @param string|int $appId
212
- * @return void
220
+ * @return PromiseInterface[int]
213
221
*/
214
- public function unsubscribeFromApp ($ appId )
222
+ public function unsubscribeFromApp ($ appId ): PromiseInterface
215
223
{
216
- //
224
+ return Helpers:: createFulfilledPromise ( 0 );
217
225
}
218
226
219
227
/**
@@ -222,23 +230,21 @@ public function unsubscribeFromApp($appId)
222
230
*
223
231
* @param string|int $appId
224
232
* @param string|null $channelName
225
- * @return \React\Promise\ PromiseInterface
233
+ * @return PromiseInterface[int]
226
234
*/
227
235
public function getLocalConnectionsCount ($ appId , string $ channelName = null ): PromiseInterface
228
236
{
229
237
return $ this ->getLocalChannels ($ appId )
230
238
->then (function ($ channels ) use ($ channelName ) {
231
- return collect ($ channels )
232
- ->when (! is_null ($ channelName ), function ($ collection ) use ($ channelName ) {
233
- return $ collection ->filter (function (Channel $ channel ) use ($ channelName ) {
234
- return $ channel ->getName () === $ channelName ;
235
- });
236
- })
237
- ->flatMap (function (Channel $ channel ) {
238
- return collect ($ channel ->getConnections ())->pluck ('socketId ' );
239
- })
240
- ->unique ()
241
- ->count ();
239
+ return collect ($ channels )->when (! is_null ($ channelName ), function ($ collection ) use ($ channelName ) {
240
+ return $ collection ->filter (function (Channel $ channel ) use ($ channelName ) {
241
+ return $ channel ->getName () === $ channelName ;
242
+ });
243
+ })
244
+ ->flatMap (function (Channel $ channel ) {
245
+ return collect ($ channel ->getConnections ())->pluck ('socketId ' );
246
+ })
247
+ ->unique ()->count ();
242
248
});
243
249
}
244
250
@@ -248,7 +254,7 @@ public function getLocalConnectionsCount($appId, string $channelName = null): Pr
248
254
*
249
255
* @param string|int $appId
250
256
* @param string|null $channelName
251
- * @return \React\Promise\ PromiseInterface
257
+ * @return PromiseInterface[int]
252
258
*/
253
259
public function getGlobalConnectionsCount ($ appId , string $ channelName = null ): PromiseInterface
254
260
{
@@ -263,11 +269,11 @@ public function getGlobalConnectionsCount($appId, string $channelName = null): P
263
269
* @param string $channel
264
270
* @param stdClass $payload
265
271
* @param string|null $serverId
266
- * @return bool
272
+ * @return PromiseInterface[ bool]
267
273
*/
268
- public function broadcastAcrossServers ($ appId , ?string $ socketId , string $ channel , stdClass $ payload , string $ serverId = null )
274
+ public function broadcastAcrossServers ($ appId , ?string $ socketId , string $ channel , stdClass $ payload , string $ serverId = null ): PromiseInterface
269
275
{
270
- return true ;
276
+ return Helpers:: createFulfilledPromise ( true ) ;
271
277
}
272
278
273
279
/**
@@ -277,12 +283,14 @@ public function broadcastAcrossServers($appId, ?string $socketId, string $channe
277
283
* @param stdClass $user
278
284
* @param string $channel
279
285
* @param stdClass $payload
280
- * @return void
286
+ * @return PromiseInterface[bool]
281
287
*/
282
- public function userJoinedPresenceChannel (ConnectionInterface $ connection , stdClass $ user , string $ channel , stdClass $ payload )
288
+ public function userJoinedPresenceChannel (ConnectionInterface $ connection , stdClass $ user , string $ channel , stdClass $ payload ): PromiseInterface
283
289
{
284
290
$ this ->users ["{$ connection ->app ->id }: {$ channel }" ][$ connection ->socketId ] = json_encode ($ user );
285
291
$ this ->userSockets ["{$ connection ->app ->id }: {$ channel }: {$ user ->user_id }" ][] = $ connection ->socketId ;
292
+
293
+ return Helpers::createFulfilledPromise (true );
286
294
}
287
295
288
296
/**
@@ -292,9 +300,9 @@ public function userJoinedPresenceChannel(ConnectionInterface $connection, stdCl
292
300
* @param stdClass $user
293
301
* @param string $channel
294
302
* @param stdClass $payload
295
- * @return void
303
+ * @return PromiseInterface[bool]
296
304
*/
297
- public function userLeftPresenceChannel (ConnectionInterface $ connection , stdClass $ user , string $ channel )
305
+ public function userLeftPresenceChannel (ConnectionInterface $ connection , stdClass $ user , string $ channel ): PromiseInterface
298
306
{
299
307
unset($ this ->users ["{$ connection ->app ->id }: {$ channel }" ][$ connection ->socketId ]);
300
308
@@ -310,6 +318,8 @@ public function userLeftPresenceChannel(ConnectionInterface $connection, stdClas
310
318
unset($ this ->userSockets ["{$ connection ->app ->id }: {$ channel }: {$ user ->user_id }" ]);
311
319
}
312
320
}
321
+
322
+ return Helpers::createFulfilledPromise (true );
313
323
}
314
324
315
325
/**
@@ -327,7 +337,7 @@ public function getChannelMembers($appId, string $channel): PromiseInterface
327
337
return json_decode ($ user );
328
338
})->unique ('user_id ' )->toArray ();
329
339
330
- return new FulfilledPromise ($ members );
340
+ return Helpers:: createFulfilledPromise ($ members );
331
341
}
332
342
333
343
/**
@@ -341,7 +351,7 @@ public function getChannelMember(ConnectionInterface $connection, string $channe
341
351
{
342
352
$ member = $ this ->users ["{$ connection ->app ->id }: {$ channel }" ][$ connection ->socketId ] ?? null ;
343
353
344
- return new FulfilledPromise ($ member );
354
+ return Helpers:: createFulfilledPromise ($ member );
345
355
}
346
356
347
357
/**
@@ -362,7 +372,7 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
362
372
return $ results ;
363
373
}, []);
364
374
365
- return new FulfilledPromise ($ results );
375
+ return Helpers:: createFulfilledPromise ($ results );
366
376
}
367
377
368
378
/**
@@ -375,7 +385,7 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
375
385
*/
376
386
public function getMemberSockets ($ userId , $ appId , $ channelName ): PromiseInterface
377
387
{
378
- return new FulfilledPromise (
388
+ return Helpers:: createFulfilledPromise (
379
389
$ this ->userSockets ["{$ appId }: {$ channelName }: {$ userId }" ] ?? []
380
390
);
381
391
}
@@ -384,21 +394,21 @@ public function getMemberSockets($userId, $appId, $channelName): PromiseInterfac
384
394
* Keep tracking the connections availability when they pong.
385
395
*
386
396
* @param \Ratchet\ConnectionInterface $connection
387
- * @return bool
397
+ * @return PromiseInterface[ bool]
388
398
*/
389
- public function connectionPonged (ConnectionInterface $ connection ): bool
399
+ public function connectionPonged (ConnectionInterface $ connection ): PromiseInterface
390
400
{
391
- return true ;
401
+ return Helpers:: createFulfilledPromise ( true ) ;
392
402
}
393
403
394
404
/**
395
405
* Remove the obsolete connections that didn't ponged in a while.
396
406
*
397
- * @return bool
407
+ * @return PromiseInterface[ bool]
398
408
*/
399
- public function removeObsoleteConnections (): bool
409
+ public function removeObsoleteConnections (): PromiseInterface
400
410
{
401
- return true ;
411
+ return Helpers:: createFulfilledPromise ( true ) ;
402
412
}
403
413
404
414
/**
0 commit comments