4
4
5
5
use InvalidArgumentException ;
6
6
use Illuminate \Support \Facades \App ;
7
+ use SwooleTW \Http \Websocket \Authenticatable ;
7
8
use Illuminate \Contracts \Container \Container ;
8
9
use SwooleTW \Http \Websocket \Rooms \RoomContract ;
9
10
use Illuminate \Contracts \Pipeline \Pipeline as PipelineContract ;
10
11
11
12
class Websocket
12
13
{
13
- const PUSH_ACTION = ' push ' ;
14
+ use Authenticatable ;
14
15
16
+ const PUSH_ACTION = 'push ' ;
15
17
const EVENT_CONNECT = 'connect ' ;
18
+ const USER_PREFIX = 'uid_ ' ;
16
19
17
20
/**
18
21
* Determine if to broadcast.
@@ -86,25 +89,15 @@ public function broadcast()
86
89
return $ this ;
87
90
}
88
91
89
- /**
90
- * Set a recepient's fd or a room name.
91
- *
92
- * @param integer, string
93
- */
94
- public function to ($ value )
95
- {
96
- $ this ->toAll ([$ value ]);
97
-
98
- return $ this ;
99
- }
100
-
101
92
/**
102
93
* Set multiple recepients' fd or room names.
103
94
*
104
- * @param array (fds or rooms)
95
+ * @param integer, string, array
105
96
*/
106
- public function toAll ( array $ values )
97
+ public function to ( $ values )
107
98
{
99
+ $ values = is_string ($ values ) || is_integer ($ values ) ? func_get_args () : $ values ;
100
+
108
101
foreach ($ values as $ value ) {
109
102
if (! in_array ($ value , $ this ->to )) {
110
103
$ this ->to [] = $ value ;
@@ -114,49 +107,29 @@ public function toAll(array $values)
114
107
return $ this ;
115
108
}
116
109
117
- /**
118
- * Join sender to a room.
119
- *
120
- * @param string
121
- */
122
- public function join (string $ room )
123
- {
124
- $ this ->room ->add ($ this ->sender , $ room );
125
-
126
- return $ this ;
127
- }
128
-
129
110
/**
130
111
* Join sender to multiple rooms.
131
112
*
132
- * @param array
113
+ * @param string, array
133
114
*/
134
- public function joinAll ( array $ rooms )
115
+ public function join ( $ rooms )
135
116
{
136
- $ this ->room ->addAll ($ this ->sender , $ rooms );
137
-
138
- return $ this ;
139
- }
117
+ $ rooms = is_string ($ rooms ) || is_integer ($ rooms ) ? func_get_args () : $ rooms ;
140
118
141
- /**
142
- * Make sender leave a room.
143
- *
144
- * @param string
145
- */
146
- public function leave (string $ room )
147
- {
148
- $ this ->room ->delete ($ this ->sender , $ room );
119
+ $ this ->room ->addAll ($ this ->sender , $ rooms );
149
120
150
121
return $ this ;
151
122
}
152
123
153
124
/**
154
125
* Make sender leave multiple rooms.
155
126
*
156
- * @param array
127
+ * @param string, array
157
128
*/
158
- public function leaveAll ( array $ rooms = [] )
129
+ public function leave ( $ rooms )
159
130
{
131
+ $ rooms = is_string ($ rooms ) || is_integer ($ rooms ) ? func_get_args () : $ rooms ;
132
+
160
133
$ this ->room ->deleteAll ($ this ->sender , $ rooms );
161
134
162
135
return $ this ;
@@ -322,7 +295,13 @@ protected function getFds()
322
295
$ rooms = array_diff ($ this ->to , $ fds );
323
296
324
297
foreach ($ rooms as $ room ) {
325
- $ fds = array_merge ($ fds , $ this ->room ->getClients ($ room ));
298
+ $ clients = $ this ->room ->getClients ($ room );
299
+ // rollback fd with wrong type back to fds array
300
+ if (empty ($ clients ) && is_numeric ($ room )) {
301
+ $ fds [] = $ room ;
302
+ } else {
303
+ $ fds = array_merge ($ fds , $ this ->room ->getClients ($ clients ));
304
+ }
326
305
}
327
306
328
307
return array_values (array_unique ($ fds ));
0 commit comments