13
13
class Handler extends WebsocketHandler
14
14
{
15
15
protected ExceptionHandler $ exceptionHandler ;
16
- protected SubscriptionRepository $ connectionRepository ;
16
+ protected SubscriptionRepository $ subscriptionRepository ;
17
+ protected ConnectionRepository $ connectionRepository ;
17
18
18
- public function __construct (ExceptionHandler $ exceptionHandler , SubscriptionRepository $ connectionRepository )
19
+ public function __construct (ExceptionHandler $ exceptionHandler , SubscriptionRepository $ subscriptionRepository , ConnectionRepository $ connectionRepository )
19
20
{
20
21
$ this ->exceptionHandler = $ exceptionHandler ;
22
+ $ this ->subscriptionRepository = $ subscriptionRepository ;
21
23
$ this ->connectionRepository = $ connectionRepository ;
22
24
}
23
25
@@ -30,22 +32,22 @@ public function handleWebsocket(WebsocketEvent $event, Context $context): HttpRe
30
32
throw new \InvalidArgumentException ("Event type {$ event ->getEventType ()} has no handler implemented. " );
31
33
}
32
34
33
- return $ this ->$ method ($ event , $ context );
35
+ $ this ->$ method ($ event , $ context );
36
+
37
+ return new HttpResponse ('OK ' );
34
38
} catch (Throwable $ throwable ) {
35
39
$ this ->exceptionHandler ->report ($ throwable );
36
40
37
41
throw $ throwable ;
38
42
}
39
43
}
40
44
41
- protected function handleDisconnect (WebsocketEvent $ event , Context $ context ): HttpResponse
45
+ protected function handleDisconnect (WebsocketEvent $ event , Context $ context ): void
42
46
{
43
- $ this ->connectionRepository ->clearConnection ($ event ->getConnectionId ());
44
-
45
- return new HttpResponse ('OK ' );
47
+ $ this ->subscriptionRepository ->clearConnection ($ event ->getConnectionId ());
46
48
}
47
49
48
- protected function handleMessage (WebsocketEvent $ event , Context $ context ): HttpResponse
50
+ protected function handleMessage (WebsocketEvent $ event , Context $ context ): void
49
51
{
50
52
$ eventBody = json_decode ($ event ->getBody (), true );
51
53
@@ -56,36 +58,29 @@ protected function handleMessage(WebsocketEvent $event, Context $context): HttpR
56
58
$ eventType = $ eventBody ['event ' ];
57
59
58
60
if ($ eventType === 'ping ' ) {
59
- return $ this ->jsonResponse ( [
61
+ $ this ->sendMessage ( $ event , $ context , [
60
62
'event ' => 'pong ' ,
61
63
'channel ' => $ eventBody ['channel ' ] ?? null ,
62
64
]);
63
- }
64
-
65
- if ($ eventType === 'whoami ' ) {
66
- return $ this ->jsonResponse ([
65
+ } elseif ($ eventType === 'whoami ' ) {
66
+ $ this ->sendMessage ($ event , $ context , [
67
67
'event ' => 'whoami ' ,
68
68
'data ' => [
69
69
'socket_id ' => $ event ->getConnectionId (),
70
70
],
71
71
]);
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
+ ]);
72
80
}
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
- ]);
86
81
}
87
82
88
- protected function subscribe (WebsocketEvent $ event , Context $ context ): HttpResponse
83
+ protected function subscribe (WebsocketEvent $ event , Context $ context ): void
89
84
{
90
85
$ eventBody = json_decode ($ event ->getBody (), true );
91
86
@@ -108,41 +103,43 @@ protected function subscribe(WebsocketEvent $event, Context $context): HttpRespo
108
103
$ signature = hash_hmac ('sha256 ' , $ data , config ('app.key ' ), false );
109
104
110
105
if ($ signature !== $ auth ) {
111
- return $ this ->jsonResponse ( [
106
+ $ this ->sendMessage ( $ event , $ context , [
112
107
'event ' => 'error ' ,
113
108
'channel ' => $ channel ,
114
109
'data ' => [
115
110
'message ' => 'Invalid auth signature ' ,
116
111
],
117
112
]);
113
+
114
+ return ;
118
115
}
119
116
}
120
117
121
- $ this ->connectionRepository ->subscribeToChannel ($ event ->getConnectionId (), $ channel );
118
+ $ this ->subscriptionRepository ->subscribeToChannel ($ event ->getConnectionId (), $ channel );
122
119
123
- return $ this ->jsonResponse ( [
120
+ $ this ->sendMessage ( $ event , $ context , [
124
121
'event ' => 'subscription_succeeded ' ,
125
122
'channel ' => $ channel ,
126
123
'data ' => [],
127
124
]);
128
125
}
129
126
130
- protected function unsubscribe (WebsocketEvent $ event , Context $ context ): HttpResponse
127
+ protected function unsubscribe (WebsocketEvent $ event , Context $ context ): void
131
128
{
132
129
$ eventBody = json_decode ($ event ->getBody (), true );
133
130
$ channel = $ eventBody ['data ' ]['channel ' ];
134
131
135
- $ this ->connectionRepository ->unsubscribeFromChannel ($ event ->getConnectionId (), $ channel );
132
+ $ this ->subscriptionRepository ->unsubscribeFromChannel ($ event ->getConnectionId (), $ channel );
136
133
137
- return $ this ->jsonResponse ( [
134
+ $ this ->sendMessage ( $ event , $ context , [
138
135
'event ' => 'unsubscription_succeeded ' ,
139
136
'channel ' => $ channel ,
140
137
'data ' => [],
141
138
]);
142
139
}
143
140
144
- protected function jsonResponse ( array $ data ): HttpResponse
141
+ public function sendMessage ( WebsocketEvent $ event , Context $ context , array $ data ): void
145
142
{
146
- return new HttpResponse ( json_encode ($ data , JSON_THROW_ON_ERROR ));
143
+ $ this -> connectionRepository -> sendMessage ( $ event -> getConnectionId (), json_encode ($ data , JSON_THROW_ON_ERROR ));
147
144
}
148
145
}
0 commit comments