Class: Wocket::WebSocket

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • socket (TCPSocket)

    TCPSocket of the new connection

  • server (Wocket::Server)

    The instance of the Wocket server that that the websocket belongs to



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

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/wocket/websocket.rb', line 11

def state
  @state
end

#versionObject (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.

Parameters:

  • code (Integer) (defaults to: :normal_closure)

    Disconnect status code. See tools.ietf.org/html/rfc6455#section-7.4.1

  • reason (String) (defaults to: "normal closure")

    An explanation why the connection is being closed.



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.

Returns:

  • (Boolean)


43
44
45
# File 'lib/wocket/websocket.rb', line 43

def connected?
  @state == :connected
end

#pingObject

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.

Parameters:

  • data (String) (defaults to: '')

    String of data to be pushed to the client.

  • type (Symbol) (defaults to: :text)

    The type of data to send to the client. Must be :text or :binary



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