websocket-controller 1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3573329d7220dca3f64720c2f42dea31daa81e09
4
+ data.tar.gz: 49a76faaba3b1cd38830b0b08c4a019687711858
5
+ SHA512:
6
+ metadata.gz: 9a1e65487785c4260315d72fec9e5b913aa391f71f776f6a348c1e1a840d146d3d77df48b945cab96d7415f6465dd176a2b283ae9b11a37ba658d54b6324a89f
7
+ data.tar.gz: ab40bfab40c785722c8307bd7ad47dbef5576e13c692c4db5c9eca17cc0aa049960e1a42320d9732ba7df4b92aaef1d3e6d71fa985ec9b56ebb4ed0c20d09032
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in websocket-controller.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 goshan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Websocket::Controller
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/websocket/controller`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'websocket-controller'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install websocket-controller
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/websocket-controller.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ namespace :socket do
2
+ desc "start web socket server"
3
+ task :start => :environment do
4
+ EM.run do
5
+ puts "Begin listen 0.0.0.0:4040"
6
+ EM::WebSocket.start(:host => "0.0.0.0", :port => 4040) do |ws|
7
+ ws.onopen do |handshake|
8
+ puts "user with signature #{ws.signature} connected"
9
+ end
10
+
11
+ ws.onmessage do |msg|
12
+ puts "user with signature #{ws.signature} sent message #{msg}"
13
+ begin
14
+ WebSocket::Parser.parse_message msg, ws
15
+ rescue Exception => e
16
+ puts "============>> Exception <<============"
17
+ puts e.inspect
18
+ puts e.backtrace
19
+ response = JSON.generate({:status => "error", :error => "invalid request"})
20
+ ws.send(response)
21
+ puts "===============>> End <<==============="
22
+ end
23
+ end
24
+
25
+ ws.onclose do
26
+ WebSocket::Parser.close_socket ws
27
+ puts "user with signature #{ws.signature} disconnected"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ module WebSocket
2
+ class Controller
3
+ attr_accessor :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def before_filter
10
+ return true
11
+ end
12
+
13
+ def current_user
14
+ User.find_by_id @params[:user_id]
15
+ end
16
+
17
+ def send_message(json, user_id=nil)
18
+ msg = JSON.generate json
19
+ user_id = @params[:user_id] unless user_id
20
+ ws = WebSocket::Manager.socket_by_user user_id
21
+ if ws
22
+ ws.send msg
23
+ puts "send message to user: #{user_id} with signature: #{ws.signature}: #{json}"
24
+ else
25
+ puts "send message failed for no socket found. user: #{user_id}, json: #{json}"
26
+ end
27
+ end
28
+
29
+ def broadcast_message(json)
30
+ msg = JSON.generate json
31
+ WebSocket::Manager.all_sockets.each do |ws|
32
+ ws.send msg
33
+ end
34
+ puts "broadcast to all sockets: #{json}"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ module WebSocket
2
+ class Manager
3
+ @@player_socket = {}
4
+ @@signature_player = {}
5
+
6
+ class << self
7
+ def bind_socket(user_id, ws)
8
+ @@player_socket[user_id] = ws
9
+ @@signature_player[ws.signature] = user_id
10
+ end
11
+
12
+ def unbind_socket(ws)
13
+ user_id = @@signature_player[ws.signature]
14
+ @@player_socket.delete user_id
15
+ @@signature_player.delete ws.signature
16
+ end
17
+
18
+ def unbind_user(user_id)
19
+ @@signature = @@player_socket[user_id].signature
20
+ @@player_socket.delete user_id
21
+ @@signature_player.delete signature
22
+ end
23
+
24
+ def all_sockets
25
+ @@player_socket.values
26
+ end
27
+
28
+ # transport between user and socket
29
+ def socket_by_user(user_id)
30
+ @@player_socket[user_id]
31
+ end
32
+
33
+ def user_by_socket(ws)
34
+ @@signature_player[ws.signature]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ module WebSocket
2
+ class Parser
3
+ def self.parse_message(msg, ws)
4
+ # parse json
5
+ begin
6
+ json = JSON.parse msg, {:symbolize_names => true}
7
+ rescue JSON::ParserError
8
+ response = JSON.generate({:status => 'error', :error => "json format error"})
9
+ ws.send(response)
10
+ return
11
+ end
12
+
13
+ # check engin and action params
14
+ unless json[:engin] && json[:action]
15
+ response = JSON.generate({:status => 'error', :error => "engin or action missing"})
16
+ ws.send(response)
17
+ return
18
+ end
19
+ engin = json[:engin].to_sym
20
+ action = json[:action]
21
+
22
+ if json[:engin] == "socket" && json[:action] == "register"
23
+ # register user with ws
24
+ WebSocket::Manager.bind_socket json[:user_id].to_i, ws
25
+ elsif WebSocket::Routes.include_engin?(engin) && WebSocket::Routes.include_action?(engin, action)
26
+ # run engin controller
27
+ json[:user_id] = WebSocket::Manager.user_by_socket ws
28
+ controller = "#{json[:engin].capitalize}Controller".constantize.send('new', json)
29
+ if controller.before_filter
30
+ controller.send(json[:action])
31
+ end
32
+ else
33
+ # 404 error
34
+ response = JSON.generate({:status => 'error', :error => "unsupport engin or action"})
35
+ ws.send(response)
36
+ end
37
+ end
38
+
39
+ def self.close_socket(ws)
40
+ WebSocket::Manager.unbind_socket ws
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ module WebSocket
2
+ class Routes
3
+ @@routes = {}
4
+
5
+ class << self
6
+ def init
7
+ @@routes = {}
8
+ end
9
+
10
+ def setup
11
+ @@routes = yield
12
+ end
13
+
14
+ def include_engin?(engin)
15
+ @@routes.keys.include? engin
16
+ end
17
+
18
+ def include_action?(engin, action)
19
+ @@routes[engin].include? action
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module WebsocketController
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "websocket/version"
2
+ require "websocket/routes"
3
+ require "websocket/manager"
4
+ require "websocket/parser"
5
+ require "websocket/controller"
6
+
7
+
8
+ module WebSocket
9
+ class Railtie < ::Rails::Railtie
10
+ rake_tasks do
11
+ load 'tasks/socket.rake'
12
+ end
13
+ end
14
+ end
15
+
16
+ module WebSocket
17
+ Routes.init
18
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'websocket/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "websocket-controller"
8
+ spec.version = WebsocketController::VERSION
9
+ spec.authors = ["goshan"]
10
+ spec.email = ["goshan.hanqiu@gmail.com"]
11
+
12
+ spec.summary = %q{a web socket engin just like rails controllers}
13
+ spec.description = %q{a rails controller class make you can write web-socket code}
14
+ spec.homepage = "https://github.com/goshan/websocket-controller"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ #end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.10"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+
31
+ spec.add_runtime_dependency "eventmachine", ">= 0.12.9"
32
+ spec.add_runtime_dependency "http_parser.rb", "~> 0.6.0"
33
+ spec.add_runtime_dependency "em-websocket", "~> 0.5"
34
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: websocket-controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - goshan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: eventmachine
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.12.9
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.12.9
55
+ - !ruby/object:Gem::Dependency
56
+ name: http_parser.rb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.6.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.6.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: em-websocket
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
83
+ description: a rails controller class make you can write web-socket code
84
+ email:
85
+ - goshan.hanqiu@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/tasks/socket.rake
96
+ - lib/websocket-controller.rb
97
+ - lib/websocket/controller.rb
98
+ - lib/websocket/manager.rb
99
+ - lib/websocket/parser.rb
100
+ - lib/websocket/routes.rb
101
+ - lib/websocket/version.rb
102
+ - websocket-controller.gemspec
103
+ homepage: https://github.com/goshan/websocket-controller
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: a web socket engin just like rails controllers
127
+ test_files: []