Class: WAMP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wamp/client.rb,
lib/wamp/client/version.rb

Constant Summary collapse

TYPEID_WELCOME =
0
TYPEID_PREFIX =
1
TYPEID_CALL =
2
TYPEID_CALLRESULT =
3
TYPEID_CALLERROR =
4
TYPEID_SUBSCRIBE =
5
TYPEID_UNSUBSCRIBE =
6
TYPEID_PUBLISH =
7
TYPEID_EVENT =
8
VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
# File 'lib/wamp/client.rb', line 21

def initialize(uri, options={})
  options = {:subprotocols => ["wamp"]}.merge(options)
  @session_id = nil
  @ws = Net::WS.new(uri, options)
  @subscriptions = {}
end

Instance Method Details

#check(timeout = 5) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/wamp/client.rb', line 64

def check(timeout=5)
  Timeout.timeout(timeout) do
    loop {handle_message}
  end
rescue Timeout::Error
  # noop
end

#closeObject



33
34
35
# File 'lib/wamp/client.rb', line 33

def close
  @ws.close
end

#handle_messageObject



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

def handle_message
  payload = receive_message

  case payload[0]
  when TYPEID_EVENT
    handle_event(payload)
  else
    $stderr.puts "Unhandled message type id: #{payload[0].pretty_inspect}"
  end
end

#open(request_uri = "/") ⇒ Object



28
29
30
31
# File 'lib/wamp/client.rb', line 28

def open(request_uri="/")
  @ws.open(request_uri)
  receive_welcome_message
end

#prefix(prefix, uri) ⇒ Object

FIXME: this doesn’t seem to be working yet… I’ve no idea why



38
39
40
41
# File 'lib/wamp/client.rb', line 38

def prefix(prefix, uri)
  payload = [TYPEID_PREFIX, prefix, uri]
  @ws.send_text(JSON.dump(payload))
end

#publish(uri_or_curie, data) ⇒ Object



72
73
74
75
# File 'lib/wamp/client.rb', line 72

def publish(uri_or_curie, data)
  payload = [TYPEID_PUBLISH, uri_or_curie, data]
  @ws.send_text(JSON.dump(payload).force_encoding("utf-8"))
end

#subscribe(uri_or_curie, cb) ⇒ Object



43
44
45
46
47
# File 'lib/wamp/client.rb', line 43

def subscribe(uri_or_curie, cb)
  payload = [TYPEID_SUBSCRIBE, uri_or_curie]
  @ws.send_text(JSON.dump(payload))
  @subscriptions[uri_or_curie] = @subscriptions.fetch(uri_or_curie, []) << cb
end

#to_ioObject



49
50
51
# File 'lib/wamp/client.rb', line 49

def to_io
  @ws.to_io
end