Class: Midori::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/em-midori/websocket.rb

Overview

This class provides methods for WebSocket connection instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ WebSocket

Returns a new instance of WebSocket.

Parameters:

  • connection (EM::Connection)

    raw EventMachine connection


11
12
13
14
# File 'lib/em-midori/websocket.rb', line 11

def initialize(connection)
  @events = {}
  @connection = connection
end

Instance Attribute Details

#connectionEM::Connection

raw EventMachine connection

Returns:

  • (EM::Connection)

    the current value of connection


7
8
9
# File 'lib/em-midori/websocket.rb', line 7

def connection
  @connection
end

#eventsHash

response for different event

Returns:

  • (Hash)

    the current value of events


7
8
9
# File 'lib/em-midori/websocket.rb', line 7

def events
  @events
end

#msgArray<Fixnum>, String

message send from client

Returns:

  • (Array<Fixnum>, String)

    the current value of msg


7
8
9
# File 'lib/em-midori/websocket.rb', line 7

def msg
  @msg
end

#opcodeFixnum

operation code of WebSocket

Returns:

  • (Fixnum)

    the current value of opcode


7
8
9
# File 'lib/em-midori/websocket.rb', line 7

def opcode
  @opcode
end

Instance Method Details

#closeObject

Close a websocket connection


100
101
102
# File 'lib/em-midori/websocket.rb', line 100

def close
  raise Midori::Error::FrameEnd
end

#decode(data) ⇒ Object

Decode raw data send from client

Parameters:

Raises:


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/em-midori/websocket.rb', line 18

def decode(data)
  # Fin and Opcode
  byte_tmp = data.getbyte
  fin = byte_tmp & 0b10000000
  @opcode = byte_tmp & 0b00001111
  # NOT Support Multiple Fragments
  raise Midori::Error::ContinuousFrame unless fin
  raise Midori::Error::OpCodeError unless [0x1, 0x2, 0x8, 0x9, 0xA].include?opcode
  close if @opcode == 0x8 # Close Frame
  # return if @opcode == 0x9 || @opcode == 0xA # Ping Pong
  decode_mask(data)
end

#decode_mask(data) ⇒ Object

Decode masked message send from client

Parameters:

Raises:


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/em-midori/websocket.rb', line 33

def decode_mask(data)
  # Mask
  byte_tmp = data.getbyte
  is_masked = byte_tmp & 0b10000000
  raise Midori::Error::NotMasked unless is_masked == 128
  # Payload
  payload = byte_tmp & 0b01111111
  mask = Array.new(4) { data.getbyte }
  # Message
  masked_msg = Array.new(payload) { data.getbyte }
  @msg = masked_msg.each_with_index.map { |byte, i| byte ^ mask[i % 4] }
  @msg = @msg.pack('C*').force_encoding('utf-8') if [0x1, 0x9, 0xA].include?opcode
  # For debug
  #  data.rewind
  #  data.bytes {|byte| puts byte.to_s(16)}
end

#heartbeat(method, str) ⇒ Object

Ancestor of ping pong

Parameters:

  • method (Fixnum)

    opcode

  • str (String)

    string to send

Raises:


94
95
96
97
# File 'lib/em-midori/websocket.rb', line 94

def heartbeat(method, str)
    raise Midori::Error::PingPongSizeTooLarge if str.size > 125
    @connection.send_data [method, str.size, str].pack("CCA#{str.size}")
end

#on(event) { ... } ⇒ Object

API definition for events

Examples:

websocket '/websocket' do |ws|
  ws.on :message do |msg|
    puts msg
  end
end

Parameters:

  • event (Symbol)

    event name(open, message, close, ping, pong)

Yields:

  • what to do after event matched


59
60
61
# File 'lib/em-midori/websocket.rb', line 59

def on(event, &block) # open, message, close, ping, pong
  @events[event] = block
end

#ping(str) ⇒ Object

Send a Ping request

Parameters:

  • str (String)

    string to send


81
82
83
# File 'lib/em-midori/websocket.rb', line 81

def ping(str)
  heartbeat(0b10001001, str)
end

#pong(str) ⇒ Object

Send a Pong request

Parameters:

  • str (String)

    string to send


87
88
89
# File 'lib/em-midori/websocket.rb', line 87

def pong(str)
  heartbeat(0b10001010, str)
end

#send(msg) ⇒ Object

Send data

Parameters:

  • msg (Array<Fixnum>, String)

    data to send


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/em-midori/websocket.rb', line 65

def send(msg)
  output = []
  if msg.is_a?String
    output << 0b10000001 << msg.size << msg
    @connection.send_data(output.pack("CCA#{msg.size}"))
  elsif msg.is_a?Array
    output << 0b10000010 << msg.size
    output.concat msg
    @connection.send_data(output.pack("C*"))
  else
    raise Midori::Error::OpCodeError
  end
end