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