Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/socket.io-client-simple/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -79,13 +81,15 @@ 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
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
Expand Down