class WebsocketConnection

Websocket connection

Constants

S_WS_GUID
UPGRADE_RESPONSE

Public Class Methods

new(client, headers) click to toggle source
# File lib/polyphony/websocket.rb, line 10
def initialize(client, headers)
  @client = client
  @headers = headers
  setup(headers)
end

Public Instance Methods

<<(data)
Alias for: send
recv() click to toggle source
# File lib/polyphony/websocket.rb, line 34
def recv
  loop do
    data = @client.readpartial(8192)
    break nil unless data

    @reader << data
    if (msg = @reader.next)
      break msg.to_s
    end
  end
end
send(data) click to toggle source
# File lib/polyphony/websocket.rb, line 46
def send(data)
  frame = ::WebSocket::Frame::Outgoing::Server.new(
    version: @version, data: data, type: :text
  )
  @client << frame.to_s
end
Also aliased as: <<
setup(headers) click to toggle source
# File lib/polyphony/websocket.rb, line 25
def setup(headers)
  key = headers['Sec-WebSocket-Key']
  @version = headers['Sec-WebSocket-Version'].to_i
  accept = Digest::SHA1.base64digest([key, S_WS_GUID].join)
  @client << format(UPGRADE_RESPONSE, accept: accept)

  @reader = ::WebSocket::Frame::Incoming::Server.new(version: @version)
end