Class: WebSocket::EventMachine::Client

Inherits:
Base
  • Object
show all
Defined in:
lib/websocket/eventmachine/client.rb,
lib/websocket/eventmachine/client/version.rb

Overview

WebSocket Client (using EventMachine)

Examples:

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

Constant Summary

VERSION =
'1.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Initialize connection

Parameters:

  • args (Hash)

    Arguments for connection

Options Hash (args):

  • :host (String)

    The host IP/DNS name

  • :port (Integer)

    The port to connect too(default = 80)

  • :version (Integer)

    Version of protocol to use(default = 13)

  • :headers (Hash)

    HTTP headers to use in the handshake

  • :ssl (Boolean)

    Force SSL/TLS connection



64
65
66
# File 'lib/websocket/eventmachine/client.rb', line 64

def initialize(args)
  @args = args
end

Class Method Details

.connect(args = {}) ⇒ Object

Connect to websocket server

Parameters:

  • args (Hash) (defaults to: {})

    The request arguments

Options Hash (args):

  • :host (String)

    The host IP/DNS name

  • :port (Integer)

    The port to connect too(default = 80)

  • :uri (String)

    Full URI for server(optional - use instead of host/port combination)

  • :version (Integer)

    Version of protocol to use(default = 13)

  • :headers (Hash)

    HTTP headers to use in the handshake

  • :ssl (Boolean)

    Force SSL/TLS connection



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/websocket/eventmachine/client.rb', line 26

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]
    port ||= 443
  else
    port ||= 80
  end

  ::EventMachine.connect host, port, self, args
end

.connect_unix_domain(socketname, args = {}) ⇒ Object

Make a websocket connection to a UNIX-domain socket.

Parameters:

  • socketname (String)

    Unix domain socket (local fully-qualified path)

  • args (Hash) (defaults to: {})

    Arguments for connection

Options Hash (args):

  • :version (Integer)

    Version of protocol to use(default = 13)

  • :headers (Hash)

    HTTP headers to use in the handshake



51
52
53
54
55
# File 'lib/websocket/eventmachine/client.rb', line 51

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

Instance Method Details

#close(code = 1000, data = nil) ⇒ Boolean

Close connection

Returns:

  • (Boolean)

    true if connection is closed immediately, false if waiting for other side to close connection



155
# File 'lib/websocket/eventmachine/client.rb', line 155

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

#connection_completedObject

Called by EventMachine after connecting. Sends handshake to server or starts SSL/TLS Eventmachine internal



84
85
86
87
88
89
90
# File 'lib/websocket/eventmachine/client.rb', line 84

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

#onclose(&blk) ⇒ Object

Called when connection is closed. No parameters are passed to block



122
# File 'lib/websocket/eventmachine/client.rb', line 122

def onclose(&blk); super; end

#onerror(&blk) ⇒ Object

Called when error occurs. One parameter passed to block:

error - string with error message


127
# File 'lib/websocket/eventmachine/client.rb', line 127

def onerror(&blk); super; end

#onmessage(&blk) ⇒ Object

Called when message is received. Two parameters passed to block:

message - string with received message
type - type of message. Valid values are :text and :binary


133
# File 'lib/websocket/eventmachine/client.rb', line 133

def onmessage(&blk); super; end

#onopen(&blk) ⇒ Object

Called when connection is opened. No parameters are passed to block



118
# File 'lib/websocket/eventmachine/client.rb', line 118

def onopen(&blk); super; end

#onping(&blk) ⇒ Object

Called when ping message is received One parameter passed to block:

message - string with ping message


138
# File 'lib/websocket/eventmachine/client.rb', line 138

def onping(&blk); super; end

#onpong(&blk) ⇒ Object

Called when pong message is received One parameter passed to block:

message - string with pong message


143
# File 'lib/websocket/eventmachine/client.rb', line 143

def onpong(&blk); super; end

#ping(data = '') ⇒ Boolean

Send ping message

Returns:

  • (Boolean)

    false if protocol version is not supporting ping requests



159
# File 'lib/websocket/eventmachine/client.rb', line 159

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

#pong(data = '') ⇒ Boolean

Send pong message

Returns:

  • (Boolean)

    false if protocol version is not supporting pong requests



163
# File 'lib/websocket/eventmachine/client.rb', line 163

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

#post_initObject

Called after initialize of connection, but before connecting to server Eventmachine internal



75
76
77
78
# File 'lib/websocket/eventmachine/client.rb', line 75

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

#send(data, args = {}) ⇒ Boolean

Send data

Parameters:

  • data (String)

    Data to send

  • args (Hash) (defaults to: {})

    Arguments for send

Options Hash (args):

  • :type (String)

    Type of frame to send - available types are “text”, “binary”, “ping”, “pong” and “close”

  • :code (Integer)

    Code for close frame

Returns:

  • (Boolean)

    true if data was send, otherwise call on_error if needed



151
# File 'lib/websocket/eventmachine/client.rb', line 151

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

#ssl_handshake_completedObject

Called by EventMachine after SSL/TLS handshake. Sends websocket handshake Eventmachine internal



96
97
98
# File 'lib/websocket/eventmachine/client.rb', line 96

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