Class: WampClient::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_timer, start_event_machine, stop_event_machine

Methods inherited from Base

add_timer, #add_timer, #connected?, #on, #on_close, #on_error, #on_message, #on_open, start_event_machine, stop_event_machine

Constructor Details

#initialize(options) ⇒ FayeWebSocket

Returns a new instance of FayeWebSocket.



36
37
38
39
40
41
42
# File 'lib/wamp_client/transport/faye_web_socket.rb', line 36

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.



34
35
36
# File 'lib/wamp_client/transport/faye_web_socket.rb', line 34

def socket
  @socket
end

Instance Method Details

#connectObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wamp_client/transport/faye_web_socket.rb', line 44

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
    @on_open.call if @on_open
  end

  self.socket.on(:message) do |event|
    @on_message.call(self.serializer.deserialize(event.data)) if @on_message
  end

  self.socket.on(:close) do |event|
    self.connected = false
    @on_close.call(event.reason) if @on_close
  end

  self.socket.on(:error) do |event|
    @on_error.call(event.message) if @on_error
  end
end

#disconnectObject



68
69
70
71
# File 'lib/wamp_client/transport/faye_web_socket.rb', line 68

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

#send_message(msg) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/wamp_client/transport/faye_web_socket.rb', line 73

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