Class: WebSocket::Handshake::Server

Inherits:
Base
  • Object
show all
Defined in:
lib/websocket/handshake/server.rb

Overview

Construct or parse a server WebSocket handshake.

Examples:

handshake = WebSocket::Handshake::Server.new

# Parse client request
@handshake << <<EOF
GET /demo HTTP/1.1\r
Upgrade: websocket\r
Connection: Upgrade\r
Host: example.com\r
Sec-WebSocket-Origin: http://example.com\r
Sec-WebSocket-Version: 13\r
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r
\r
EOF

# All data received?
@handshake.finished?

# No parsing errors?
@handshake.valid?

# Create response
@handshake.to_s # HTTP/1.1 101 Switching Protocols
                # Upgrade: websocket
                # Connection: Upgrade
                # Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Instance Attribute Summary

Attributes inherited from Base

#error, #path, #query, #secure, #state, #version

Instance Method Summary collapse

Methods inherited from Base

#finished?, #inspect, #leftovers, #to_s, #uri, #valid?

Constructor Details

#initialize(args = {}) ⇒ Server

Initialize new WebSocket Server

Examples:

Websocket::Handshake::Server.new(:secure => true)

Parameters:

  • args (Hash) (defaults to: {})

    Arguments for server

Options Hash (args):

  • :secure (Boolean)

    If true then server will use wss:// protocol



42
43
44
45
# File 'lib/websocket/handshake/server.rb', line 42

def initialize(args = {})
  super
  @secure = !!args[:secure]
end

Instance Method Details

#<<(data) ⇒ Object

Add text of request from Client. This method will parse content immediately and update version, state and error(if neccessary)

Examples:

@handshake << <<EOF
GET /demo HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: example.com
Sec-WebSocket-Origin: http://example.com
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

EOF

Parameters:

  • data (String)

    Data to add



62
63
64
65
66
67
# File 'lib/websocket/handshake/server.rb', line 62

def <<(data)
  @data << data
  if parse_data
    set_version
  end
end

#hostString

Host of server according to client header

Returns:

  • (String)

    host



77
78
79
# File 'lib/websocket/handshake/server.rb', line 77

def host
  @headers["host"].to_s.split(":")[0].to_s
end

#portString

Port of server according to client header

Returns:

  • (String)

    port



83
84
85
# File 'lib/websocket/handshake/server.rb', line 83

def port
  @headers["host"].to_s.split(":")[1]
end

#should_respond?Boolean

Should send content to client after finished parsing?

Returns:

  • (Boolean)

    true



71
72
73
# File 'lib/websocket/handshake/server.rb', line 71

def should_respond?
  true
end