Class: Wocket::WebSocket
- Inherits:
-
Object
- Object
- Wocket::WebSocket
- Includes:
- Celluloid
- Defined in:
- lib/wocket/websocket.rb
Defined Under Namespace
Classes: HandshakeError
Constant Summary collapse
- BUFFER_SIZE =
4096
- STATUS_CODES =
{ normal_closure: 1000, going_away: 1001, protocol_error: 1002, invalid_data: 1003, inconsistent_data: 1007, policy_violation: 1008, too_large: 1009, extension_error: 1010, unexpected_condition: 1011 }
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#close(code = :normal_closure, reason = "normal closure") ⇒ Object
Closes the open websocket with a code and a reason defaulting to a normal closure, once connection is closed the :onclose callback is triggered.
-
#connected? ⇒ Boolean
Returns true if the client is connected, false if they are not.
-
#initialize(socket, server) ⇒ WebSocket
constructor
Initializes a new Wocket WebSocket, verifies the client handshake, if the handhake is valid the server returns a handshake completing the handshake process.
-
#ping ⇒ Object
Send a ping message to the client.
-
#push(data = '', type = :text) ⇒ Object
(also: #write)
Writes data frame to the websocket.
Constructor Details
#initialize(socket, server) ⇒ WebSocket
Initializes a new Wocket WebSocket, verifies the client handshake, if
the handhake is valid the server returns a handshake completing the
handshake process.
33 34 35 36 37 38 39 40 |
# File 'lib/wocket/websocket.rb', line 33 def initialize(socket, server) @socket = socket @server = server @state = :connecting @stop_read = false validate_handshake end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/wocket/websocket.rb', line 11 def state @state end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
11 12 13 |
# File 'lib/wocket/websocket.rb', line 11 def version @version end |
Instance Method Details
#close(code = :normal_closure, reason = "normal closure") ⇒ Object
Closes the open websocket with a code and a reason defaulting to a normal
closure, once connection is closed the :onclose callback is triggered.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wocket/websocket.rb', line 63 def close(code = :normal_closure, reason = "normal closure") code = STATUS_CODES[code] @stop_read = true unless @socket.closed? @socket.write outgoing_frame.new(version: @version, data: reason, type: :close, code: code).to_s @socket.close end @state = :disconnected @server.trigger(:onclose, self, code, reason) end |
#connected? ⇒ Boolean
Returns true if the client is connected, false if they are not.
43 44 45 |
# File 'lib/wocket/websocket.rb', line 43 def connected? @state == :connected end |
#ping ⇒ Object
Send a ping message to the client
81 82 83 |
# File 'lib/wocket/websocket.rb', line 81 def ping end |
#push(data = '', type = :text) ⇒ Object Also known as: write
Writes data frame to the websocket.
51 52 53 54 |
# File 'lib/wocket/websocket.rb', line 51 def push(data = '', type = :text) frame = outgoing_frame.new(version: @version, data: data, type: type) @socket.write frame.to_s end |