@@ -249,6 +249,8 @@ protected function receiveFragment(): array
249249
250250 if ($ opcode === 'close ' ) {
251251 // Get the close status.
252+ $ status_bin = '' ;
253+ $ status = '' ;
252254 if ($ payload_length > 0 ) {
253255 $ status_bin = $ payload [0 ] . $ payload [1 ];
254256 $ status = bindec (sprintf ("%08b%08b " , ord ($ payload [0 ]), ord ($ payload [1 ])));
@@ -305,12 +307,9 @@ protected function write($data): void
305307 $ length = strlen ($ data );
306308 $ written = fwrite ($ this ->socket , $ data );
307309 if ($ written === false ) {
308- fclose ($ this ->socket );
309310 $ this ->throwException ("Failed to write {$ length } bytes. " );
310311 }
311-
312312 if ($ written < strlen ($ data )) {
313- fclose ($ this ->socket );
314313 $ this ->throwException ("Could only write {$ written } out of {$ length } bytes. " );
315314 }
316315 $ this ->logger ->debug ("Wrote {$ written } of {$ length } bytes. " );
@@ -323,11 +322,9 @@ protected function read($length): string
323322 $ buffer = fread ($ this ->socket , $ length - strlen ($ data ));
324323 if ($ buffer === false ) {
325324 $ read = strlen ($ data );
326- fclose ($ this ->socket );
327325 $ this ->throwException ("Broken frame, read {$ read } of stated {$ length } bytes. " );
328326 }
329327 if ($ buffer === '' ) {
330- fclose ($ this ->socket );
331328 $ this ->throwException ("Empty read; connection dead? " );
332329 }
333330 $ data .= $ buffer ;
@@ -339,6 +336,7 @@ protected function throwException($message, $code = 0): void
339336 {
340337 $ meta = $ this ->isConnected () ? stream_get_meta_data ($ this ->socket ) : [];
341338 $ json_meta = json_encode ($ meta );
339+ fclose ($ this ->socket );
342340 if (!empty ($ meta ['timed_out ' ])) {
343341 $ code = ConnectionException::TIMED_OUT ;
344342 $ this ->logger ->warning ("{$ message }" , (array )$ meta );
0 commit comments