Class: Narou::PushServer

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

Constant Summary collapse

HISTORY_SAVED_COUNT =

保存する履歴の数

60

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.



26
27
28
29
30
31
# File 'lib/web/pushserver.rb', line 26

def initialize
  @accepted_domains = ["*"]
  @port = 31000
  @connections = []
  clear_history
end

Instance Attribute Details

#accepted_domainsObject

Returns the value of attribute accepted_domains.



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

def accepted_domains
  @accepted_domains
end

#connectionsObject (readonly)

Returns the value of attribute connections.



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

def connections
  @connections
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#clear_historyObject



85
86
87
88
89
90
# File 'lib/web/pushserver.rb', line 85

def clear_history
  @history = [nil] * HISTORY_SAVED_COUNT
  # Sinatra で get "/" { clear_history } とかやった場合に [nil,nil...] な配列データが
  # 渡されないようにするため(配列は Sinatra にとって特別なデータ)
  true
end

#quitObject

PushServer を停止させる



81
82
83
# File 'lib/web/pushserver.rb', line 81

def quit
  @server.quit
end

#runObject



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
73
74
75
76
# File 'lib/web/pushserver.rb', line 33

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: {
              target_console: "#console",
              body: e.message
            }))
          end
        end
      rescue Errno::ECONNRESET => e
      ensure
        @connections.delete(que)
        thread.terminate if thread
      end
    end
  end
end

#send_all(data) ⇒ Object

接続している全てのクライアントに対してメッセージを送信



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/web/pushserver.rb', line 95

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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/web/pushserver.rb', line 113

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