Class: WebSocket::EventMachine::Base
- Inherits:
-
Connection
- Object
- Connection
- WebSocket::EventMachine::Base
- Defined in:
- lib/websocket/eventmachine/base.rb,
lib/websocket/eventmachine/base/version.rb
Overview
WebSocket Base for Client and Server (using EventMachine)
Constant Summary collapse
- VERSION =
'1.2.0'
Instance Method Summary collapse
-
#close(code = 1000, data = nil) ⇒ Boolean
Close connection.
-
#onclose(&blk) ⇒ Object
Called when connection is closed.
-
#onerror(&blk) ⇒ Object
Called when error occurs.
-
#onmessage(&blk) ⇒ Object
Called when message is received.
-
#onopen(&blk) ⇒ Object
Called when connection is opened.
-
#onping(&blk) ⇒ Object
Called when ping message is received One parameter passed to block: message - string with ping message.
-
#onpong(&blk) ⇒ Object
Called when pond message is received One parameter passed to block: message - string with pong message.
-
#ping(data = '') ⇒ Boolean
Send ping message.
-
#pong(data = '') ⇒ Boolean
Send pong message.
-
#receive_data(data) ⇒ Object
Eventmachine internal.
-
#send(data, args = {}) ⇒ Boolean
Send data.
-
#unbind ⇒ Object
Eventmachine internal.
Instance Method Details
#close(code = 1000, data = nil) ⇒ Boolean
Close connection
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/websocket/eventmachine/base.rb', line 80 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. Two parameters are passed to block:
code - status code
reason - optional reason for closure
31 |
# File 'lib/websocket/eventmachine/base.rb', line 31 def onclose(&blk); @onclose = blk; end |
#onerror(&blk) ⇒ Object
Called when error occurs. One parameter passed to block:
error - string with error
36 |
# File 'lib/websocket/eventmachine/base.rb', line 36 def onerror(&blk); @onerror = blk; end |
#onmessage(&blk) ⇒ Object
Called when message is received. Two parameters passed to block:
- string with received
type - type of . Valid values are :text and :binary
42 |
# File 'lib/websocket/eventmachine/base.rb', line 42 def (&blk); = blk; end |
#onopen(&blk) ⇒ Object
Called when connection is opened. No parameters are passed to block
25 |
# File 'lib/websocket/eventmachine/base.rb', line 25 def onopen(&blk); @onopen = blk; end |
#onping(&blk) ⇒ Object
Called when ping message is received One parameter passed to block:
- string with ping
47 |
# File 'lib/websocket/eventmachine/base.rb', line 47 def onping(&blk); @onping = blk; end |
#onpong(&blk) ⇒ Object
Called when pond message is received One parameter passed to block:
- string with pong
52 |
# File 'lib/websocket/eventmachine/base.rb', line 52 def onpong(&blk); @onpong = blk; end |
#ping(data = '') ⇒ Boolean
Send ping message
94 95 96 |
# File 'lib/websocket/eventmachine/base.rb', line 94 def ping(data = '') send(data, :type => :ping) end |
#pong(data = '') ⇒ Boolean
Send pong message
100 101 102 |
# File 'lib/websocket/eventmachine/base.rb', line 100 def pong(data = '') send(data, :type => :pong) end |
#receive_data(data) ⇒ Object
Eventmachine internal
110 111 112 113 114 115 116 117 |
# File 'lib/websocket/eventmachine/base.rb', line 110 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/websocket/eventmachine/base.rb', line 60 def send(data, args = {}) type = args[:type] || :text return if @state == :closed || (@state == :closing && type != :close) unless type == :plain frame = outgoing_frame.new(:version => @handshake.version, :data => data, :type => type.to_s, :code => args[:code]) 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
121 122 123 124 125 126 127 |
# File 'lib/websocket/eventmachine/base.rb', line 121 def unbind unless @state == :closed @state = :closed close trigger_onclose(1002, '') unless @state == :connecting end end |