class WebSocket::Connection

Attributes

socket[R]

Public Class Methods

establish(socket) click to toggle source
# File lib/local_server.rb, line 12
def self.establish(socket)
  handshake = WebSocket::Handshake::Server.new
  handshake << socket.gets until handshake.finished?

  raise "Malformed handshake received from WebSocket client" unless handshake.valid?

  socket.puts(handshake.to_s)
  Connection.new(socket, handshake)
end
new(socket, handshake) click to toggle source
# File lib/local_server.rb, line 22
def initialize(socket, handshake)
  @socket    = socket
  @handshake = handshake
end

Public Instance Methods

each_message() { |message| ... } click to toggle source
# File lib/local_server.rb, line 32
def each_message
  frame = WebSocket::Frame::Incoming::Server.new(version: @handshake.version)
  frame << @socket.read_nonblock(4096)
  while message = frame.next
    yield message
  end
end
puts(message) click to toggle source
# File lib/local_server.rb, line 27
def puts(message)
  frame = WebSocket::Frame::Outgoing::Server.new(version: @handshake.version, data: message, type: :text)
  @socket.puts(frame.to_s)
end