Class: SPSChat
- Inherits:
-
Object
- Object
- SPSChat
- Defined in:
- lib/sps_chat.rb
Instance Method Summary collapse
-
#initialize(host: 'localhost', port: '8080', userid: 'user' + (0..1000).to_a.sample.to_s, room: '') ⇒ SPSChat
constructor
A new instance of SPSChat.
- #onincoming(sender, msg, typing = false) ⇒ Object
-
#ontopic(topic, msg) ⇒ Object
used by the callback routine.
- #send(msg) ⇒ Object
- #typing(c) ⇒ Object
Constructor Details
#initialize(host: 'localhost', port: '8080', userid: 'user' + (0..1000).to_a.sample.to_s, room: '') ⇒ SPSChat
Returns a new instance of SPSChat.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sps_chat.rb', line 11 def initialize(host: 'localhost', port: '8080', \ userid: 'user' + (0..1000).to_a.sample.to_s, room: '') @userid = userid sps = SPSSub.new host: host, port: port, callback: self puts 'connecting ...' sleep 1 # give it a second to connect topic = ['chat'] topic << room if room.length > 0 Thread.new { sps.subscribe topic: (topic + ['#']).join('/') } @pub = SPSPub.new address: host, port: port topic << userid @topic = topic.join('/') end |
Instance Method Details
#onincoming(sender, msg, typing = false) ⇒ Object
62 63 64 65 66 |
# File 'lib/sps_chat.rb', line 62 def onincoming(sender, msg, typing=false) puts "%s: %s" % [sender, msg] end |
#ontopic(topic, msg) ⇒ Object
used by the callback routine
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sps_chat.rb', line 48 def ontopic(topic, msg) typing = false a = topic.split('/') sender = a.pop (sender = a.pop; typing = true) if sender == 'typing' return if sender == @userid typing ? onincoming(sender, msg, true) : onincoming(sender, msg) end |
#send(msg) ⇒ Object
32 33 34 35 36 |
# File 'lib/sps_chat.rb', line 32 def send(msg) @pub.notice ("%s: %s" % [@topic, msg]) end |
#typing(c) ⇒ Object
38 39 40 41 42 |
# File 'lib/sps_chat.rb', line 38 def typing(c) @pub.notice ("%s/typing: %s" % [@topic, c]) end |