1313class Handler extends WebsocketHandler
1414{
1515 protected ExceptionHandler $ exceptionHandler ;
16- protected SubscriptionRepository $ connectionRepository ;
16+ protected SubscriptionRepository $ subscriptionRepository ;
17+ protected ConnectionRepository $ connectionRepository ;
1718
18- public function __construct (ExceptionHandler $ exceptionHandler , SubscriptionRepository $ connectionRepository )
19+ public function __construct (ExceptionHandler $ exceptionHandler , SubscriptionRepository $ subscriptionRepository , ConnectionRepository $ connectionRepository )
1920 {
2021 $ this ->exceptionHandler = $ exceptionHandler ;
22+ $ this ->subscriptionRepository = $ subscriptionRepository ;
2123 $ this ->connectionRepository = $ connectionRepository ;
2224 }
2325
@@ -30,22 +32,22 @@ public function handleWebsocket(WebsocketEvent $event, Context $context): HttpRe
3032 throw new \InvalidArgumentException ("Event type {$ event ->getEventType ()} has no handler implemented. " );
3133 }
3234
33- return $ this ->$ method ($ event , $ context );
35+ $ this ->$ method ($ event , $ context );
36+
37+ return new HttpResponse ('OK ' );
3438 } catch (Throwable $ throwable ) {
3539 $ this ->exceptionHandler ->report ($ throwable );
3640
3741 throw $ throwable ;
3842 }
3943 }
4044
41- protected function handleDisconnect (WebsocketEvent $ event , Context $ context ): HttpResponse
45+ protected function handleDisconnect (WebsocketEvent $ event , Context $ context ): void
4246 {
43- $ this ->connectionRepository ->clearConnection ($ event ->getConnectionId ());
44-
45- return new HttpResponse ('OK ' );
47+ $ this ->subscriptionRepository ->clearConnection ($ event ->getConnectionId ());
4648 }
4749
48- protected function handleMessage (WebsocketEvent $ event , Context $ context ): HttpResponse
50+ protected function handleMessage (WebsocketEvent $ event , Context $ context ): void
4951 {
5052 $ eventBody = json_decode ($ event ->getBody (), true );
5153
@@ -56,36 +58,29 @@ protected function handleMessage(WebsocketEvent $event, Context $context): HttpR
5658 $ eventType = $ eventBody ['event ' ];
5759
5860 if ($ eventType === 'ping ' ) {
59- return $ this ->jsonResponse ( [
61+ $ this ->sendMessage ( $ event , $ context , [
6062 'event ' => 'pong ' ,
6163 'channel ' => $ eventBody ['channel ' ] ?? null ,
6264 ]);
63- }
64-
65- if ($ eventType === 'whoami ' ) {
66- return $ this ->jsonResponse ([
65+ } elseif ($ eventType === 'whoami ' ) {
66+ $ this ->sendMessage ($ event , $ context , [
6767 'event ' => 'whoami ' ,
6868 'data ' => [
6969 'socket_id ' => $ event ->getConnectionId (),
7070 ],
7171 ]);
72+ } elseif ($ eventType === 'subscribe ' ) {
73+ $ this ->subscribe ($ event , $ context );
74+ } elseif ($ eventType === 'unsubscribe ' ) {
75+ $ this ->unsubscribe ($ event , $ context );
76+ } else {
77+ $ this ->sendMessage ($ event , $ context , [
78+ 'event ' => 'error ' ,
79+ ]);
7280 }
73-
74- if ($ eventType === 'subscribe ' ) {
75- return $ this ->subscribe ($ event , $ context );
76- }
77-
78- if ($ eventType === 'unsubscribe ' ) {
79- return $ this ->unsubscribe ($ event , $ context );
80- }
81-
82-
83- return $ this ->jsonResponse ([
84- 'event ' => 'error ' ,
85- ]);
8681 }
8782
88- protected function subscribe (WebsocketEvent $ event , Context $ context ): HttpResponse
83+ protected function subscribe (WebsocketEvent $ event , Context $ context ): void
8984 {
9085 $ eventBody = json_decode ($ event ->getBody (), true );
9186
@@ -108,41 +103,43 @@ protected function subscribe(WebsocketEvent $event, Context $context): HttpRespo
108103 $ signature = hash_hmac ('sha256 ' , $ data , config ('app.key ' ), false );
109104
110105 if ($ signature !== $ auth ) {
111- return $ this ->jsonResponse ( [
106+ $ this ->sendMessage ( $ event , $ context , [
112107 'event ' => 'error ' ,
113108 'channel ' => $ channel ,
114109 'data ' => [
115110 'message ' => 'Invalid auth signature ' ,
116111 ],
117112 ]);
113+
114+ return ;
118115 }
119116 }
120117
121- $ this ->connectionRepository ->subscribeToChannel ($ event ->getConnectionId (), $ channel );
118+ $ this ->subscriptionRepository ->subscribeToChannel ($ event ->getConnectionId (), $ channel );
122119
123- return $ this ->jsonResponse ( [
120+ $ this ->sendMessage ( $ event , $ context , [
124121 'event ' => 'subscription_succeeded ' ,
125122 'channel ' => $ channel ,
126123 'data ' => [],
127124 ]);
128125 }
129126
130- protected function unsubscribe (WebsocketEvent $ event , Context $ context ): HttpResponse
127+ protected function unsubscribe (WebsocketEvent $ event , Context $ context ): void
131128 {
132129 $ eventBody = json_decode ($ event ->getBody (), true );
133130 $ channel = $ eventBody ['data ' ]['channel ' ];
134131
135- $ this ->connectionRepository ->unsubscribeFromChannel ($ event ->getConnectionId (), $ channel );
132+ $ this ->subscriptionRepository ->unsubscribeFromChannel ($ event ->getConnectionId (), $ channel );
136133
137- return $ this ->jsonResponse ( [
134+ $ this ->sendMessage ( $ event , $ context , [
138135 'event ' => 'unsubscription_succeeded ' ,
139136 'channel ' => $ channel ,
140137 'data ' => [],
141138 ]);
142139 }
143140
144- protected function jsonResponse ( array $ data ): HttpResponse
141+ public function sendMessage ( WebsocketEvent $ event , Context $ context , array $ data ): void
145142 {
146- return new HttpResponse ( json_encode ($ data , JSON_THROW_ON_ERROR ));
143+ $ this -> connectionRepository -> sendMessage ( $ event -> getConnectionId (), json_encode ($ data , JSON_THROW_ON_ERROR ));
147144 }
148145}
0 commit comments