Class: WebSocket::EventMachine::Server

Inherits:
EventMachine::Connection
  • 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.0'

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



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

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



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

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 server to close connection



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/websocket/eventmachine/server.rb', line 98

def close(code = 1000, data = nil)
  if @state == :open
    @state = :closing
    return false if send(data, :type => :close, :code => code)
  else
    send(data, :type => :close) if @state == :closing
    @state = :closed
  end
  close_connection_after_writing
  true
end

#onclose(&blk) ⇒ Object

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



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

def onclose(&blk);    @onclose = blk;   end

#onerror(&blk) ⇒ Object

Called when error occurs. One parameter passed to block:

error - string with error message


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

def onerror(&blk);    @onerror = blk;   end

#onmessage(&blk) ⇒ Object

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

message - string with message sent to server
type - type of message. Valid values are :text and :binary


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

def onmessage(&blk);  @onmessage = blk; end

#onopen(&blk) ⇒ Object

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



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

def onopen(&blk);     @onopen = blk;    end

#onping(&blk) ⇒ Object

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

message - string with ping message


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

def onping(&blk);     @onping = blk;    end

#onpong(&blk) ⇒ Object

Called when pond message is received from server. One parameter passed to block:

message - string with pong message


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

def onpong(&blk);     @onpong = blk;    end

#ping(data = '') ⇒ Boolean

Send ping message to client

Returns:

  • (Boolean)

    false if protocol version is not supporting ping requests



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

def ping(data = '')
  send(data, :type => :ping)
end

#pong(data = '') ⇒ Boolean

Send pong message to client

Returns:

  • (Boolean)

    false if protocol version is not supporting pong requests



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

def pong(data = '')
  send(data, :type => :pong)
end

#post_initObject

Eventmachine internal



128
129
130
131
132
# File 'lib/websocket/eventmachine/server.rb', line 128

def post_init
  @state = :connecting
  @handshake = WebSocket::Handshake::Server.new(:secure => @secure_proxy)
  start_tls(@tls_options) if @secure
end

#receive_data(data) ⇒ Object

Eventmachine internal



136
137
138
139
140
141
142
143
# File 'lib/websocket/eventmachine/server.rb', line 136

def receive_data(data)
  debug "Received raw: ", data
  case @state
  when :connecting then handle_connecting(data)
  when :open then handle_open(data)
  when :closing then handle_closing(data)
  end
end

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

Send data to client

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/websocket/eventmachine/server.rb', line 79

def send(data, args = {})
  type = args[:type] || :text
  unless type == :plain
    frame = WebSocket::Frame::Outgoing::Server.new args.merge(:version => @handshake.version, :data => data)
    if !frame.supported?
      trigger_onerror("Frame type '#{type}' is not supported in protocol version #{@handshake.version}")
      return false
    elsif !frame.require_sending?
      return false
    end
    data = frame.to_s
  end
  debug "Sending raw: ", data
  send_data(data)
  true
end

#unbindObject

Eventmachine internal



147
148
149
150
151
152
153
# File 'lib/websocket/eventmachine/server.rb', line 147

def unbind
  unless @state == :closed
    @state = :closed
    close
    trigger_onclose('')
  end
end