Class: SocketIO::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/socketio-client.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(host, session_id, heartbeat_timeout, connection_timeout, supported_transports, options = {}) ⇒ Client

The state of the Socket.IO socket can be disconnected, disconnecting, connected and connecting. The transport connection can be closed, closing, open, and opening.



32
33
34
35
36
37
38
39
40
41
# File 'lib/socketio-client.rb', line 32

def initialize(host, session_id, heartbeat_timeout, connection_timeout, supported_transports, options = {})
  @host = host
  @session_id = session_id
  @hb_timeout = heartbeat_timeout
  @connect_timeout = connection_timeout
  @supported_transports = supported_transports
  @options = options
  @reconnect = options[:reconnect]
  @on_event = {}
end

Instance Method Details

#after_start(&block) ⇒ Object



128
129
130
# File 'lib/socketio-client.rb', line 128

def after_start(&block)
  @after_start = block
end

#before_start(&block) ⇒ Object



124
125
126
# File 'lib/socketio-client.rb', line 124

def before_start(&block)
  @before_start = block
end

#connect_transportObject



52
53
54
55
56
57
58
# File 'lib/socketio-client.rb', line 52

def connect_transport
  if @supported_transports.include? "websocket"
    @transport = WebSocket.new("ws://#{@host}/socket.io/1/websocket/#{@session_id}")
  else
    raise "We only support WebSockets.. and this server doesnt like web sockets.. O NO!!"
  end
end

#disconnectObject



91
92
93
# File 'lib/socketio-client.rb', line 91

def disconnect
  @transport.send("0::")
end

#disconnectedObject



95
96
97
98
99
100
# File 'lib/socketio-client.rb', line 95

def disconnected
  if @reconnect
    connect_transport
    start_recieve_loop
  end
end

#joinObject



102
103
104
# File 'lib/socketio-client.rb', line 102

def join
  @thread.join
end

#on_ack(&block) ⇒ Object



156
157
158
# File 'lib/socketio-client.rb', line 156

def on_ack(&block)
  @on_ack = block
end

#on_connect(&block) ⇒ Object



136
137
138
# File 'lib/socketio-client.rb', line 136

def on_connect(&block)
  @on_connect = block
end

#on_disconnect(&block) ⇒ Object



132
133
134
# File 'lib/socketio-client.rb', line 132

def on_disconnect(&block)
  @on_disconnect = block
end

#on_error(&block) ⇒ Object



160
161
162
# File 'lib/socketio-client.rb', line 160

def on_error(&block)
  @on_error = block
end

#on_event(name, &block) ⇒ Object



152
153
154
# File 'lib/socketio-client.rb', line 152

def on_event(name, &block)
  @on_event[name] = block
end

#on_heartbeat(&block) ⇒ Object



140
141
142
# File 'lib/socketio-client.rb', line 140

def on_heartbeat(&block)
  @on_heartbeat = block
end

#on_json_message(&block) ⇒ Object



148
149
150
# File 'lib/socketio-client.rb', line 148

def on_json_message(&block)
  @on_json_message = block
end

#on_message(&block) ⇒ Object



144
145
146
# File 'lib/socketio-client.rb', line 144

def on_message(&block)
  @on_message = block
end

#on_noop(&block) ⇒ Object



164
165
166
# File 'lib/socketio-client.rb', line 164

def on_noop(&block)
  @on_noop = block
end

#send_event(name, hash) ⇒ Object Also known as: emit



119
120
121
# File 'lib/socketio-client.rb', line 119

def send_event(name, hash)
  @transport.send("5:::#{{name: name, args: [hash]}.to_json}") # rescue false
end

#send_heartbeatObject



106
107
108
# File 'lib/socketio-client.rb', line 106

def send_heartbeat
  @transport.send("2::") #rescue false
end

#send_json_message(hash) ⇒ Object



115
116
117
# File 'lib/socketio-client.rb', line 115

def send_json_message(hash)
  @transport.send("4:::#{hash.to_json}") # rescue false
end

#send_message(string) ⇒ Object Also known as: send



110
111
112
# File 'lib/socketio-client.rb', line 110

def send_message(string)
  @transport.send("3:::#{string}") #rescue false
end

#startObject



43
44
45
46
47
48
49
50
# File 'lib/socketio-client.rb', line 43

def start
  self.instance_eval(&@before_start) if @before_start
  connect_transport
  start_recieve_loop
  self.instance_eval(&@after_start) if @after_start
  @thread.join unless @options[:sync]
  self
end

#start_recieve_loopObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/socketio-client.rb', line 60

def start_recieve_loop
  @thread = Thread.new() do
    while data = @transport.receive()
      decoded = Parser.decode(data)
      case decoded[:type]
      when '0'
        @on_disconnect.call if @on_disconnect
      when '1'
        @on_connect.call if @on_connect
      when '2'
        send_heartbeat
        @on_heartbeat.call if @on_heartbeat
      when '3'
        @on_message.call decoded[:data] if @on_message
      when '4'
        @on_json_message.call decoded[:data] if @on_json_message
      when '5'
        message = JSON.parse(decoded[:data])
        @on_event[message['name']].call message['args'] if @on_event[message['name']]
      when '6'
        @on_error.call decoded[:data] if @on_error
      when '7'
        @on_ack.call if @on_ack
      when '8'
        @on_noop.call if @on_noop
      end
    end
  end
  @thread
end