webrtc-rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e42286f0f578e7c50fbc02e8718dffc5f137813
4
- data.tar.gz: 1bab5500d6106da4d5ab92f731368edba4e05bb5
3
+ metadata.gz: 9d339834737230df0935d9e8c788cc54265e44b3
4
+ data.tar.gz: 86f28c8f99f96d7e71b9b96c04234b6e245ced86
5
5
  SHA512:
6
- metadata.gz: b7c9fe5c45454e28ced58826f3085ad22e7786efbd6c91c78cfd5f84b121173aa07bed10f307b9a34902bb5342031e7a53d70d866ce3e93466ee4f4977487c62
7
- data.tar.gz: e41bf81bfcdb494dbeb121f0b4e4bf79ba17a3984c9fa7af256a87e50ffd043d036d31702b342a340f9fc45c245ba4f11ef63f0ef450f45e17016ea04c37fb3a
6
+ metadata.gz: e97489ca7fd9cb466e9844a62003c308939e0af720ad52829668239126f257e1eac99abef5712e411b621a41286c300913be8f8170a36217bac6d68233de7e9c
7
+ data.tar.gz: 150ae7a9d38706e3f3531003b923248d6d555627ae457d01dc041d38de95c4ab2b89f7408451b6d169e3fe51e488bc2cf71cf83ad3f3b265a9c2a72bf6a93c1e
@@ -8,12 +8,14 @@ class @WebRTC
8
8
  onWebSocketConnected: ->
9
9
  onWebSocketReconnectingStarted: ->
10
10
  onWebSocketReconnected: ->
11
+ onWebRTCCall: (remoteUserID) ->
11
12
  onWebRTCConnected: ->
12
13
  onWebRTCReconnectingStarted: ->
13
14
  onWebRTCReconnected: ->
14
15
  onWebRTCHangedUp: ->
15
16
  onWebRTCConnectFailed: (reason) ->
16
17
  onServerMessage: (message) ->
18
+ onUserMessage: (sentUserID, event, message) ->
17
19
 
18
20
  constructor: (url, userToken, localOutput, remoteOutput) ->
19
21
  @localOutput = if localOutput? then (localOutput[0] || localOutput) else null
@@ -48,6 +50,18 @@ class @WebRTC
48
50
  else
49
51
  alert 'Local stream not running yet - try again.'
50
52
 
53
+ answer: ->
54
+ @_isCaller = false
55
+ @_sendOffer()
56
+ @_peerStarted = true
57
+
58
+ sendUserMessage: (userID, event, message) ->
59
+ @_sendValue('userMessage',
60
+ userID: String(userID)
61
+ event: event
62
+ message: message
63
+ )
64
+
51
65
  enableVideo: ->
52
66
  @setVideoEnabled(true)
53
67
 
@@ -91,10 +105,14 @@ class @WebRTC
91
105
  @_webSocket.onmessage = (data) =>
92
106
  event = JSON.parse(data.data)
93
107
  eventType = event['type']
108
+ if eventType == 'userMessage'
109
+ @onUserMessage(event['remoteUserID'], event['event'], event['message'])
110
+ return
111
+
94
112
  if eventType != 'myUserID' && eventType != 'call' && eventType != 'webSocketReconnected'
95
113
  if @_remoteUserID != event['remoteUserID']
96
114
  return
97
-
115
+
98
116
  switch eventType
99
117
  when 'myUserID'
100
118
  @myUserID = event['myUserID']
@@ -122,10 +140,8 @@ class @WebRTC
122
140
  else if event['reconnect'] && @_hangedUp
123
141
  @_sendMessage(type: 'hangUp')
124
142
  else
125
- @_isCaller = false
126
143
  @_remoteUserID = event['remoteUserID']
127
- @_sendOffer()
128
- @_peerStarted = true
144
+ @onWebRTCCall(@_remoteUserID)
129
145
  when 'hangUp'
130
146
  @_callAnswerReceived = true
131
147
  @_hangUp()
@@ -141,6 +157,8 @@ class @WebRTC
141
157
  @_onCandidate(event)
142
158
  when 'serverMessage'
143
159
  @onServerMessage(event['message'])
160
+ when 'userMessage'
161
+ @onUserMessage(event['userType'], event['message'])
144
162
 
145
163
  _addNetworkEventListener: ->
146
164
  window.addEventListener('offline', (event) =>
@@ -3,14 +3,20 @@ require 'rails'
3
3
  module WebrtcRails
4
4
  module Generators
5
5
  class InstallGenerator < Rails::Generators::Base
6
- def add_gems
7
- gem 'daemons-rails'
8
- gem 'em-websocket'
9
- gem 'em-hiredis'
10
- end
6
+ source_root File.expand_path("../templates", __FILE__)
11
7
 
12
- def injection
13
- generate 'webrtc_rails:injection'
8
+ def create_initializer_file
9
+ initializer_path = File.join('config', 'initializers')
10
+ file_name = 'webrtc_rails.rb'
11
+ template file_name, File.join(initializer_path, file_name)
12
+ end
13
+
14
+ def injection_js
15
+ append_to_file File.join('app', 'assets', 'javascripts', 'application.js') do
16
+ out = ''
17
+ out << "\n\n// append by webrtc_rails\n"
18
+ out << "//= require webrtc_rails/main\n\n"
19
+ end
14
20
  end
15
21
  end
16
22
  end
@@ -0,0 +1,6 @@
1
+ WebrtcRails.configure do |config|
2
+ # config.user_model_class = 'User'
3
+ # config.fetch_user_by_token_method = :fetch_by_token
4
+ # config.user_id = :id
5
+ # config.daemon_delegate = 'WebrtcRails::DaemonDelegate'
6
+ end
@@ -0,0 +1,56 @@
1
+ require 'webrtc-rails'
2
+
3
+ namespace :webrtc_rails do
4
+ desc 'start webrtc daemon'
5
+ task start: :environment do
6
+ start
7
+ end
8
+
9
+ desc 'stop webrtc daemon'
10
+ task stop: :environment do
11
+ stop
12
+ end
13
+
14
+ desc 'restart webrtc daemon'
15
+ task restart: :environment do
16
+ stop
17
+ start
18
+ end
19
+
20
+ @pid_file = "#{Rails.root}/tmp/pids/webrtc_rails.pid"
21
+
22
+ def start
23
+ if File.exists?(@pid_file)
24
+ pid = File.read(@pid_file)
25
+ begin
26
+ if Process.kill(0, pid.to_i)
27
+ puts 'webrtc daemon alrerady exists'
28
+ return
29
+ end
30
+ rescue Errno::ESRCH
31
+ end
32
+ end
33
+ puts 'webrtc daemon started'
34
+ Process.daemon
35
+ File.write(@pid_file, Process.pid.to_s)
36
+ daemon = WebrtcRails::Daemon.new
37
+ daemon.start
38
+ end
39
+
40
+ def stop
41
+ unless File.exists?(@pid_file)
42
+ puts 'webrtc daemon does not exist'
43
+ return
44
+ end
45
+
46
+ pid = File.read(@pid_file)
47
+ begin
48
+ if Process.kill(:INT, pid.to_i)
49
+ puts 'webrtc daemon stoped'
50
+ File.delete(@pid_file)
51
+ end
52
+ rescue Errno::ESRCH
53
+ puts 'webrtc daemon does not exist'
54
+ end
55
+ end
56
+ end
data/lib/webrtc-rails.rb CHANGED
@@ -1,2 +1,17 @@
1
+ require 'webrtc_rails/daemon'
2
+ require 'webrtc_rails/daemon_delegate'
1
3
  require 'webrtc_rails/server_message'
4
+ require 'webrtc_rails/engine'
5
+ require 'webrtc_rails/configuration'
2
6
  require 'webrtc_rails/version'
7
+
8
+ module WebrtcRails
9
+ class << self
10
+ attr_accessor :configuration
11
+ end
12
+
13
+ def self.configure
14
+ self.configuration ||= WebrtcRails::Configuration.new
15
+ yield(configuration)
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module WebrtcRails
2
+ class Configuration
3
+ attr_accessor :user_model_class, :fetch_user_by_token_method, :user_id, :daemon_delegate
4
+
5
+ def initialize
6
+ @user_model_class = 'User'
7
+ @fetch_user_by_token_method = :fetch_by_token
8
+ @user_id = :id
9
+ @daemon_delegate = 'WebrtcRails::DaemonDelegate'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,114 @@
1
+ require 'webrtc-rails'
2
+ require 'em-websocket'
3
+ require 'em-hiredis'
4
+
5
+ module WebrtcRails
6
+ class Daemon
7
+ def initialize
8
+ @websockets = {}
9
+ @config = WebrtcRails.configuration
10
+ @user_class = @config.user_model_class.constantize
11
+ @fetch_user_by_token_method = @config.fetch_user_by_token_method
12
+ @user_id = @config.user_id
13
+ @daemon_delegate = @config.daemon_delegate.constantize.new
14
+ end
15
+
16
+ def start
17
+ EM.run do
18
+ redis = EM::Hiredis.connect
19
+ pubsub = redis.pubsub
20
+ pubsub.subscribe('webrtc-rails')
21
+ pubsub.on(:message) do |channel, message|
22
+ data = JSON.parse(message, {symbolize_names: true})
23
+ user_id = data[:user_id].to_s
24
+ message = data[:message]
25
+ if @websockets.key?(user_id)
26
+ for ws in @websockets[user_id]
27
+ send_data = {
28
+ type: 'serverMessage',
29
+ message: message
30
+ }
31
+ ws.send JSON.generate(send_data)
32
+ end
33
+ end
34
+ end
35
+
36
+ EM::WebSocket.run(host: 'localhost', port: 3001) do |websocket|
37
+ my_user_id = nil
38
+
39
+ websocket.onclose do
40
+ if my_user_id.present?
41
+ if @websockets[my_user_id].present?
42
+ @websockets[my_user_id].delete(websocket)
43
+ @daemon_delegate.onWebSocketDisconnected(my_user_id)
44
+ end
45
+ end
46
+ end
47
+
48
+ websocket.onmessage do |message|
49
+ data = JSON.parse(message, {symbolize_names: true})
50
+ if data[:event] != 'heartbeat'
51
+ token = data[:token]
52
+ if token.present?
53
+ user = @user_class.send(@fetch_user_by_token_method, token)
54
+ my_user_id = user ? user.send(@user_id).to_s : nil
55
+ if my_user_id.present?
56
+ case data[:event]
57
+ when 'userMessage'
58
+ user_id = data[:value][:userID]
59
+ event = data[:value][:event]
60
+ message = data[:value][:message]
61
+ if @daemon_delegate.onWantSendUserMessage(my_user_id, user_id, event, message)
62
+ message = {
63
+ type: 'userMessage',
64
+ remoteUserID: my_user_id,
65
+ event: event,
66
+ message: message
67
+ }
68
+ sendMessage(user_id, message)
69
+ end
70
+ when 'setMyToken'
71
+ @websockets[my_user_id] ||= []
72
+ @websockets[my_user_id].push(websocket)
73
+ message = {
74
+ type: 'myUserID',
75
+ myUserID: my_user_id
76
+ }
77
+ @daemon_delegate.onWebSocketConnected(my_user_id)
78
+ websocket.send JSON.generate(message)
79
+ when 'sendMessage'
80
+ user_id = data[:value][:userID]
81
+ type = data[:value][:message][:type]
82
+ allow_types = %w/call hangUp offer answer candidate callFailed userMessage webSocketReconnected/
83
+ if @websockets.key?(user_id) && type.present? && allow_types.include?(type)
84
+ if type != 'call' || @daemon_delegate.onWantCall(my_user_id, user_id)
85
+ message = data[:value][:message]
86
+ message[:remoteUserID] = my_user_id
87
+ sendMessage(user_id, message)
88
+ end
89
+ else
90
+ message = {
91
+ type: 'callFailed',
92
+ reason: 0,
93
+ remoteUserID: user_id
94
+ }
95
+ websocket.send JSON.generate(message)
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def sendMessage(user_id, message)
109
+ for ws in @websockets[user_id]
110
+ ws.send JSON.generate(message)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,19 @@
1
+ module WebrtcRails
2
+ class DaemonDelegate
3
+ def onWebSocketConnected(user_id)
4
+
5
+ end
6
+
7
+ def onWebSocketDisconnected(user_id)
8
+
9
+ end
10
+
11
+ def onWantCall(sent_user_id, will_receive_user_id)
12
+ true
13
+ end
14
+
15
+ def onWantSendUserMessage(sent_user_id, will_receive_user_id, event, message)
16
+ true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module WebrtcRails
2
+ class Engine < Rails::Engine
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module WebrtcRails
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/webrtc-rails.gemspec CHANGED
@@ -19,8 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'em-websocket'
22
- spec.add_dependency 'redis'
23
- spec.add_dependency 'daemons-rails'
22
+ spec.add_dependency 'em-hiredis'
24
23
 
25
24
  spec.add_development_dependency "bundler", "~> 1.10"
26
25
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrtc-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji Murata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-25 00:00:00.000000000 Z
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: em-websocket
@@ -25,21 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: redis
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: daemons-rails
28
+ name: em-hiredis
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - ">="
@@ -111,17 +97,16 @@ files:
111
97
  - Rakefile
112
98
  - bin/console
113
99
  - bin/setup
114
- - lib/generators/webrtc_rails/injection/USAGE
115
- - lib/generators/webrtc_rails/injection/injection_generator.rb
100
+ - lib/assets/javascripts/webrtc_rails/main.js.coffee
116
101
  - lib/generators/webrtc_rails/install/USAGE
117
102
  - lib/generators/webrtc_rails/install/install_generator.rb
118
- - lib/generators/webrtc_rails/templates/daemons
119
- - lib/generators/webrtc_rails/templates/daemons.yml
120
- - lib/generators/webrtc_rails/templates/main.js.coffee
121
- - lib/generators/webrtc_rails/templates/webrtc.rb
122
- - lib/generators/webrtc_rails/templates/webrtc_controller.rb
123
- - lib/generators/webrtc_rails/templates/webrtc_ctl
103
+ - lib/generators/webrtc_rails/install/templates/webrtc_rails.rb
104
+ - lib/tasks/webrtc_rails.rake
124
105
  - lib/webrtc-rails.rb
106
+ - lib/webrtc_rails/configuration.rb
107
+ - lib/webrtc_rails/daemon.rb
108
+ - lib/webrtc_rails/daemon_delegate.rb
109
+ - lib/webrtc_rails/engine.rb
125
110
  - lib/webrtc_rails/server_message.rb
126
111
  - lib/webrtc_rails/version.rb
127
112
  - webrtc-rails.gemspec
@@ -1,8 +0,0 @@
1
- Description:
2
- Explain the generator
3
-
4
- Example:
5
- rails generate webrtc_rails:injection Thing
6
-
7
- This will create:
8
- what/will/it/create
@@ -1,39 +0,0 @@
1
- require 'rails'
2
-
3
- module WebrtcRails
4
- module Generators
5
- class InjectionGenerator < Rails::Generators::Base
6
- source_root File.expand_path("../../templates", __FILE__)
7
-
8
- def generate_daemon
9
- daemons_dir = ::Daemons::Rails.configuration.daemons_directory
10
- unless File.exists?(Rails.root.join(daemons_dir, 'daemons'))
11
- copy_file "daemons", daemons_dir.join('daemons')
12
- chmod daemons_dir.join('daemons'), 0755
13
- end
14
-
15
- script_path = daemons_dir.join('webrtc.rb')
16
- template 'webrtc.rb', script_path
17
- chmod script_path, 0755
18
-
19
- ctl_path = daemons_dir.join('webrtc_ctl')
20
- template "webrtc_ctl", ctl_path
21
- chmod ctl_path, 0755
22
-
23
- unless File.exists?(Rails.root.join('config', 'daemons.yml'))
24
- copy_file 'daemons.yml', 'config/daemons.yml'
25
- end
26
- end
27
-
28
- def create_events_initializer_file
29
- js_path = File.join('app', 'assets', 'javascripts')
30
- template 'main.js.coffee', File.join(js_path, 'webrtc_rails', 'main.js.coffee')
31
- append_to_file File.join(js_path, 'application.js') do
32
- out = ''
33
- out << "\n\n// append by webrtc_rails\n"
34
- out << "//= require webrtc_rails/main\n\n"
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env ruby
2
- results = []
3
- Dir[File.dirname(__FILE__) + "/*_ctl"].each {|f| results << `ruby #{f} #{ARGV.first}`}
4
- results.delete_if { |result| result.nil? || result.empty? }
5
- puts results.join unless results.empty?
@@ -1,6 +0,0 @@
1
- dir_mode: script
2
- dir: ../../log
3
- multiple: false
4
- backtrace: true
5
- monitor: false
6
- ontop: false
@@ -1,85 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- ENV["RAILS_ENV"] ||= "production"
4
-
5
- root = File.expand_path(File.dirname(__FILE__))
6
- root = File.dirname(root) until File.exists?(File.join(root, 'config'))
7
- Dir.chdir(root)
8
-
9
- require File.join(root, "config", "environment")
10
-
11
- @websockets = {}
12
-
13
- EM.run do
14
- redis = EM::Hiredis.connect
15
- pubsub = redis.pubsub
16
- pubsub.subscribe('webrtc-rails')
17
- pubsub.on(:message) do |channel, message|
18
- data = JSON.parse(message, {symbolize_names: true})
19
- user_id = data[:user_id].to_s
20
- message = data[:message]
21
- if @websockets.key?(user_id)
22
- for ws in @websockets[user_id]
23
- send_data = {
24
- type: 'serverMessage',
25
- message: message
26
- }
27
- ws.send JSON.generate(send_data)
28
- end
29
- end
30
- end
31
-
32
- EM::WebSocket.run(host: 'localhost', port: 3001) do |websocket|
33
- my_user_id = nil
34
-
35
- websocket.onclose do
36
- if my_user_id.present?
37
- if @websockets[my_user_id].present?
38
- @websockets[my_user_id].delete(websocket)
39
- end
40
- end
41
- end
42
-
43
- websocket.onmessage do |message|
44
- data = JSON.parse(message, {symbolize_names: true})
45
- if data[:event] != 'heartbeat'
46
- token = data[:token]
47
- if token.present?
48
- user = User.fetch_by_token(token)
49
- my_user_id = user ? user.id.to_s : nil
50
- if my_user_id.present?
51
- case data[:event]
52
- when 'setMyToken'
53
- @websockets[my_user_id] ||= []
54
- @websockets[my_user_id].push(websocket)
55
- message = {
56
- type: 'myUserID',
57
- myUserID: my_user_id
58
- }
59
- websocket.send JSON.generate(message)
60
- when 'sendMessage'
61
- user_id = data[:value][:userID]
62
- type = data[:value][:message][:type]
63
- Rails.logger.info type
64
- allow_types = %w/call hangUp offer answer candidate callFailed webSocketReconnected/
65
- if @websockets.key?(user_id) && type.present? && allow_types.include?(type)
66
- for ws in @websockets[user_id]
67
- message = data[:value][:message]
68
- message[:remoteUserID] = my_user_id
69
- ws.send JSON.generate(message)
70
- end
71
- else
72
- message = {
73
- type: 'callFailed',
74
- reason: 0,
75
- remoteUserID: user_id
76
- }
77
- websocket.send JSON.generate(message)
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end
@@ -1,6 +0,0 @@
1
- class WebrtcController < ApplicationController
2
- def send_message
3
- WebsocketRails.users[params[:user_id]].send_message 'webrtc', params[:message]
4
- render :json => 'OK'
5
- end
6
- end
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'daemons/rails/config'
4
-
5
- config = Daemons::Rails::Config.for_controller(File.expand_path(__FILE__))
6
- Daemons::Rails.run config[:script], config.to_hash