Class: Wamp::Client::Transport::WebSocketEventMachine

Inherits:
EventMachineBase show all
Defined in:
lib/wamp/client/transport/web_socket_event_machine.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?, #on, #on_close, #on_error, #on_message, #on_open, start_event_machine, stop_event_machine

Constructor Details

#initialize(options) ⇒ WebSocketEventMachine

Returns a new instance of WebSocketEventMachine.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 38

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

  # Only make them include the gem if they are going to use it
  require 'websocket-eventmachine-client'

  # Raise an exception if proxy was included (not supported)
  if self.proxy != nil
    raise RuntimeError, "The WebSocketEventMachine transport does not support 'proxy'.  Try using 'faye-websocket' transport instead"
  end
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



36
37
38
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 36

def socket
  @socket
end

Instance Method Details

#connectObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 51

def connect
  self.socket = WebSocket::EventMachine::Client.connect(
      :uri => self.uri,
      :headers => self.headers
  )

  self.socket.onopen do
    self.connected = true
    @on_open.call if @on_open
  end

  self.socket.onmessage do |msg, type|
    @on_message.call(self.serializer.deserialize(msg)) if @on_message
  end

  self.socket.onclose do |code, reason|
    self.connected = false
    @on_close.call(reason) if @on_close
  end
end

#disconnectObject



72
73
74
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 72

def disconnect
  self.connected = !self.socket.close  # close returns 'true' if the connection was closed immediately
end

#send_message(msg) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 76

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