Class: WebSocket::EventMachine::Server

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

Overview

WebSocket Server (using EventMachine)

Examples:

WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => 8080) do |ws|
  ws.onopen    { ws.send "Hello Client!"}
  ws.onmessage { |msg| ws.send "Pong: #{msg}" }
  ws.onclose   { puts "WebSocket closed" }
  ws.onerror   { |e| puts "Error: #{e}" }
end

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Server

Initialize connection

Parameters:

  • args (Hash)

    Arguments for server

Options Hash (args):

  • :debug (Boolean)

    Should server log debug data?

  • :secure (Boolean)

    If true then server will run over SSL

  • :secure_proxy (Boolean)

    If true then server will use wss protocol but will not encrypt connection. Usefull for sll proxies.

  • :tls_options (Hash)

    Options for SSL if secure = true


36
37
38
39
40
41
# File 'lib/websocket/eventmachine/server.rb', line 36

def initialize(args)
  @debug = !!args[:debug]
  @secure = !!args[:secure]
  @secure_proxy = args[:secure_proxy] || @secure
  @tls_options = args[:tls_options] || {}
end

Class Method Details

.start(options, &block) ⇒ Object

Start server

Parameters:

  • options (Hash)

    The request arguments

  • args (Hash)

    a customizable set of options


24
25
26
27
28
# File 'lib/websocket/eventmachine/server.rb', line 24

def self.start(options, &block)
  ::EventMachine::start_server(options[:host], options[:port], self, options) do |c|
    block.call(c)
  end
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


114
# File 'lib/websocket/eventmachine/server.rb', line 114

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

#onclose(&blk) ⇒ Object

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


81
# File 'lib/websocket/eventmachine/server.rb', line 81

def onclose(&blk); super; end

#onerror(&blk) ⇒ Object

Called when error occurs. One parameter passed to block:

error - string with error message

86
# File 'lib/websocket/eventmachine/server.rb', line 86

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

92
# File 'lib/websocket/eventmachine/server.rb', line 92

def onmessage(&blk); super; end

#onopen(&blk) ⇒ Object

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


77
# File 'lib/websocket/eventmachine/server.rb', line 77

def onopen(&blk); super; end

#onping(&blk) ⇒ Object

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

message - string with ping message

97
# File 'lib/websocket/eventmachine/server.rb', line 97

def onping(&blk); super; end

#onpong(&blk) ⇒ Object

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

message - string with pong message

102
# File 'lib/websocket/eventmachine/server.rb', line 102

def onpong(&blk); super; end

#ping(data = '') ⇒ Boolean

Send ping message

Returns:

  • (Boolean)

    false if protocol version is not supporting ping requests


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

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

#pong(data = '') ⇒ Boolean

Send pong message

Returns:

  • (Boolean)

    false if protocol version is not supporting pong requests


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

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

#post_initObject

Eventmachine internal


49
50
51
52
53
# File 'lib/websocket/eventmachine/server.rb', line 49

def post_init
  @state = :connecting
  @handshake = ::WebSocket::Handshake::Server.new(:secure => @secure_proxy)
  start_tls(@tls_options) if @secure
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


110
# File 'lib/websocket/eventmachine/server.rb', line 110

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