openpushed 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/openpushed.rb +135 -0
- metadata +47 -0
data/lib/openpushed.rb
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
require 'redis'
|
|
2
|
+
require 'mongo_mapper'
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'em-websocket'
|
|
5
|
+
require 'securerandom'
|
|
6
|
+
require 'sinatra/base'
|
|
7
|
+
|
|
8
|
+
$logger = true
|
|
9
|
+
|
|
10
|
+
module OpenPushed
|
|
11
|
+
class Logger
|
|
12
|
+
def self.log(message)
|
|
13
|
+
if($logger)
|
|
14
|
+
puts message
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.toggle
|
|
19
|
+
if($logger)
|
|
20
|
+
$logger = false
|
|
21
|
+
else
|
|
22
|
+
$logger = true
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
class OpenPushedKeys
|
|
27
|
+
include MongoMapper::Document
|
|
28
|
+
|
|
29
|
+
key :secret, String
|
|
30
|
+
key :channelid, String
|
|
31
|
+
key :device, Boolean
|
|
32
|
+
end
|
|
33
|
+
class Server
|
|
34
|
+
# Initialize mongomapper
|
|
35
|
+
# Set the OpenPushedKeys class as an instance variable
|
|
36
|
+
def initialize
|
|
37
|
+
MongoMapper.connection = Mongo::Connection.new('localhost')
|
|
38
|
+
MongoMapper.database = 'openpushed'
|
|
39
|
+
|
|
40
|
+
@keys = OpenPushed::OpenPushedKeys
|
|
41
|
+
end
|
|
42
|
+
def run!
|
|
43
|
+
$SOCKS = {}
|
|
44
|
+
|
|
45
|
+
## Startup Redis Subscriber on channel notifications
|
|
46
|
+
Thread.new do
|
|
47
|
+
$redis = Redis.new
|
|
48
|
+
|
|
49
|
+
$redis.subscribe("notifications") do|on|
|
|
50
|
+
OpenPushed::Logger.log "Subcribed to channel"
|
|
51
|
+
on.message do |channel,message|
|
|
52
|
+
data = JSON.parse(message)
|
|
53
|
+
$SOCKS[data["channel"]].each do |socket|
|
|
54
|
+
socket.send(data["msg"])
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
## Startup WebSockets Server
|
|
61
|
+
Thread.new do
|
|
62
|
+
EM.run {
|
|
63
|
+
EM::WebSocket.run(:host => "0.0.0.0", :port => 2803) do |ws|
|
|
64
|
+
ws.onopen { |handshake|
|
|
65
|
+
OpenPushed::Logger.log "Opening connection"
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
ws.onclose { OpenPushed::Logger.log "Connection closed" }
|
|
69
|
+
|
|
70
|
+
ws.onmessage { |msg|
|
|
71
|
+
if(msg == "0\n" || msg == "0")
|
|
72
|
+
|
|
73
|
+
else
|
|
74
|
+
# Check if channel ID exists if not close we will close the socket immediately
|
|
75
|
+
key = @keys.where(:channelid => msg).first
|
|
76
|
+
if(key == nil)
|
|
77
|
+
ws.close
|
|
78
|
+
else
|
|
79
|
+
OpenPushed::Logger.log "Client added to channel"
|
|
80
|
+
if($SOCKS[msg] == nil)
|
|
81
|
+
$SOCKS[msg] = []
|
|
82
|
+
end
|
|
83
|
+
$SOCKS[msg] << ws
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
end
|
|
90
|
+
}
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
Thread.new do
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
## Keep the proccess running so the Threads won't quit
|
|
97
|
+
sleep
|
|
98
|
+
end
|
|
99
|
+
def verbose(isit)
|
|
100
|
+
$logger = isit
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
class Migrate
|
|
105
|
+
def self.run
|
|
106
|
+
MongoMapper.connection = Mongo::Connection.new('localhost')
|
|
107
|
+
MongoMapper.database = 'openpushed'
|
|
108
|
+
|
|
109
|
+
key = OpenPushed::OpenPushedKeys.new(:secret => SecureRandom.hex,:channelid => SecureRandom.hex,:device => false);
|
|
110
|
+
key.save!
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
class WebAPI < Sinatra::Base
|
|
114
|
+
# Setup port, mongomapper connections and redis
|
|
115
|
+
configure do
|
|
116
|
+
set :port,2804
|
|
117
|
+
|
|
118
|
+
MongoMapper.connection = Mongo::Connection.new('localhost')
|
|
119
|
+
MongoMapper.database = 'openpushed'
|
|
120
|
+
|
|
121
|
+
$redis = Redis.new
|
|
122
|
+
$keys = OpenPushed::OpenPushedKeys
|
|
123
|
+
end
|
|
124
|
+
# Route to broadcast to all subscribed devices
|
|
125
|
+
post '/api/push/broadcast' do
|
|
126
|
+
key = $keys.where(:secret => params[:secret],:channelid => params[:channelid]).first
|
|
127
|
+
if(key == nil)
|
|
128
|
+
{:error => "invalid keys"}.to_json
|
|
129
|
+
else
|
|
130
|
+
$redis.publish "notifications", {:channel => params[:channelid], :msg => params[:message]}.to_json
|
|
131
|
+
{:status => "ok"}.to_json
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: openpushed
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Bram Vandenbogaerde
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: An fully open source push notifications server
|
|
15
|
+
email: contact@bramvdb.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/openpushed.rb
|
|
21
|
+
homepage: http://opensource.bramvdb.com/openpushed/
|
|
22
|
+
licenses:
|
|
23
|
+
- MIT
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
none: false
|
|
30
|
+
requirements:
|
|
31
|
+
- - ! '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
|
+
none: false
|
|
36
|
+
requirements:
|
|
37
|
+
- - ! '>='
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 1.8.23
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 3
|
|
45
|
+
summary: An fully open source push notifications server, client libraries are also
|
|
46
|
+
available
|
|
47
|
+
test_files: []
|