class OpenPushed::Server

Public Class Methods

new() click to toggle source

Initialize mongomapper Set the OpenPushedKeys class as an instance variable

# File lib/openpushed.rb, line 36
def initialize
        MongoMapper.connection = Mongo::Connection.new('localhost')
        MongoMapper.database = 'openpushed'
        
        @keys = OpenPushed::OpenPushedKeys
end

Public Instance Methods

run!() click to toggle source
# File lib/openpushed.rb, line 42
def run!
        $SOCKS = {}

        ## Startup Redis Subscriber on channel notifications
        Thread.new do
                $redis = Redis.new

                $redis.subscribe("notifications") do|on|
                        OpenPushed::Logger.log "Subcribed to channel"
                        on.message do |channel,message|
                                data = JSON.parse(message)
                                $SOCKS[data["channel"]].each do |socket|
                                        socket.send(data["msg"])
                                end
                        end
                end
        end

        ## Startup WebSockets Server
        Thread.new do
                EM.run {
                  EM::WebSocket.run(:host => "0.0.0.0", :port => 2803) do |ws|
                    ws.onopen { |handshake|
                                OpenPushed::Logger.log "Opening connection"        
                    }

                    ws.onclose { OpenPushed::Logger.log "Connection closed" }

                    ws.onmessage { |msg|
                     if(msg  == "0\n" || msg == "0")

                     else
                            # Check if channel ID exists if not close we will close the socket immediately
                            key = @keys.where(:channelid => msg).first
                            if(key == nil)
                                    ws.close
                            else
                                    OpenPushed::Logger.log "Client added to channel"
                                   if($SOCKS[msg] == nil) 
                                           $SOCKS[msg] = []
                                   end
                                   $SOCKS[msg] << ws
                            end

                     end    
                    }

                  end
                }
        end

        Thread.new do
        end

        ## Keep the proccess running so the Threads won't quit
        sleep
end
verbose(isit) click to toggle source
# File lib/openpushed.rb, line 99
def verbose(isit)
        $logger = isit
end