@@ -53,73 +53,54 @@ trait InteractsWithWebsocket
5353    protected  $ wsEvents'open ' , 'message ' , 'close ' ];
5454
5555    /** 
56-      * "onOpen" listener. 
57-      * 
58-      * @param \Swoole\Websocket\Server $server 
56+      * "onHandShake" listener. 
5957     * @param \Swoole\Http\Request $swooleRequest 
58+      * @param \Swoole\Http\Response $response 
6059     */ 
61-     public  function  onOpen ( $ server $ swooleRequest 
60+     public  function  onHandShake ( $ swooleRequest $ response 
6261    {
63-         $ illuminateRequestmake ($ swooleRequesttoIlluminate ();
64-         $ websocket$ this app ->make (Websocket::class);
65-         $ sandbox$ this app ->make (Sandbox::class);
66- 
67-         try  {
68-             $ websocketreset (true )->setSender ($ swooleRequestfd );
69-             // set currnt request to sandbox 
70-             $ sandboxsetRequest ($ illuminateRequest
71-             // enable sandbox 
72-             $ sandboxenable ();
73-             // check if socket.io connection established 
74-             if  (! $ this websocketHandler ->onOpen ($ swooleRequestfd , $ illuminateRequest
75-                 return ;
76-             }
77-             // trigger 'connect' websocket event 
78-             if  ($ websocketeventExists ('connect ' )) {
79-                 // set sandbox container to websocket pipeline 
80-                 $ websocketsetContainer ($ sandboxgetApplication ());
81-                 $ websocketcall ('connect ' , $ illuminateRequest
82-             }
83-         } catch  (Throwable $ e
84-             $ this logServerError ($ e
85-         } finally  {
86-             // disable and recycle sandbox resource 
87-             $ sandboxdisable ();
88-         }
62+         $ this onOpen (
63+             $ this app ->make (Server::class),
64+             $ swooleRequest
65+             $ response
66+         );
8967    }
9068
9169    /** 
92-      * @param \Swoole\Http\Request $swooleRequest 
93-      * @param \Swoole\Http\Response $response 
70+      * "onOpen" listener. 
9471     * 
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) 
9775     */ 
98-     public  function  onHandShake ( $ swooleRequest$ response
76+     public  function  onOpen ( $ server ,  $ swooleRequest$ response =  null )
9977    {
10078        $ illuminateRequestmake ($ swooleRequesttoIlluminate ();
10179        $ websocket$ this app ->make (Websocket::class);
10280        $ 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 ' );
10483
10584        try  {
10685            $ websocketreset (true )->setSender ($ swooleRequestfd );
10786            // set currnt request to sandbox 
10887            $ sandboxsetRequest ($ illuminateRequest
10988            // enable sandbox 
11089            $ sandboxenable ();
111- 
112-             if  (! $ this app ->make ($ handlerhandle ($ swooleRequest$ response
113-                 return  false ;
90+             // call customized handshake handler 
91+             if  ($ response$ this app ->make ($ handshakeHandlerhandle ($ swooleRequest$ response
92+                 return ;
93+             }
94+             // check if socket.io connection established 
95+             if  (! $ this websocketHandler ->onOpen ($ swooleRequestfd , $ illuminateRequest
96+                 return ;
11497            }
11598            // trigger 'connect' websocket event 
11699            if  ($ websocketeventExists ('connect ' )) {
117100                // set sandbox container to websocket pipeline 
118101                $ websocketsetContainer ($ sandboxgetApplication ());
119102                $ websocketcall ('connect ' , $ illuminateRequest
120103            }
121- 
122-             return  true ;
123104        } catch  (Throwable $ e
124105            $ this logServerError ($ e
125106        } finally  {
@@ -258,17 +239,19 @@ public function getPayloadParser()
258239    protected  function  prepareWebsocket ()
259240    {
260241        $ config$ this container ->make ('config ' );
261-         $ isWebsocket$ configget ('swoole_http.websocket.enabled ' );
262242        $ parser$ configget ('swoole_websocket.parser ' );
263243
264-         if  ($ isWebsocket
265-             $ handshake$ configget ('swoole_websocket.handshake.enabled ' );
244+         if  (! $ this isServerWebsocket  = $ configget ('swoole_http.websocket.enabled ' )) {
245+             return ;
246+         }
266247
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  ($ configget ('swoole_websocket.handshake.enabled ' )) {
249+             $ this wsEvents  = array_merge ($ this wsEvents , ['handshake ' ]);
271250        }
251+ 
252+         $ this events  = array_merge ($ this events  ?? [], $ this wsEvents );
253+         $ this prepareWebsocketRoom ();
254+         $ this setPayloadParser (new  $ parser
272255    }
273256
274257    /** 
0 commit comments