From 1b84e4a2073c5e7f7b913d24037ab4acc1247959 Mon Sep 17 00:00:00 2001 From: bitte-ein-bit Date: Fri, 24 May 2013 20:14:16 +0200 Subject: [PATCH 1/4] Silence notice on Windows --- php5.3/SocketIO.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php5.3/SocketIO.class.php b/php5.3/SocketIO.class.php index 252bbef..192355d 100644 --- a/php5.3/SocketIO.class.php +++ b/php5.3/SocketIO.class.php @@ -84,7 +84,7 @@ public function loop(&$stay_in_loop = true) { $r = array($this->fd); $w = null; $e = null; - $n = stream_select($r, $w, $e, 5); + $n = @stream_select($r, $w, $e, 5); if ($n == 0) continue; $this->prvHandleData(); From 80bd120d7a8fe0b54fcd75674f6df41db2e3159a Mon Sep 17 00:00:00 2001 From: bitte-ein-bit Date: Fri, 24 May 2013 20:16:59 +0200 Subject: [PATCH 2/4] detect dead connection --- php5.3/SocketIO.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php5.3/SocketIO.class.php b/php5.3/SocketIO.class.php index 192355d..c9f536f 100644 --- a/php5.3/SocketIO.class.php +++ b/php5.3/SocketIO.class.php @@ -167,7 +167,7 @@ protected function raw_read() { } if ($pos === false) { $tmp = fread($this->fd, 4096); - if ($tmp === false) throw new \Exception('Lost connection?'); + if ($tmp === false || feof($this->fd)) throw new \Exception('Lost connection?'); $this->buf .= $tmp; continue; } From 2ee1d200a4f60ee29fc3190688b84f082037cc9e Mon Sep 17 00:00:00 2001 From: bitte-ein-bit Date: Fri, 24 May 2013 20:18:03 +0200 Subject: [PATCH 3/4] send hardbeat 5 seconds early, not 5 seconds too late --- php5.3/SocketIO.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php5.3/SocketIO.class.php b/php5.3/SocketIO.class.php index c9f536f..3575257 100644 --- a/php5.3/SocketIO.class.php +++ b/php5.3/SocketIO.class.php @@ -76,7 +76,7 @@ public function send_json($obj) { public function loop(&$stay_in_loop = true) { while($stay_in_loop) { - if ($this->stamp < (time()-$this->session[1]-5)) { + if ($this->stamp < (time()-$this->session[1]+5)) { // heartbeat time $this->raw_send('2::'); $this->stamp = time(); From e54bf0114b2ccec0d05d81fd23757f63bb1d7135 Mon Sep 17 00:00:00 2001 From: bitte-ein-bit Date: Fri, 24 May 2013 20:18:57 +0200 Subject: [PATCH 4/4] simplified code (removed redundant code) --- php5.3/SocketIO.class.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/php5.3/SocketIO.class.php b/php5.3/SocketIO.class.php index 3575257..2f5172e 100644 --- a/php5.3/SocketIO.class.php +++ b/php5.3/SocketIO.class.php @@ -75,7 +75,7 @@ public function send_json($obj) { } public function loop(&$stay_in_loop = true) { - while($stay_in_loop) { + do { if ($this->stamp < (time()-$this->session[1]+5)) { // heartbeat time $this->raw_send('2::'); @@ -88,7 +88,8 @@ public function loop(&$stay_in_loop = true) { if ($n == 0) continue; $this->prvHandleData(); - } + } while($stay_in_loop); + } public function getFd() { @@ -96,16 +97,8 @@ public function getFd() { } public function handleData() { - if ($this->stamp < (time()-$this->session[1]-5)) { - // heartbeat time - $this->raw_send('2::'); - $this->stamp = time(); - } - $r = array($this->fd); - $w = null; $e = null; - $n = stream_select($r, $w, $e, 0); - if ($n == 0) continue; - $this->prvHandleData(); + $f = false; + $this->loop($f); } private function prvHandleData() {