class SocketIO::Client::Simple::Client

Attributes

auto_reconnection[RW]
last_ping_at[RW]
last_pong_at[RW]
ping_interval[RW]
ping_timeout[RW]
reconnecting[RW]
session_id[RW]
state[RW]
url[RW]
websocket[RW]

Public Class Methods

new(url, opts={}) click to toggle source
# File lib/socket.io-client-simple/client.rb, line 18
def initialize(url, opts={})
  @url = url
  @opts = opts
  @opts[:transport] = :websocket
  @reconnecting = false
  @state = :disconnect
  @auto_reconnection = true

  Thread.new do
    loop do
      if @websocket
        if @state == :connect
          if Time.now.to_i - @last_ping_at > @ping_interval/1000
            @websocket.send "2"  ## ping
            @last_ping_at = Time.now.to_i
          end
        end
        if @websocket.open? and Time.now.to_i - @last_pong_at > @ping_timeout/1000
          @websocket.close
          @state = :disconnect
          __emit :disconnect
          reconnect
        end
      end
      sleep 1
    end
  end

end

Public Instance Methods

__emit(event_name, *data)
Alias for: emit
connect() click to toggle source
# File lib/socket.io-client-simple/client.rb, line 49
def connect
  query = @opts.map{|k,v| URI.encode "#{k}=#{v}" }.join '&'
  begin
    @websocket = WebSocket::Client::Simple.connect "#{@url}/socket.io/?#{query}"
  rescue Errno::ECONNREFUSED => e
    @state = :disconnect
    @reconnecting = false
    reconnect
    return
  end
  @reconnecting = false

  this = self

  @websocket.on :error do |err|
    if err.kind_of? Errno::ECONNRESET and this.state == :connect
      this.state = :disconnect
      this.__emit :disconnect
      this.reconnect
      next
    end
    this.__emit :error, err
  end

  @websocket.on :message do |msg|
    next unless msg.data =~ /^\d+/
    code, body = msg.data.scan(/^(\d+)(.*)$/)[0]
    code = code.to_i
    case code
    when 0  ##  socket.io 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.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
    when 41  ## disconnect from server
      this.websocket.close if this.websocket.open?
      this.state = :disconnect
      this.__emit :disconnect
      reconnect
    when 42  ## data
      data = JSON.parse body rescue next
      event_name = data.shift
      this.__emit event_name, *data
    end
  end

  return self
end
disconnect() click to toggle source
# File lib/socket.io-client-simple/client.rb, line 123
def disconnect
  @auto_reconnection = false
  @websocket.close
  @state = :disconnect
end
emit(event_name, *data) click to toggle source
# File lib/socket.io-client-simple/client.rb, line 116
def emit(event_name, *data)
  return unless open?
  return unless @state == :connect
  data.unshift event_name
  @websocket.send "42#{data.to_json}"
end
Also aliased as: __emit
open?() click to toggle source
# File lib/socket.io-client-simple/client.rb, line 112
def open?
  @websocket and @websocket.open?
end
reconnect() click to toggle source
# File lib/socket.io-client-simple/client.rb, line 104
def reconnect
  return unless @auto_reconnection
  return if @reconnecting
  @reconnecting = true
  sleep rand(5) + 5
  connect
end