Class: Wamp::Client::Transport::FayeWebSocket

Inherits:
EventMachineBase show all
Defined in:
lib/wamp/client/transport/faye_web_socket.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#connected, #headers, #protocol, #proxy, #serializer, #uri

Instance Method Summary collapse

Methods inherited from EventMachineBase

add_tick_loop, add_timer, start_event_machine, stop_event_machine

Methods inherited from Base

add_tick_loop, add_timer, #add_timer, #connected?, start_event_machine, stop_event_machine

Methods included from Event

included

Constructor Details

#initialize(options) ⇒ FayeWebSocket

Returns a new instance of FayeWebSocket.



10
11
12
13
14
15
16
# File 'lib/wamp/client/transport/faye_web_socket.rb', line 10

def initialize(options)
  super(options)
  self.socket = nil

  # Only make them include the gem if they are going to use it
  require 'faye/websocket'
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



8
9
10
# File 'lib/wamp/client/transport/faye_web_socket.rb', line 8

def socket
  @socket
end

Instance Method Details

#connectObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wamp/client/transport/faye_web_socket.rb', line 18

def connect
  options = { :headers => self.headers }
  options[:proxy] = self.proxy if self.proxy != nil
  self.socket = Faye::WebSocket::Client.new(self.uri, [self.protocol], options)

  self.socket.on(:open) do |event|
    self.connected = true
    trigger :open
  end

  self.socket.on(:message) do |event|
    trigger :message, self.serializer.deserialize(event.data)
  end

  self.socket.on(:close) do |event|
    self.connected = false
    trigger :close, event.reason
  end

  self.socket.on(:error) do |event|
    trigger :error, event.message
  end
end

#disconnectObject



42
43
44
45
# File 'lib/wamp/client/transport/faye_web_socket.rb', line 42

def disconnect
  self.socket.close
  self.connected = false
end

#send_message(msg) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/wamp/client/transport/faye_web_socket.rb', line 47

def send_message(msg)
  if self.connected
    self.socket.send(self.serializer.serialize(msg))
  else
    raise RuntimeError, "Socket must be open to call 'send_message'"
  end
end