Class: SimplePubSub::Client
- Inherits:
-
Object
- Object
- SimplePubSub::Client
- Defined in:
- lib/simplepubsub.rb
Defined Under Namespace
Classes: PubSub
Class Method Summary collapse
Class Method Details
.connect(hostname, port = '59000') {|pubsub| ... } ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/simplepubsub.rb', line 95 def self.connect(hostname, port='59000') pubsub = PubSub.new yield(pubsub) blk = lambda do |ws, em_already_running| ws.onopen do puts "Connected" end ws. do |msg, type| a = msg.split(/\s*:\s*/,2) topic, = a r = pubsub.proc.call topic, (ws.close; EM.stop) if r == :stop and em_already_running == false end ws.onclose do puts "Disconnected" end EventMachine.next_tick do ws.send pubsub.topic + ': ' + pubsub. end end address = hostname + ':' + port params = {uri: 'ws://' + address} c = WebSocket::EventMachine::Client begin # attempt to run a websocket assuming the EventMachine is # already running ws = c.connect(params) blk.call ws, em_already_running = true rescue EM.run do ws = c.connect(params) blk.call(ws, em_already_running = false) end end end |