Module: DDP::Server::Protocol

Includes:
Data, Heartbeat, RPC
Included in:
WebSocket
Defined in:
lib/ddp/server/protocol.rb,
lib/ddp/server/protocol/rpc.rb,
lib/ddp/server/protocol/data.rb,
lib/ddp/server/protocol/heartbeat.rb

Overview

Implementation of the DDP protocol Can be included into any class that has an on_open, a read_message and a write_message method

Defined Under Namespace

Modules: Data, Heartbeat, RPC

Constant Summary collapse

DDP_VERSION =
'1'

Constants included from RPC

RPC::NO_RESULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RPC

#handle_method, #handle_rpc, #send_error_result, #send_result, #send_updated

Methods included from Data

#handle_data, #handle_sub, #handle_unsub, #send_added, #send_added_before, #send_changed, #send_moved_before, #send_nosub, #send_ready, #send_removed

Methods included from Heartbeat

#handle_heartbeat

Instance Attribute Details

#session_idObject

Returns the value of attribute session_id.



17
18
19
# File 'lib/ddp/server/protocol.rb', line 17

def session_id
  @session_id
end

Instance Method Details

#handle_connectObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/ddp/server/protocol.rb', line 23

def handle_connect
  message = read_message

  if message['msg'] == 'connect' && message['version'] == DDP_VERSION
    handle_session(message)
  else
    write_message('msg' => 'failed', 'version' => DDP_VERSION)
    close
  end
end

#handle_establishedObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ddp/server/protocol.rb', line 42

def handle_established
  loop do
    @message = read_message

    next if handle_heartbeat
    next if handle_data
    next if handle_rpc
    break
  end

  close
end

#handle_session(message) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ddp/server/protocol.rb', line 34

def handle_session(message)
  @session_id = message['session'] || new_session_id

  write_message('msg' => 'connected', 'session' => session_id)

  handle_established
end

#new_session_idObject



19
20
21
# File 'lib/ddp/server/protocol.rb', line 19

def new_session_id
  SecureRandom.hex
end