Class: Wamp::Connection::WebsocketClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wamp/connection/websocket_client.rb

Overview

handles opening connection and providing callbacks when data is avaialble

Constant Summary collapse

CONNECTING =
0
OPEN =
1
CLOSING =
2
CLOSED =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_target, protocols) ⇒ WebsocketClient

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wamp/connection/websocket_client.rb', line 17

def initialize(event_target, protocols) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  @event_target = event_target

  @driver = @driver_started = nil
  @close_params = ["", 1006]

  @url = event_target.url
  @ready_state = CONNECTING

  @driver = WebSocket::Driver.client(self, protocols: protocols)

  @driver.on(:open)     { |_e| open }
  @driver.on(:message)  { |e| receive_message(e.data) }
  @driver.on(:close)    { |e| begin_close(e.reason, e.code) }
  @driver.on(:error)    { |e| emit_error(e.message) }

  @stream = Wamp::Connection::Stream.new(self)
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



15
16
17
# File 'lib/wamp/connection/websocket_client.rb', line 15

def url
  @url
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/wamp/connection/websocket_client.rb', line 87

def alive?
  @ready_state == OPEN
end

#close(code = nil, reason = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wamp/connection/websocket_client.rb', line 69

def close(code = nil, reason = nil)
  code   ||= 1000
  reason ||= ""

  unless code == 1000 || (code >= 3000 && code <= 4999)
    raise ArgumentError, "Failed to execute 'close' on WebSocket: " \
                         "The code must be either 1000, or between 3000 and 4999. " \
                         "#{code} is neither."
  end

  @ready_state = CLOSING unless @ready_state == CLOSED
  @driver.close(reason, code)
end

#connection_goneObject



65
66
67
# File 'lib/wamp/connection/websocket_client.rb', line 65

def connection_gone
  finalize_close
end

#parse(data) ⇒ Object



83
84
85
# File 'lib/wamp/connection/websocket_client.rb', line 83

def parse(data)
  @driver.parse(data)
end

#protocolObject

This is populated after successful handeshake



92
93
94
# File 'lib/wamp/connection/websocket_client.rb', line 92

def protocol
  @driver.protocol
end

#runObject



36
37
38
39
# File 'lib/wamp/connection/websocket_client.rb', line 36

def run
  start_driver
  @stream.run
end

#start_driverObject



41
42
43
44
45
46
# File 'lib/wamp/connection/websocket_client.rb', line 41

def start_driver
  return if @driver_started

  @driver_started = true
  @driver.start
end

#transmit(message) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/wamp/connection/websocket_client.rb', line 54

def transmit(message)
  return false if @ready_state > OPEN

  case message
  when Numeric then @driver.text(message.to_s)
  when String  then @driver.text(message)
  when Array   then @driver.binary(message)
  else false
  end
end

#write(data) ⇒ Object



48
49
50
51
52
# File 'lib/wamp/connection/websocket_client.rb', line 48

def write(data)
  @stream.write(data)
rescue StandardError => e
  emit_error(e.message)
end