simple_pusher 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTZiOWQ2ZjAxYzAyMjVlMTc3NDkyNTI0YzljOTU4MmYzOWYwNTc0Ng==
5
+ data.tar.gz: !binary |-
6
+ MDVjZWQ4OWQ4NjU0YjkzNzQ3OGJmNzc3OWJkNTI1OTZjZDU3MDA4MQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Y2IwZGUxNTI3MzY0YzgzZTNjMWZhZDk5NmUyOTlkMWQzM2RlZjdhNWY4ZWE1
10
+ Mzc5MDZmNTM1MzA4NzJmMDYzM2U5ZjFlNDM0MzcwY2EzYzBmYTFkMTc2YjRi
11
+ YmM5MWMyYzk1YWYwOWI5M2Y1MTVkNzBkN2IxYTM3MmQwMTkyNTQ=
12
+ data.tar.gz: !binary |-
13
+ NDUwOTNlNzUxNzRiNTVkZDRiNGZkYTcxNWNmNjRmNjkwNTc4NzZhZWRkZWY3
14
+ ZWUwYjgxMjgzMWFmYjdlNWFkNjIzZjQxMWQ1YTIwYzA5OGIyOTBkZGI2YWMy
15
+ MjRmNThkODVmNjJmYzFkM2JhMzdhNjU5ODVhZDMxZDg3ZmM2MWM=
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea/
7
+ .DS_Store
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 qichunren
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # SimplePusher
2
+
3
+ SimplePusher is a HTML5 websocket powered realtime messaging tool.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple_pusher'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_pusher
18
+
19
+ ## Usage
20
+
21
+ 1. Start simple pusher server in rails config/initializers.
22
+
23
+ ```
24
+ # config/initializers/simple_pusher.rb
25
+ EventMachine.next_tick do
26
+ SimplePusher.setup do |config|
27
+ config.port = 8088
28
+ config.debug = false
29
+ end
30
+ SimplePusher.start
31
+ end
32
+ ```
33
+
34
+ 2. Setup client js code.
35
+
36
+ ```
37
+ var simple_pusher = new SimplePusher("ws://<%= request.host %>:8088/");
38
+ simple_pusher.on("message", function(message){
39
+ console.log("Receive:", message )
40
+ });
41
+
42
+ simple_pusher.on("online_count", function(message){
43
+
44
+ });
45
+ ```
46
+
47
+ 3. Broadcast message via server side.
48
+
49
+ ```
50
+ SimplePusher.broadcast("Time now #{Time.now.to_s(:db}")
51
+ ```
52
+
53
+ 4. Broadcast message via client side.
54
+
55
+ ```
56
+ simple_pusher.broadcast("Hello everybody.")
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ module SimplePusher
2
+ class Configuration
3
+ attr_accessor :port
4
+ attr_accessor :debug
5
+
6
+ def initialize
7
+ @port ||= 8088
8
+ @debug ||= false
9
+ end
10
+
11
+
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ module SimplePusher
2
+ class Connection
3
+ attr_accessor :websocket
4
+ attr_accessor :name
5
+
6
+ def self.clients
7
+ @@clients ||= {}
8
+ end
9
+
10
+ def self.add_client(socket)
11
+ client = self.new(socket)
12
+ clients[socket] = client
13
+ end
14
+
15
+ def self.remove_client(socket)
16
+ clients.delete(socket)
17
+ end
18
+
19
+ def self.broadcast(message)
20
+ clients.each do |websocket, client|
21
+ websocket.send message
22
+ end
23
+ end
24
+
25
+ def self.count
26
+ clients.size
27
+ end
28
+
29
+ def initialize(websocket)
30
+ @websocket = websocket
31
+ end
32
+
33
+ def emit(message)
34
+ @websocket.send(message)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module SimplePusher
2
+ class Engine < ::Rails::Engine
3
+
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require 'singleton'
2
+ require 'em-websocket'
3
+ require 'connection'
4
+
5
+ module SimplePusher
6
+ class Server
7
+ include Singleton
8
+
9
+ attr :host, :port
10
+
11
+ def initialize
12
+ @host = "0.0.0.0"
13
+ @port = SimplePusher.configuration.port
14
+ end
15
+
16
+ def self.start
17
+ $stderr.puts "Start SimplePush ..." if SimplePusher.configuration.debug
18
+ EM::WebSocket.run(:host => instance.host, :port => instance.port) do |socket|
19
+ socket.onopen do |handshake|
20
+ $stderr.puts "on open:" + handshake.inspect if SimplePusher.configuration.debug
21
+ Connection.add_client(socket)
22
+ end
23
+
24
+ socket.onclose do
25
+ $stderr.puts "on close"
26
+ Connection.remove_client(socket)
27
+ end
28
+
29
+ socket.onmessage do |raw_message|
30
+ $stderr.puts "on message: " + raw_message if SimplePusher.configuration.debug
31
+ action, *message = raw_message.split(":")
32
+
33
+ case action
34
+ when 'broadcast'
35
+ message = message.join(":")
36
+ Connection.broadcast(message)
37
+ else
38
+ #TODO
39
+ end
40
+ end
41
+
42
+
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module SimplePusher
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,35 @@
1
+ require "simple_pusher/version"
2
+ require 'em-websocket'
3
+ require 'simple_pusher/engine' if defined?(Rails)
4
+
5
+ module SimplePusher
6
+
7
+ class << self
8
+ def setup
9
+ yield configuration
10
+ configuration
11
+ end
12
+
13
+ def configuration
14
+ @_configuration ||= Configuration.new
15
+ end
16
+ end
17
+
18
+ def start(options={})
19
+ EventMachine.next_tick do
20
+ Server.start
21
+ end
22
+ end
23
+
24
+ def broadcast(message)
25
+ Connection.broadcast(message)
26
+ end
27
+
28
+ module_function :start
29
+ module_function :broadcast
30
+
31
+ end
32
+
33
+ SimplePusher.autoload :Configuration, "simple_pusher/configuration"
34
+ SimplePusher.autoload :Connection, "simple_pusher/connection"
35
+ SimplePusher.autoload :Server, "simple_pusher/server"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_pusher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_pusher"
8
+ spec.version = SimplePusher::VERSION
9
+ spec.authors = ["qichunren"]
10
+ spec.email = ["whyruby@gmail.com"]
11
+ spec.description = %q{Simple pusher based on websocket}
12
+ spec.summary = %q{Simple pusher based on websocket, build with Eventmachine}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "em-websocket", "~> 0.5.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path("../test_helper", __FILE__)
2
+
3
+ describe SimplePusher do
4
+
5
+ it "must respond version method" do
6
+ SimplePusher::VERSION.wont_be_nil
7
+ end
8
+
9
+ it "must respond listen method" do
10
+ #SimplePusher.repond_with
11
+ end
12
+
13
+
14
+
15
+ end
16
+
@@ -0,0 +1,10 @@
1
+ ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
2
+ require 'bundler'
3
+ Bundler.setup
4
+ Bundler.require :default, :test
5
+
6
+ require 'minitest/autorun'
7
+
8
+
9
+ lib = File.expand_path('../../lib', __FILE__)
10
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
@@ -0,0 +1,26 @@
1
+ class SimplePusher
2
+ constructor: (url) ->
3
+ @websocket = new WebSocket(url)
4
+ @websocket.onmessage = (e) ->
5
+ event = e.data.split(":")
6
+
7
+ on: (event_name, callback) ->
8
+ if event_name == 'connect'
9
+ console.log("on open")
10
+ @websocket.onopen = callback
11
+ else if event_name == 'message'
12
+ console.log("on message")
13
+ @websocket.onmessage = (e) ->
14
+ callback(e.data)
15
+ else if event_name == 'close'
16
+ console.log("on close")
17
+ @websocket.onclose = callback
18
+
19
+ broadcast: (message) ->
20
+ @websocket.send("broadcast:#{message}")
21
+
22
+ emit: (event, message_object) ->
23
+ @websocket.send("emit:#{event}:#{message_object}")
24
+
25
+
26
+ window.SimplePusher = SimplePusher
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_pusher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - qichunren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: em-websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple pusher based on websocket
56
+ email:
57
+ - whyruby@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/simple_pusher.rb
68
+ - lib/simple_pusher/configuration.rb
69
+ - lib/simple_pusher/connection.rb
70
+ - lib/simple_pusher/engine.rb
71
+ - lib/simple_pusher/server.rb
72
+ - lib/simple_pusher/version.rb
73
+ - simple_pusher.gemspec
74
+ - test/simple_pusher_test.rb
75
+ - test/test_helper.rb
76
+ - vendor/assets/javascripts/simple_pusher.coffee
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Simple pusher based on websocket, build with Eventmachine
101
+ test_files:
102
+ - test/simple_pusher_test.rb
103
+ - test/test_helper.rb