Class: WebSocket::EventMachine::Server
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- WebSocket::EventMachine::Server
- Defined in:
- lib/websocket/eventmachine/server.rb,
lib/websocket/eventmachine/server/version.rb
Overview
WebSocket Server (using EventMachine)
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
-
.start(options, &block) ⇒ Object
Start server.
Instance Method Summary collapse
-
#close(code = 1000, data = nil) ⇒ Boolean
Close connection.
-
#initialize(args) ⇒ Server
constructor
Initialize connection.
-
#onclose(&blk) ⇒ Object
Called when connection is closed.
-
#onerror(&blk) ⇒ Object
Called when error occurs.
-
#onmessage(&blk) ⇒ Object
Called when message is received from server.
-
#onopen(&blk) ⇒ Object
Called when connection is opened.
-
#onping(&blk) ⇒ Object
Called when ping message is received from server.
-
#onpong(&blk) ⇒ Object
Called when pond message is received from server.
-
#ping(data = '') ⇒ Boolean
Send ping message to client.
-
#pong(data = '') ⇒ Boolean
Send pong message to client.
-
#post_init ⇒ Object
Eventmachine internal.
-
#receive_data(data) ⇒ Object
Eventmachine internal.
-
#send(data, args = {}) ⇒ Boolean
Send data to client.
-
#unbind ⇒ Object
Eventmachine internal.
Constructor Details
#initialize(args) ⇒ Server
Initialize connection
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
25 26 27 28 29 |
# File 'lib/websocket/eventmachine/server.rb', line 25 def self.start(, &block) ::EventMachine::start_server([:host], [:port], self, ) do |c| block.call(c) end end |
Instance Method Details
#close(code = 1000, data = nil) ⇒ Boolean
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 (&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
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
118 119 120 |
# File 'lib/websocket/eventmachine/server.rb', line 118 def pong(data = '') send(data, :type => :pong) end |
#post_init ⇒ Object
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
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 |
#unbind ⇒ Object
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 |