Class: OpenPushed::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/openpushed.rb

Instance Method Summary collapse

Constructor Details

#initializeServer

Initialize mongomapper Set the OpenPushedKeys class as an instance variable



36
37
38
39
40
41
# File 'lib/openpushed.rb', line 36

def initialize
	MongoMapper.connection = Mongo::Connection.new('localhost')
	MongoMapper.database = 'openpushed'
	
	@keys = OpenPushed::OpenPushedKeys
end

Instance Method Details

#run!Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object



99
100
101
# File 'lib/openpushed.rb', line 99

def verbose(isit)
	$logger = isit
end