class WebSocket::EventMachine::Client

ws.send “data”
ws.onmessage { |msg| ws.send “Pong: #{msg}” }
ws = WebSocket::EventMachine::Client.connect(:host => “0.0.0.0”, :port => 8080)
@example
WebSocket Client (using EventMachine)

def self.connect(args = {})

Options Hash: (**args)
  • :tls (Hash) -- TLS options hash to be passed to EM start_tls
  • :ssl (Boolean) -- Force SSL/TLS connection
  • :headers (Hash) -- HTTP headers to use in the handshake
  • :version (Integer) -- Version of protocol to use(default = 13)
  • :uri (String) -- Full URI for server(optional - use instead of host/port combination)
  • :port (Integer) -- The port to connect too(default = 80)
  • :host (String) -- The host IP/DNS name

Parameters:
  • args (Hash) -- The request arguments
def self.connect(args = {})
  host = nil
  port = nil
  if args[:uri]
    uri = URI.parse(args[:uri])
    host = uri.host
    port = uri.port
    args[:ssl] = true if uri.scheme == 'wss'
  end
  host = args[:host] if args[:host]
  port = args[:port] if args[:port]
  if args[:ssl]
    args[:tls] ||= {}
    args[:tls][:sni_hostname] ||= host
    port ||= 443
  else
    port ||= 80
  end
  ::EventMachine.connect host, port, self, args
end

def self.connect_unix_domain(socketname, args = {})

Options Hash: (**args)
  • :headers (Hash) -- HTTP headers to use in the handshake
  • :version (Integer) -- Version of protocol to use(default = 13)

Parameters:
  • args (Hash) -- Arguments for connection
  • socketname (String) -- Unix domain socket (local fully-qualified path)
def self.connect_unix_domain(socketname, args = {})
  fail ArgumentError, 'invalid socket' unless File.socket?(socketname)
  args[:host] ||= 'localhost'
  ::EventMachine.connect_unix_domain socketname, self, args
end

def close(code = 1000, data = nil); super; end

Returns:
  • (Boolean) - true if connection is closed immediately, false if waiting for other side to close connection
def close(code = 1000, data = nil); super; end

def connection_completed

Other tags:
    Private: -
def connection_completed
  if @args[:ssl]
    start_tls @args[:tls]
  else
    send(@handshake.to_s, :type => :plain)
  end
end

def incoming_frame

def incoming_frame
  ::WebSocket::Frame::Incoming::Client
end

def initialize(args)

Options Hash: (**args)
  • :ssl (Boolean) -- Force SSL/TLS connection
  • :headers (Hash) -- HTTP headers to use in the handshake
  • :version (Integer) -- Version of protocol to use(default = 13)
  • :port (Integer) -- The port to connect too(default = 80)
  • :host (String) -- The host IP/DNS name

Parameters:
  • args (Hash) -- Arguments for connection
def initialize(args)
  @args = args
end

def onclose(&blk); super; end

No parameters are passed to block
Called when connection is closed.
def onclose(&blk); super; end

def onerror(&blk); super; end

error - string with error message
One parameter passed to block:
Called when error occurs.
def onerror(&blk); super; end

def onmessage(&blk); super; end

type - type of message. Valid values are :text and :binary
message - string with received message
Two parameters passed to block:
Called when message is received.
def onmessage(&blk); super; end

def onopen(&blk); super; end

No parameters are passed to block
Called when connection is opened.
def onopen(&blk); super; end

def onping(&blk); super; end

message - string with ping message
One parameter passed to block:
Called when ping message is received
def onping(&blk); super; end

def onpong(&blk); super; end

message - string with pong message
One parameter passed to block:
Called when pong message is received
def onpong(&blk); super; end

def outgoing_frame

def outgoing_frame
  ::WebSocket::Frame::Outgoing::Client
end

def ping(data = ''); super; end

Returns:
  • (Boolean) - false if protocol version is not supporting ping requests
def ping(data = ''); super; end

def pong(data = ''); super; end

Returns:
  • (Boolean) - false if protocol version is not supporting pong requests
def pong(data = ''); super; end

def post_init

Other tags:
    Private: -
def post_init
  @state = :connecting
  @handshake = ::WebSocket::Handshake::Client.new(@args)
end

def send(data, args = {}); super; end

Returns:
  • (Boolean) - true if data was send, otherwise call on_error if needed

Options Hash: (**args)
  • :code (Integer) -- Code for close frame
  • :type (String) -- Type of frame to send - available types are "text", "binary", "ping", "pong" and "close"

Parameters:
  • args (Hash) -- Arguments for send
  • data (String) -- Data to send
def send(data, args = {}); super; end

def ssl_handshake_completed

Other tags:
    Private: -
def ssl_handshake_completed
  send(@handshake.to_s, :type => :plain)
end