@@ -53,73 +53,54 @@ trait InteractsWithWebsocket
53
53
protected $ wsEvents = ['open ' , 'message ' , 'close ' ];
54
54
55
55
/**
56
- * "onOpen" listener.
57
- *
58
- * @param \Swoole\Websocket\Server $server
56
+ * "onHandShake" listener.
59
57
* @param \Swoole\Http\Request $swooleRequest
58
+ * @param \Swoole\Http\Response $response
60
59
*/
61
- public function onOpen ( $ server , $ swooleRequest )
60
+ public function onHandShake ( $ swooleRequest , $ response )
62
61
{
63
- $ illuminateRequest = Request::make ($ swooleRequest )->toIlluminate ();
64
- $ websocket = $ this ->app ->make (Websocket::class);
65
- $ sandbox = $ this ->app ->make (Sandbox::class);
66
-
67
- try {
68
- $ websocket ->reset (true )->setSender ($ swooleRequest ->fd );
69
- // set currnt request to sandbox
70
- $ sandbox ->setRequest ($ illuminateRequest );
71
- // enable sandbox
72
- $ sandbox ->enable ();
73
- // check if socket.io connection established
74
- if (! $ this ->websocketHandler ->onOpen ($ swooleRequest ->fd , $ illuminateRequest )) {
75
- return ;
76
- }
77
- // trigger 'connect' websocket event
78
- if ($ websocket ->eventExists ('connect ' )) {
79
- // set sandbox container to websocket pipeline
80
- $ websocket ->setContainer ($ sandbox ->getApplication ());
81
- $ websocket ->call ('connect ' , $ illuminateRequest );
82
- }
83
- } catch (Throwable $ e ) {
84
- $ this ->logServerError ($ e );
85
- } finally {
86
- // disable and recycle sandbox resource
87
- $ sandbox ->disable ();
88
- }
62
+ $ this ->onOpen (
63
+ $ this ->app ->make (Server::class),
64
+ $ swooleRequest ,
65
+ $ response
66
+ );
89
67
}
90
68
91
69
/**
92
- * @param \Swoole\Http\Request $swooleRequest
93
- * @param \Swoole\Http\Response $response
70
+ * "onOpen" listener.
94
71
*
95
- * @return bool
96
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
72
+ * @param \Swoole\Websocket\Server $server
73
+ * @param \Swoole\Http\Request $swooleRequest
74
+ * @param \Swoole\Http\Response $response (optional)
97
75
*/
98
- public function onHandShake ( $ swooleRequest , $ response )
76
+ public function onOpen ( $ server , $ swooleRequest , $ response = null )
99
77
{
100
78
$ illuminateRequest = Request::make ($ swooleRequest )->toIlluminate ();
101
79
$ websocket = $ this ->app ->make (Websocket::class);
102
80
$ sandbox = $ this ->app ->make (Sandbox::class);
103
- $ handler = $ this ->container ->make ('config ' )->get ('swoole_websocket.handshake.handler ' );
81
+ $ handshakeHandler = $ this ->app ->make ('config ' )
82
+ ->get ('swoole_websocket.handshake.handler ' );
104
83
105
84
try {
106
85
$ websocket ->reset (true )->setSender ($ swooleRequest ->fd );
107
86
// set currnt request to sandbox
108
87
$ sandbox ->setRequest ($ illuminateRequest );
109
88
// enable sandbox
110
89
$ sandbox ->enable ();
111
-
112
- if (! $ this ->app ->make ($ handler )->handle ($ swooleRequest , $ response )) {
113
- return false ;
90
+ // call customized handshake handler
91
+ if ($ response && ! $ this ->app ->make ($ handshakeHandler )->handle ($ swooleRequest , $ response )) {
92
+ return ;
93
+ }
94
+ // check if socket.io connection established
95
+ if (! $ this ->websocketHandler ->onOpen ($ swooleRequest ->fd , $ illuminateRequest )) {
96
+ return ;
114
97
}
115
98
// trigger 'connect' websocket event
116
99
if ($ websocket ->eventExists ('connect ' )) {
117
100
// set sandbox container to websocket pipeline
118
101
$ websocket ->setContainer ($ sandbox ->getApplication ());
119
102
$ websocket ->call ('connect ' , $ illuminateRequest );
120
103
}
121
-
122
- return true ;
123
104
} catch (Throwable $ e ) {
124
105
$ this ->logServerError ($ e );
125
106
} finally {
@@ -258,17 +239,19 @@ public function getPayloadParser()
258
239
protected function prepareWebsocket ()
259
240
{
260
241
$ config = $ this ->container ->make ('config ' );
261
- $ isWebsocket = $ config ->get ('swoole_http.websocket.enabled ' );
262
242
$ parser = $ config ->get ('swoole_websocket.parser ' );
263
243
264
- if ($ isWebsocket ) {
265
- $ handshake = $ config ->get ('swoole_websocket.handshake.enabled ' );
244
+ if (! $ this ->isServerWebsocket = $ config ->get ('swoole_http.websocket.enabled ' )) {
245
+ return ;
246
+ }
266
247
267
- $ this ->events = array_merge ($ this ->events ?? [], array_merge ($ this ->wsEvents , $ handshake ? ['handshake ' ] : []));
268
- $ this ->isServerWebsocket = true ;
269
- $ this ->prepareWebsocketRoom ();
270
- $ this ->setPayloadParser (new $ parser );
248
+ if ($ config ->get ('swoole_websocket.handshake.enabled ' )) {
249
+ $ this ->wsEvents = array_merge ($ this ->wsEvents , ['handshake ' ]);
271
250
}
251
+
252
+ $ this ->events = array_merge ($ this ->events ?? [], $ this ->wsEvents );
253
+ $ this ->prepareWebsocketRoom ();
254
+ $ this ->setPayloadParser (new $ parser );
272
255
}
273
256
274
257
/**
0 commit comments