require 'em-websocket' require 'yajl' require 'ganymed' require 'ganymed/websocket/connection' module Ganymed class Websocket attr_accessor :config, :db def initialize(config, db) @config, @db = config, db @connections = [] run end def run log.info("accepting WebSocket connections on tcp##{config.host}:#{config.port}") EM.start_server(config.host, config.port, Connection, {}) do |connection| connection.config = config connection.db = db connection.onopen do log.info("new connection from #{connection.peer}") @connections << connection end connection.onclose do log.info("#{connection.peer} has closed the connection") @connections.delete(connection) end end end def each(&block) @connections.each(&block) end def send(type, data) each do |connection| connection.send(type, data) end end end end