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?, start_event_machine, stop_event_machine

Methods included from Event

included

Constructor Details

#initialize(options) ⇒ WebSocketEventMachine

Returns a new instance of WebSocketEventMachine.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 11

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.



9
10
11
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 9

def socket
  @socket
end

Instance Method Details

#connectObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 24

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

  self.socket.onopen do
    self.connected = true
    trigger :open
  end

  self.socket.onmessage do |msg, type|
    trigger :message, self.serializer.deserialize(msg)
  end

  self.socket.onclose do |code, reason|
    self.connected = false
    trigger :close, reason
  end
end

#disconnectObject



45
46
47
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 45

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

#send_message(msg) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/wamp/client/transport/web_socket_event_machine.rb', line 49

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