Class: Wampus::Servers::Wamp

Inherits:
Object
  • Object
show all
Includes:
AfterInit, Protocols::Wamp
Defined in:
lib/wampus/servers/wamp.rb

Direct Known Subclasses

WampCra

Constant Summary

Constants included from Protocols::Wamp

Protocols::Wamp::CALL_ERROR_MSG_ID, Protocols::Wamp::CALL_MSG_ID, Protocols::Wamp::CALL_RESULT_MSG_ID, Protocols::Wamp::EVENT_MSG_ID, Protocols::Wamp::PREFIX_MSG_ID, Protocols::Wamp::PROTOCOL_VERSION, Protocols::Wamp::PUBLISH_MSG_ID, Protocols::Wamp::SUBSCRIBE_MSG_ID, Protocols::Wamp::UNSUBSCRIBE_MSG_ID, Protocols::Wamp::URI_BASE, Protocols::Wamp::URI_ERROR, Protocols::Wamp::URI_RPC, Protocols::Wamp::URI_TOPIC, Protocols::Wamp::WELCOME_MSG_ID

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Protocols::Wamp

#call_error_msg, #call_msg, #call_result_msg, #event_msg, #prefix_msg, #publish_msg, #subscribe_msg, #unsubscribe_msg, #welcome_msg

Methods included from AfterInit

#exec_after_init

Constructor Details

#initialize(*args) ⇒ Wamp

Returns a new instance of Wamp.



9
10
11
12
13
# File 'lib/wampus/servers/wamp.rb', line 9

def initialize(*args)
  @connections = {}

  super(*args) if defined? super
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



7
8
9
# File 'lib/wampus/servers/wamp.rb', line 7

def connections
  @connections
end

Instance Method Details

#handle_call_msg(connection, data) ⇒ Object



79
80
81
# File 'lib/wampus/servers/wamp.rb', line 79

def handle_call_msg(connection, data)
  super if defined? super
end

#handle_prefix_msg(connection, data) ⇒ Object



73
74
75
76
77
# File 'lib/wampus/servers/wamp.rb', line 73

def handle_prefix_msg(connection, data)
  prefix, uri = data

  connection.add_prefix prefix, uri
end

#handle_publish_msg(connection, data) ⇒ Object



91
92
93
# File 'lib/wampus/servers/wamp.rb', line 91

def handle_publish_msg(connection, data)
  super if defined? super
end

#handle_subscribe_msg(connection, data) ⇒ Object



83
84
85
# File 'lib/wampus/servers/wamp.rb', line 83

def handle_subscribe_msg(connection, data)
  super if defined? super
end

#handle_unsubscribe_msg(connection, data) ⇒ Object



87
88
89
# File 'lib/wampus/servers/wamp.rb', line 87

def handle_unsubscribe_msg(connection, data)
  super if defined? super
end

#on_connection_close(websocket, event) ⇒ Object



63
64
65
66
# File 'lib/wampus/servers/wamp.rb', line 63

def on_connection_close(websocket, event)
  connection = delete_connection websocket
  connection.close_connection
end

#on_connection_error(websocket, error) ⇒ Object

TODO Handle Errors



69
70
71
# File 'lib/wampus/servers/wamp.rb', line 69

def on_connection_error(websocket, error)
  p [:error!, error]
end

#on_connection_message(websocket, message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wampus/servers/wamp.rb', line 37

def on_connection_message(websocket, message)
  connection = find_connections(:websocket => websocket).first

  # Consume any Non-WAMP WS messages
  begin
    data = JSON.parse(message)
    msg_type = data.shift
  rescue => error
    # TODO Handle Error
    return
  end

  case msg_type
    when PREFIX_MSG_ID
      handle_prefix_msg connection, data
    when CALL_MSG_ID
      handle_call_msg connection, data
    when SUBSCRIBE_MSG_ID
      handle_subscribe_msg connection, data
    when UNSUBSCRIBE_MSG_ID
      handle_unsubscribe_msg connection, data
    when PUBLISH_MSG_ID
      handle_publish_msg connection, data
  end
end

#on_connection_open(websocket, handshake) ⇒ Object



31
32
33
34
35
# File 'lib/wampus/servers/wamp.rb', line 31

def on_connection_open(websocket, handshake)
  connection = new_connection(websocket)
  connection.write welcome_msg connection.id
  on_session_open connection
end

#on_session_open(connection) ⇒ Object



27
28
29
# File 'lib/wampus/servers/wamp.rb', line 27

def on_session_open(connection)
  # Nothing
end

#run(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wampus/servers/wamp.rb', line 15

def run(options = {})
  # Maybe we'll get subprotocol support in EM::Websocket one day
  options[:protocol] ||= ['wamp']
  options['sec-websocket-protocol'] = 'wamp'
  EM::WebSocket.run(options) do |ws|
    ws.onopen { |handshake| on_connection_open ws, handshake }
    ws.onmessage { |event| on_connection_message ws, event }
    ws.onerror { |error| on_connection_error ws, error }
    ws.onclose { |event| on_connection_close ws, event }
  end
end

#server_identObject



95
96
97
# File 'lib/wampus/servers/wamp.rb', line 95

def server_ident
  Wampus::identity
end