Class: LibWebSocket::OpeningHandshake::Client

Inherits:
LibWebSocket::OpeningHandshake show all
Defined in:
lib/libwebsocket/opening_handshake/client.rb

Overview

Construct or parse a client WebSocket handshake. This module is written for convenience, since using request and response directly requires the same code again and again.

SYNOPSIS

h = LibWebSocket::OpeningHandshake::Client.new(:url => 'ws://example.com')

# Create request
h.to_s # GET /demo HTTP/1.1
       # Upgrade: WebSocket
       # Connection: Upgrade
       # Host: example.com
       # Origin: http://example.com
       # Sec-WebSocket-Key1: 18x 6]8vM;54 *(5:  {   U1]8  z [  8
       # Sec-WebSocket-Key2: 1_ tx7X d  <  nw  334J702) 7]o}` 0
       #
       # Tm[K T2u

# Parse server response
h.parse \<<EOF
HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://example.com
Sec-WebSocket-Location: ws://example.com/demo

fQJ,fN/4F4!~K~MH
EOF

h.error # Check if there were any errors
h.done? # Returns true

Instance Method Summary collapse

Methods inherited from LibWebSocket::OpeningHandshake

#controller, #parse

Constructor Details

#initialize(hash = {}) ⇒ Client

Returns a new instance of Client.



39
40
41
# File 'lib/libwebsocket/opening_handshake/client.rb', line 39

def initialize(hash = {})
  @controller = ::WebSocket::Handshake::Client.new({:version => 76}.merge(hash))
end

Instance Method Details

#urlObject



43
44
45
46
47
# File 'lib/libwebsocket/opening_handshake/client.rb', line 43

def url
  lws_url = LibWebSocket::URL.new
  lws_url.parse(uri)
  lws_url
end