Class: WebsocketClientLite

Inherits:
Object
  • Object
show all
Defined in:
lib/websocket_client_lite.rb,
lib/websocket_client_lite/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, logger: nil) ⇒ WebsocketClientLite

Returns a new instance of WebsocketClientLite.



12
13
14
15
16
17
# File 'lib/websocket_client_lite.rb', line 12

def initialize(url, logger: nil)
  @url = url
  @logger = logger || Logger.new(File::NULL)
  @incoming_queue = Queue.new
  @state = nil
end

Instance Attribute Details

#handshake_objectObject

Returns the value of attribute handshake_object.



10
11
12
# File 'lib/websocket_client_lite.rb', line 10

def handshake_object
  @handshake_object
end

#incoming_queueObject

Returns the value of attribute incoming_queue.



10
11
12
# File 'lib/websocket_client_lite.rb', line 10

def incoming_queue
  @incoming_queue
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/websocket_client_lite.rb', line 10

def logger
  @logger
end

#socketObject

Returns the value of attribute socket.



10
11
12
# File 'lib/websocket_client_lite.rb', line 10

def socket
  @socket
end

#stateObject

Returns the value of attribute state.



10
11
12
# File 'lib/websocket_client_lite.rb', line 10

def state
  @state
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/websocket_client_lite.rb', line 10

def url
  @url
end

Instance Method Details

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



75
76
77
78
79
80
# File 'lib/websocket_client_lite.rb', line 75

def close(code = nil, data = nil)
  if @state == :open
    @state = :closing_by_client
    send_frame(:close, data, code: code)
  end
end

#each_payload(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/websocket_client_lite.rb', line 41

def each_payload(&block)
  thread = nil
  return unless @state == :open

  thread = start_incoming_thread

  while %i[open closing_by_client].include?(@state)
    frame = @incoming_queue.pop
    break unless frame

    case frame.type
    when :ping
      send_pong(frame.data)
    when :pong
      # no-op
    when :text, :binary
      block.yield(frame.data)
    when :close
      if @state == :open
        @state = :closing_by_server
        send_close(frame.data)
      end
      @state = :closed
    else
      raise "Unknown frame. type: #{frame.type}, data: #{frame.data}"
    end
  end
ensure
  thread&.join(1)
  @socket&.close
  @socket = nil
  @state = nil
end

#handshakeObject



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

def handshake
  @state = :connecting
  @handshake_object = WebSocket::Handshake::Client.new(url: @url)
  @socket = make_socket(@handshake_object.host, @handshake_object.port, @handshake_object.secure)
  @socket.write(@handshake_object.to_s)

  until @handshake_object.finished?
    byte = @socket.read(1)
    @handshake_object << byte
  end

  @handshake_object.valid?
  @state = :open
  true
rescue StandardError => e
  @logger.error("Handshake failed: #{e.inspect}")
  @socket&.close
  @socket = nil
  @state = nil
  false
end

#send_binary(data) ⇒ Object



98
99
100
# File 'lib/websocket_client_lite.rb', line 98

def send_binary(data)
  send_frame(:binary, data)
end

#send_close(code) ⇒ Object



90
91
92
# File 'lib/websocket_client_lite.rb', line 90

def send_close(code)
  send_frame(:close, nil, code: code)
end

#send_ping(code, data) ⇒ Object



82
83
84
# File 'lib/websocket_client_lite.rb', line 82

def send_ping(code, data)
  send_frame(:ping, data, code: code)
end

#send_pong(data) ⇒ Object



86
87
88
# File 'lib/websocket_client_lite.rb', line 86

def send_pong(data)
  send_frame(:pong, data)
end

#send_text(data) ⇒ Object



94
95
96
# File 'lib/websocket_client_lite.rb', line 94

def send_text(data)
  send_frame(:text, data)
end