Class FTW::WebSocket::Rack
In: lib/ftw/websocket/rack.rb
lib/ftw/websocket/rack.rb
Parent: Object

A websocket helper for Rack

An example with Sinatra:

    get "/websocket/echo" do
      ws = FTW::WebSocket::Rack.new(env)
      stream(:keep_open) do |out|
        ws.each do |payload|
          # 'payload' is the text payload of a single websocket message
          # publish it back to the client
          ws.publish(payload)
        end
      end
      ws.rack_response
    end

Methods

each   each   new   new   publish   publish   rack_response   rack_response   valid?   valid?  

Included Modules

FTW::WebSocket::Constants FTW::CRLF FTW::WebSocket::Constants FTW::CRLF

Public Class methods

Create a new websocket rack helper… thing.

@param rack_env the ‘env’ bit given to your Rack application

Create a new websocket rack helper… thing.

@param rack_env the ‘env’ bit given to your Rack application

Public Instance methods

Enumerate each websocket payload (message).

The payload of each message will be yielded to the block.

Example:

    ws.each do |payload|
      puts "Received: #{payload}"
    end

Enumerate each websocket payload (message).

The payload of each message will be yielded to the block.

Example:

    ws.each do |payload|
      puts "Received: #{payload}"
    end

Publish a message over this websocket.

@param message Publish a string message to the websocket.

Publish a message over this websocket.

@param message Publish a string message to the websocket.

Get the response Rack is expecting.

If this was a valid websocket request, it will return a response that completes the HTTP portion of the websocket handshake.

If this was an invalid websocket request, it will return a 400 status code and descriptions of what failed in the body of the response.

@return [number, hash, body]

Get the response Rack is expecting.

If this was a valid websocket request, it will return a response that completes the HTTP portion of the websocket handshake.

If this was an invalid websocket request, it will return a 400 status code and descriptions of what failed in the body of the response.

@return [number, hash, body]

Is this a valid handshake?

Is this a valid handshake?

[Validate]