@@ -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-         $ 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+         );
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        $ illuminateRequest  = Request::make ($ swooleRequest )->toIlluminate ();
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            $ websocket ->reset (true )->setSender ($ swooleRequest ->fd );
10786            // set currnt request to sandbox 
10887            $ sandbox ->setRequest ($ illuminateRequest );
10988            // enable sandbox 
11089            $ 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 ;
11497            }
11598            // trigger 'connect' websocket event 
11699            if  ($ websocket ->eventExists ('connect ' )) {
117100                // set sandbox container to websocket pipeline 
118101                $ websocket ->setContainer ($ sandbox ->getApplication ());
119102                $ websocket ->call ('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  = $ config ->get ('swoole_http.websocket.enabled ' );
262242        $ parser  = $ config ->get ('swoole_websocket.parser ' );
263243
264-         if  ($ isWebsocket ) {
265-             $ handshake  = $ config ->get ('swoole_websocket.handshake.enabled ' );
244+         if  (! $ this  ->isServerWebsocket  = $ config ->get ('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  ($ config ->get ('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