From f98e3c3423a03cca592760e2e406aa96ea3da3db Mon Sep 17 00:00:00 2001 From: Andres Espinosa Date: Thu, 29 Oct 2020 12:53:31 -0300 Subject: [PATCH 1/2] fix(connect): fix ping system --- lib/socket.io-client-simple/client.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/socket.io-client-simple/client.rb b/lib/socket.io-client-simple/client.rb index d4a48ad..b85ae08 100644 --- a/lib/socket.io-client-simple/client.rb +++ b/lib/socket.io-client-simple/client.rb @@ -12,8 +12,9 @@ class Client include EventEmitter alias_method :__emit, :emit - attr_accessor :auto_reconnection, :websocket, :url, :reconnecting, :state, - :session_id, :ping_interval, :ping_timeout, :last_pong_at, :last_ping_at + attr_accessor :auto_reconnection, :websocket, :url, :reconnecting, + :state, :session_id, :ping_interval, :ping_timeout, + :ping_state, :last_pong_at, :last_ping_at def initialize(url, opts={}) @url = url @@ -27,12 +28,13 @@ def initialize(url, opts={}) loop do if @websocket if @state == :connect - if Time.now.to_i - @last_ping_at > @ping_interval/1000 + if @ping_state == 'ready_to_ping' and Time.now.to_i - @last_pong_at > @ping_interval/1000 @websocket.send "2" ## ping @last_ping_at = Time.now.to_i + @ping_state = 'waiting_pong' end end - if @websocket.open? and Time.now.to_i - @last_pong_at > @ping_timeout/1000 + if @websocket.open? and @ping_state == 'waiting_pong' and Time.now.to_i - @last_ping_at > @ping_timeout/1000 @websocket.close @state = :disconnect __emit :disconnect @@ -80,12 +82,14 @@ def connect this.session_id = body["sid"] || "no_sid" this.ping_interval = body["pingInterval"] || 25000 this.ping_timeout = body["pingTimeout"] || 60000 + this.ping_state = 'ready_to_ping' this.last_ping_at = Time.now.to_i this.last_pong_at = Time.now.to_i this.state = :connect this.__emit :connect when 3 ## pong this.last_pong_at = Time.now.to_i + this.ping_state = 'ready_to_ping' when 41 ## disconnect from server this.websocket.close if this.websocket.open? this.state = :disconnect From 93732ca52f1f4ec39be042ad34ac43c910f63049 Mon Sep 17 00:00:00 2001 From: Andres Espinosa Date: Thu, 29 Oct 2020 12:54:50 -0300 Subject: [PATCH 2/2] feat(connect): change default pingTimeout to 5000, socket.io default --- lib/socket.io-client-simple/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/socket.io-client-simple/client.rb b/lib/socket.io-client-simple/client.rb index b85ae08..546901a 100644 --- a/lib/socket.io-client-simple/client.rb +++ b/lib/socket.io-client-simple/client.rb @@ -81,7 +81,7 @@ def connect body = JSON.parse body rescue next this.session_id = body["sid"] || "no_sid" this.ping_interval = body["pingInterval"] || 25000 - this.ping_timeout = body["pingTimeout"] || 60000 + this.ping_timeout = body["pingTimeout"] || 5000 this.ping_state = 'ready_to_ping' this.last_ping_at = Time.now.to_i this.last_pong_at = Time.now.to_i