@@ -97,6 +97,10 @@ public function connect(string $target = '/'): string
97
97
$ socket = @stream_socket_client ($ this ->endpoint , $ errno , $ errstr );
98
98
99
99
if (false === $ socket ) {
100
+ if (null !== $ this ->logger ) {
101
+ $ this ->logger ->error ('Could not open socket. ' , ['errno ' => $ errno , 'errstr ' => $ errstr ]);
102
+ }
103
+
100
104
throw new BadResponseException ('Could not open socket. Reason: ' .$ errstr , $ errno );
101
105
}
102
106
@@ -112,6 +116,10 @@ public function connect(string $target = '/'): string
112
116
}
113
117
114
118
if (Protocol::MSG_WELCOME !== $ payload [0 ]) {
119
+ if (null !== $ this ->logger ) {
120
+ $ this ->logger ->error ('WAMP Server did not send a welcome message. ' , ['payload ' => $ payload ]);
121
+ }
122
+
115
123
throw new BadResponseException ('WAMP Server did not send a welcome message. ' );
116
124
}
117
125
@@ -130,6 +138,10 @@ private function upgradeProtocol(string $target)
130
138
$ key = $ this ->generateKey ();
131
139
132
140
if (false === strpos ($ target , '/ ' )) {
141
+ if (null !== $ this ->logger ) {
142
+ $ this ->logger ->error ('Invalid target path for WAMP server. ' , ['target ' => $ target ]);
143
+ }
144
+
133
145
throw new WebsocketException ('WAMP server target must contain a "/" ' );
134
146
}
135
147
@@ -159,12 +171,20 @@ private function upgradeProtocol(string $target)
159
171
private function verifyResponse ($ response ): void
160
172
{
161
173
if (false === $ response ) {
174
+ if (null !== $ this ->logger ) {
175
+ $ this ->logger ->error ('WAMP Server did not respond properly ' );
176
+ }
177
+
162
178
throw new BadResponseException ('WAMP Server did not respond properly ' );
163
179
}
164
180
165
181
$ responseStatus = substr ($ response , 0 , 12 );
166
182
167
183
if ('HTTP/1.1 101 ' !== $ responseStatus ) {
184
+ if (null !== $ this ->logger ) {
185
+ $ this ->logger ->error ('Unexpected HTTP response from WAMP server. ' , ['response ' => $ response ]);
186
+ }
187
+
168
188
throw new BadResponseException (sprintf ('Unexpected response status. Expected "HTTP/1.1 101", got "%s". ' , $ responseStatus ));
169
189
}
170
190
}
@@ -181,13 +201,21 @@ private function read(): string
181
201
$ streamBody = stream_get_contents ($ this ->socket , stream_get_meta_data ($ this ->socket )['unread_bytes ' ]);
182
202
183
203
if (false === $ streamBody ) {
204
+ if (null !== $ this ->logger ) {
205
+ $ this ->logger ->error ('The stream buffer could not be read. ' , ['error ' => error_get_last ()]);
206
+ }
207
+
184
208
throw new BadResponseException ('The stream buffer could not be read. ' );
185
209
}
186
210
187
211
$ startPos = strpos ($ streamBody , '[ ' );
188
212
$ endPos = strpos ($ streamBody , '] ' );
189
213
190
214
if (false === $ startPos || false === $ endPos ) {
215
+ if (null !== $ this ->logger ) {
216
+ $ this ->logger ->error ('Could not extract response body from stream. ' , ['body ' => $ streamBody ]);
217
+ }
218
+
191
219
throw new BadResponseException ('Could not extract response body from stream. ' );
192
220
}
193
221
@@ -216,13 +244,21 @@ public function disconnect(): bool
216
244
$ firstByte = fread ($ this ->socket , 1 );
217
245
218
246
if (false === $ firstByte ) {
247
+ if (null !== $ this ->logger ) {
248
+ $ this ->logger ->error ('Could not extract the payload from the buffer. ' , ['error ' => error_get_last ()]);
249
+ }
250
+
219
251
throw new WebsocketException ('Could not extract the payload from the buffer. ' );
220
252
}
221
253
222
254
$ payloadLength = \ord ($ firstByte );
223
255
$ payload = fread ($ this ->socket , $ payloadLength );
224
256
225
257
if (false === $ payload ) {
258
+ if (null !== $ this ->logger ) {
259
+ $ this ->logger ->error ('Could not extract the payload from the buffer. ' , ['error ' => error_get_last ()]);
260
+ }
261
+
226
262
throw new WebsocketException ('Could not extract the payload from the buffer. ' );
227
263
}
228
264
0 commit comments