PHP stream_get_line hangs on dead connection, how to combat this

I have a twitchbot I wrote in PHP and I make a connection to Twitch via these snippets

self::$socket = stream_socket_client(%url%, $errno, $errstr, 60);
...
stream_set_timeout(self::$socket, 2);
stream_set_blocking(self::$socket, false);

Once connected, the bot throws itself into a read loop, reading the socket like this

driver::$socketDataRaw = stream_get_line(self::$socket, 1024, "\r\n");

All of this has worked for the last couple years. My problem is if my internet at home goes out, or my router reboots, the socket connection is broken obviously. When the next read loop is attempted, the code hangs on that stream_get_line until I manually kill the bot from the CLI. Is there ANY way to get the stream_get_line to time out or something if it doesn’t get anything back within 1 or 2 seconds? The problem exists whether I have the socket on blocking mode or not.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.