lib/angelo/base.rb in angelo-0.1.8 vs lib/angelo/base.rb in angelo-0.1.9

- old
+ new

@@ -10,11 +10,11 @@ @@addr = DEFAULT_ADDR @@port = DEFAULT_PORT @@ping_time = DEFAULT_PING_TIME - if ARGV.any? and not Kernel.const_defined?('RSpec') + if ARGV.any? and not Kernel.const_defined?('Minitest') require 'optparse' OptionParser.new { |op| op.on('-p port', 'set the port (default is 4567)') { |val| @@port = Integer(val) } op.on('-o addr', "set the host (default is #{@@addr})") { |val| @@addr = val } }.parse!(ARGV.dup) @@ -85,11 +85,11 @@ def task name, &block Angelo::Server.define_task name, &block end def websockets - @websockets ||= WebsocketsArray.new server + @websockets ||= Stash.new server @websockets.reject! &:closed? @websockets end def content_type type @@ -147,64 +147,13 @@ end end task :ping_websockets do every(@@ping_time) do - websockets.all.each do |ws| + websockets.all_each do |ws| ws.socket << ::WebSocket::Message.ping.to_data end end - end - - class WebsocketsArray < Array - include Celluloid::Logger - - @@peeraddrs = {} - @@socket_context = {} - @@websockets = {} - - def initialize server, context = nil - @context, @server = context, server - super() - end - - def << ws - @@socket_context[ws] = @context if @context - @@peeraddrs[ws] = ws.peeraddr - @server.async.handle_websocket ws - super ws - end - - def each &block - super do |ws| - begin - yield ws - rescue Reel::SocketError - remove_socket ws - end - end - end - - def remove_socket ws - if c = @@socket_context[ws] - warn "removing socket from context ':#{c}' (#{@@peeraddrs[ws][2]})" - self[c].delete ws - else - warn "removing socket (#{@@peeraddrs[ws][2]})" - delete ws - end - @@peeraddrs.delete ws - end - - def [] context - raise ArgumentError.new "symbol required" unless Symbol === context - @@websockets[context] ||= self.class.new @server, context - end - - def all - a = self + @@websockets.values.flatten - end - end end end