Class: WebSocket::Frame::Incoming

Inherits:
Base
  • Object
show all
Defined in:
lib/websocket/frame/incoming.rb,
lib/websocket/frame/incoming/client.rb,
lib/websocket/frame/incoming/server.rb

Overview

Note:

You should NEVER use this class directly - use Client or Server subclasses instead, as they contain additional frame options(i.e. Client-side masking in draft 04)

Construct or parse incoming WebSocket Frame.

Examples:

frame = WebSocket::Frame::Incoming::Server.new(version: @handshake.version)
frame << "\x81\x05\x48\x65\x6c\x6c\x6f\x81\x06\x77\x6f\x72\x6c\x64\x21"
frame.next # "Hello"
frame.next # "world!""

Direct Known Subclasses

Client, Server

Defined Under Namespace

Classes: Client, Server

Instance Attribute Summary

Attributes inherited from Base

#code, #data, #error, #type, #version

Attributes included from ExceptionHandler

#error

Instance Method Summary collapse

Methods inherited from Base

#error?, #support_type?, #supported_frames

Methods included from NiceInspect

#inspect

Methods included from ExceptionHandler

included

Constructor Details

#initialize(args = {}) ⇒ Incoming

Returns a new instance of Incoming.



17
18
19
20
# File 'lib/websocket/frame/incoming.rb', line 17

def initialize(args = {})
  @decoded = args[:decoded] || false
  super
end

Instance Method Details

#<<(data) ⇒ Object

Add provided string as raw incoming frame.

Parameters:

  • data (String)

    Raw frame



32
33
34
# File 'lib/websocket/frame/incoming.rb', line 32

def <<(data)
  @data << data
end

#decoded?Boolean

If data is still encoded after receiving then this is false. After calling “next” you will receive another instance of incoming frame, but with data decoded - this function will return true and to_s will return frame content instead of raw data.

Returns:

  • (Boolean)

    If frame already decoded?



26
27
28
# File 'lib/websocket/frame/incoming.rb', line 26

def decoded?
  @decoded
end

#nextWebSocket::Frame::Incoming

Return next complete frame. This function will merge together splitted frames and return as combined content. Check #error if nil received to check for eventual parsing errors

Returns:



40
41
42
# File 'lib/websocket/frame/incoming.rb', line 40

def next
  @handler.decode_frame unless decoded?
end

#to_sString

If decoded then this will return frame content. Otherwise it will return raw frame.

Returns:

  • (String)

    Data of frame



47
48
49
# File 'lib/websocket/frame/incoming.rb', line 47

def to_s
  @data
end