Class: XRemoteBot::WS

Inherits:
Object
  • Object
show all
Defined in:
lib/xremotebot.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, path) ⇒ WS

Returns a new instance of WS.

Raises:

  • (Exception)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/xremotebot.rb', line 9

def initialize(host, port, path)
  @sock = TCPSocket.new host, port
  url = %Q(ws://#{host}:#{port}/#{path})
  @handshake = WebSocket::Handshake::Client.new(url: url)
  @sock.puts(@handshake)

  @handshake << @sock.recv(4096)
  while !@handshake.finished?
    @handshake << @sock.recv(4096)
  end

  raise Exception unless @handshake.valid?

end

Instance Method Details

#receiveObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xremotebot.rb', line 33

def receive()
  frame = WebSocket::Frame::Incoming::Server.new(
    version: @handshake.version
  )
  frame << @sock.recv(4096)
  data = ''
  loop do
    part = frame.next
    break if part.nil?
    data += part.to_s
  end
  return data
end

#send(data) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/xremotebot.rb', line 24

def send(data)
  frame = WebSocket::Frame::Outgoing::Server.new(
    version: @handshake.version,
    data: data,
    type: :text
  )
  @sock.send frame.to_s, 0
end