Sha256: 79bfb1633101f6eb70e54cd4b2eb2c220377b08a45e48aa1c686aa7fbd7daadc
Contents?: true
Size: 1.64 KB
Versions: 2
Compression:
Stored size: 1.64 KB
Contents
require 'em-websocket' require 'json' module Pokeplot class Socket include Database def initialize(host = "0.0.0.0", port = 9090, log = false) @clients = [] Thread.new do EM.run do db = Database.mongo EM::WebSocket.run(:host => host, :port => port) do |ws| ws.onopen do |handshake| puts "[+] WebSocket connection open" if @log pokemon = {:type => "count", :data => {}} spawns = {:type => "spawn_points", :data => {}} db[:encounters].find().each do |bson| #get initial pokemon if pokemon[:data].has_key?(bson[:pokemon]) pokemon[:data][bson[:pokemon]] += 1 else pokemon[:data][bson[:pokemon]] = 1 end #get spawn points if spawns[:data].has_key?(bson[:spawn_point_id]) spawns[:data][bson[:spawn_point_id]] += 1 else spawns[:data][bson[:spawn_point_id]] = 1 end end ws.send(pokemon.to_json) ws.send(spawns.to_json) @clients << ws end ws.onclose { @clients.delete(ws) } end end end end #Mongo def started(event) if event.command.has_key?('insert') if event.command.fetch('insert') == 'encounters' @clients.each do |c| c.send({:type => "pokemon", :data => event.command.fetch('documents')[0]}.to_json) end end end end def failed(_); end def succeeded(_); end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
pokeplot-0.2.1beta | lib/pokeplot/socket.rb |
pokeplot-0.2.0beta | lib/pokeplot/socket.rb |