Class: WAMP::Client

Inherits:
Object
  • Object
show all
Includes:
Bindable
Defined in:
lib/wamp/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bindable

#bind, #trigger

Constructor Details

#initialize(options = {}) ⇒ Client

WAMP Client

Connects to WAMP server, after connection client should receve WELCOME message
from server.
Client can then register prefix, call, subscribe, unsubscribe, and publish


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wamp/client.rb', line 17

def initialize(options = {})
  @ws_server = options[:host] || "ws://localhost:9000"
  @protocols = options[:protocols]
  @headers = options[:headers]
  @id     = nil
  @socket = nil
  @wamp_protocol = nil
  @server_ident  = nil

  @prefixes  = {}
  @topics    = []
  @callbacks = {}
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



9
10
11
# File 'lib/wamp/client.rb', line 9

def callbacks
  @callbacks
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/wamp/client.rb', line 9

def id
  @id
end

#prefixesObject

Returns the value of attribute prefixes.



9
10
11
# File 'lib/wamp/client.rb', line 9

def prefixes
  @prefixes
end

#server_identObject

Returns the value of attribute server_ident.



9
10
11
# File 'lib/wamp/client.rb', line 9

def server_ident
  @server_ident
end

#socketObject

Returns the value of attribute socket.



9
10
11
# File 'lib/wamp/client.rb', line 9

def socket
  @socket
end

#topicsObject

Returns the value of attribute topics.



9
10
11
# File 'lib/wamp/client.rb', line 9

def topics
  @topics
end

#wamp_protocolObject

Returns the value of attribute wamp_protocol.



9
10
11
# File 'lib/wamp/client.rb', line 9

def wamp_protocol
  @wamp_protocol
end

#ws_serverObject

Returns the value of attribute ws_server.



9
10
11
# File 'lib/wamp/client.rb', line 9

def ws_server
  @ws_server
end

Instance Method Details

#available_bindingsObject



31
32
33
# File 'lib/wamp/client.rb', line 31

def available_bindings
  [:connect, :welcome, :call_result, :call_error, :event, :disconnect]
end

#prefix(prefix, topic_uri) ⇒ Object



45
46
47
48
49
# File 'lib/wamp/client.rb', line 45

def prefix(prefix, topic_uri)
  socket.send [WAMP::MessageType[:PREFIX], prefix, topic_uri].to_json

  prefixes[prefix] = topic_uri
end

#publish(topic_uri, payload, options = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/wamp/client.rb', line 57

def publish(topic_uri, payload, options = {})
  exclude = options.fetch(:exclude, nil)
  include = options.fetch(:include, nil)

  socket.send [WAMP::MessageType[:PUBLISH], topic_uri, payload, exclude, include].to_json
end

#startObject



35
36
37
38
39
40
41
42
43
# File 'lib/wamp/client.rb', line 35

def start
  EM.run do
    ws = Faye::WebSocket::Client.new(ws_server, @protocols, {:headers => @headers})

    ws.onopen    = lambda { |event| handle_open(ws, event) }
    ws.onmessage = lambda { |event| handle_message(event) }
    ws.onclose   = lambda { |event| handle_close(event) }
  end
end

#stopObject



64
65
66
# File 'lib/wamp/client.rb', line 64

def stop
  EM.stop
end

#subscribe(topic_uri) ⇒ Object



51
52
53
54
55
# File 'lib/wamp/client.rb', line 51

def subscribe(topic_uri)
  socket.send [WAMP::MessageType[:SUBSCRIBE], topic_uri].to_json

  topics << topic_uri
end