Class: Narou::PushServer

Inherits:
Object
  • Object
show all
Includes:
Eventable, Singleton
Defined in:
lib/web/pushserver.rb

Constant Summary collapse

HISTORY_SAVED_COUNT =

保存する履歴の数

30

Constants included from Eventable

Eventable::EVENTS_CONTAINER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Eventable

#add_event_listener, included, #one, #remove_event_listener, #trigger_event

Constructor Details

#initializePushServer

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_domainsObject

Returns the value of attribute accepted_domains.



19
20
21
# File 'lib/web/pushserver.rb', line 19

def accepted_domains
  @accepted_domains
end

#connectionsObject (readonly)

Returns the value of attribute connections.



19
20
21
# File 'lib/web/pushserver.rb', line 19

def connections
  @connections
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/web/pushserver.rb', line 18

def host
  @host
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/web/pushserver.rb', line 18

def port
  @port
end

Instance Method Details

#clear_historyObject



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

#quitObject

PushServer を停止させる



77
78
79
# File 'lib/web/pushserver.rb', line 77

def quit
  @server.quit
end

#runObject



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 |message|
          ws.send(JSON.generate(echo: message))
        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.message))
          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
# 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 以外のイベントは履歴に保存しない
  message = data[:echo]
  if message
    stack_to_history(message)
  end
rescue JSON::GeneratorError => e
  STDERR.puts $@.shift + ": #{e.message} (#{e.class})"
end

#stack_to_history(message) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/web/pushserver.rb', line 109

def stack_to_history(message)
  if message == "." && @history[-1] =~ /\A\.+\z/
    # 進行中を表す .... の出力でヒストリーが消費されるのを防ぐため、
    # 連続した . は一つにまとめる
    @history[-1] << "."
  else
    @history.push(message)
    @history.shift
  end
end