Class: Narou::PushServer
- Inherits:
-
Object
- Object
- Narou::PushServer
- Includes:
- Eventable, Singleton
- Defined in:
- lib/web/pushserver.rb
Constant Summary collapse
- HISTORY_SAVED_COUNT =
保存する履歴の数
30
Constants included from Eventable
Instance Attribute Summary collapse
-
#accepted_domains ⇒ Object
Returns the value of attribute accepted_domains.
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #clear_history ⇒ Object
-
#initialize ⇒ PushServer
constructor
A new instance of PushServer.
-
#quit ⇒ Object
PushServer を停止させる.
- #run ⇒ Object
-
#send_all(data) ⇒ Object
接続している全てのクライアントに対してメッセージを送信.
Methods included from Eventable
#add_event_listener, included, #one, #remove_event_listener, #trigger_event
Constructor Details
#initialize ⇒ PushServer
Returns a new instance of PushServer.
25 26 27 28 29 30 |
# File 'lib/web/pushserver.rb', line 25 def initialize @accepted_domains = ["*"] @port = 31000 @connections = [] clear_history end |
Instance Attribute Details
#accepted_domains ⇒ Object
Returns the value of attribute accepted_domains.
19 20 21 |
# File 'lib/web/pushserver.rb', line 19 def accepted_domains @accepted_domains end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
19 20 21 |
# File 'lib/web/pushserver.rb', line 19 def connections @connections end |
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/web/pushserver.rb', line 18 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
18 19 20 |
# File 'lib/web/pushserver.rb', line 18 def port @port end |
Instance Method Details
#clear_history ⇒ Object
81 82 83 84 85 86 |
# File 'lib/web/pushserver.rb', line 81 def clear_history @history = [nil] * HISTORY_SAVED_COUNT # Sinatra で get "/" { clear_history } とかやった場合に [nil,nil...] な配列データが # 渡されないようにするため(配列は Sinatra にとって特別なデータ) true end |
#quit ⇒ Object
PushServer を停止させる
77 78 79 |
# File 'lib/web/pushserver.rb', line 77 def quit @server.quit end |
#run ⇒ Object
32 33 34 35 36 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 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/web/pushserver.rb', line 32 def run @server = WebSocketServer.new({ accepted_domains: @accepted_domains, port: @port, host: @host }) Thread.new do @server.run do |ws| begin ws.handshake que = Queue.new @connections.push(que) @history.compact.each do || ws.send(JSON.generate(echo: )) end thread = Thread.new do while true data = que.pop ws.send(data) end end while data = ws.receive begin JSON.parse(data).each do |name, value| trigger(name, value, ws) end rescue JSON::ParserError => e ws.send(JSON.generate(echo: e.)) end end rescue Errno::ECONNRESET => e ensure @connections.delete(que) thread.terminate if thread end end end end |
#send_all(data) ⇒ Object
接続している全てのクライアントに対してメッセージを送信
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/web/pushserver.rb', line 91 def send_all(data) if data.kind_of?(Symbol) # send_all(:"events.name") としてイベント名だけで送りたい場合の対応 data = { data => true } end json = JSON.generate(data) @connections.each do |queue_of_connection| queue_of_connection.push(json) end # echo 以外のイベントは履歴に保存しない = data[:echo] if @history.push() @history.shift end rescue JSON::GeneratorError => e STDERR.puts $@.shift + ": #{e.} (#{e.class})" end |