hkroger-websocket-rails 0.7.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.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +328 -0
  3. data/Gemfile +27 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +237 -0
  6. data/Rakefile +72 -0
  7. data/bin/thin-socketrails +45 -0
  8. data/lib/assets/javascripts/websocket_rails/abstract_connection.js.coffee +45 -0
  9. data/lib/assets/javascripts/websocket_rails/channel.js.coffee +70 -0
  10. data/lib/assets/javascripts/websocket_rails/event.js.coffee +42 -0
  11. data/lib/assets/javascripts/websocket_rails/http_connection.js.coffee +66 -0
  12. data/lib/assets/javascripts/websocket_rails/main.js +6 -0
  13. data/lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee +29 -0
  14. data/lib/assets/javascripts/websocket_rails/websocket_rails.js.coffee +158 -0
  15. data/lib/config.ru +3 -0
  16. data/lib/generators/websocket_rails/install/install_generator.rb +33 -0
  17. data/lib/generators/websocket_rails/install/templates/events.rb +14 -0
  18. data/lib/generators/websocket_rails/install/templates/websocket_rails.rb +63 -0
  19. data/lib/hkroger-websocket-rails.rb +1 -0
  20. data/lib/rails/app/controllers/websocket_rails/delegation_controller.rb +13 -0
  21. data/lib/rails/config/routes.rb +7 -0
  22. data/lib/rails/tasks/websocket_rails.tasks +42 -0
  23. data/lib/spec_helpers/matchers/route_matchers.rb +65 -0
  24. data/lib/spec_helpers/matchers/trigger_matchers.rb +113 -0
  25. data/lib/spec_helpers/spec_helper_event.rb +34 -0
  26. data/lib/websocket-rails.rb +108 -0
  27. data/lib/websocket_rails/base_controller.rb +197 -0
  28. data/lib/websocket_rails/channel.rb +97 -0
  29. data/lib/websocket_rails/channel_manager.rb +55 -0
  30. data/lib/websocket_rails/configuration.rb +169 -0
  31. data/lib/websocket_rails/connection_adapters.rb +195 -0
  32. data/lib/websocket_rails/connection_adapters/http.rb +120 -0
  33. data/lib/websocket_rails/connection_adapters/web_socket.rb +36 -0
  34. data/lib/websocket_rails/connection_manager.rb +119 -0
  35. data/lib/websocket_rails/controller_factory.rb +80 -0
  36. data/lib/websocket_rails/data_store.rb +145 -0
  37. data/lib/websocket_rails/dispatcher.rb +129 -0
  38. data/lib/websocket_rails/engine.rb +26 -0
  39. data/lib/websocket_rails/event.rb +189 -0
  40. data/lib/websocket_rails/event_map.rb +184 -0
  41. data/lib/websocket_rails/event_queue.rb +33 -0
  42. data/lib/websocket_rails/internal_events.rb +37 -0
  43. data/lib/websocket_rails/logging.rb +133 -0
  44. data/lib/websocket_rails/spec_helpers.rb +3 -0
  45. data/lib/websocket_rails/synchronization.rb +182 -0
  46. data/lib/websocket_rails/user_manager.rb +276 -0
  47. data/lib/websocket_rails/version.rb +3 -0
  48. data/spec/dummy/Rakefile +7 -0
  49. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  50. data/spec/dummy/app/controllers/chat_controller.rb +53 -0
  51. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  52. data/spec/dummy/app/models/user.rb +2 -0
  53. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  54. data/spec/dummy/config.ru +4 -0
  55. data/spec/dummy/config/application.rb +45 -0
  56. data/spec/dummy/config/boot.rb +10 -0
  57. data/spec/dummy/config/database.yml +22 -0
  58. data/spec/dummy/config/environment.rb +5 -0
  59. data/spec/dummy/config/environments/development.rb +26 -0
  60. data/spec/dummy/config/environments/production.rb +49 -0
  61. data/spec/dummy/config/environments/test.rb +34 -0
  62. data/spec/dummy/config/events.rb +7 -0
  63. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  64. data/spec/dummy/config/initializers/inflections.rb +10 -0
  65. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  66. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  67. data/spec/dummy/config/initializers/session_store.rb +8 -0
  68. data/spec/dummy/config/locales/en.yml +5 -0
  69. data/spec/dummy/config/routes.rb +58 -0
  70. data/spec/dummy/db/development.sqlite3 +0 -0
  71. data/spec/dummy/db/migrate/20130902222552_create_users.rb +10 -0
  72. data/spec/dummy/db/schema.rb +23 -0
  73. data/spec/dummy/db/test.sqlite3 +0 -0
  74. data/spec/dummy/log/development.log +17 -0
  75. data/spec/dummy/log/production.log +0 -0
  76. data/spec/dummy/log/server.log +0 -0
  77. data/spec/dummy/public/404.html +26 -0
  78. data/spec/dummy/public/422.html +26 -0
  79. data/spec/dummy/public/500.html +26 -0
  80. data/spec/dummy/public/favicon.ico +0 -0
  81. data/spec/dummy/public/javascripts/application.js +2 -0
  82. data/spec/dummy/public/javascripts/controls.js +965 -0
  83. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  84. data/spec/dummy/public/javascripts/effects.js +1123 -0
  85. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  86. data/spec/dummy/public/javascripts/rails.js +202 -0
  87. data/spec/dummy/script/rails +6 -0
  88. data/spec/integration/connection_manager_spec.rb +135 -0
  89. data/spec/javascripts/support/jasmine.yml +52 -0
  90. data/spec/javascripts/support/jasmine_helper.rb +38 -0
  91. data/spec/javascripts/support/vendor/sinon-1.7.1.js +4343 -0
  92. data/spec/javascripts/websocket_rails/channel_spec.coffee +112 -0
  93. data/spec/javascripts/websocket_rails/event_spec.coffee +69 -0
  94. data/spec/javascripts/websocket_rails/helpers.coffee +6 -0
  95. data/spec/javascripts/websocket_rails/websocket_connection_spec.coffee +158 -0
  96. data/spec/javascripts/websocket_rails/websocket_rails_spec.coffee +274 -0
  97. data/spec/spec_helper.rb +41 -0
  98. data/spec/spec_helpers/matchers/route_matchers_spec.rb +109 -0
  99. data/spec/spec_helpers/matchers/trigger_matchers_spec.rb +247 -0
  100. data/spec/spec_helpers/spec_helper_event_spec.rb +66 -0
  101. data/spec/support/helper_methods.rb +42 -0
  102. data/spec/support/mock_web_socket.rb +41 -0
  103. data/spec/unit/base_controller_spec.rb +74 -0
  104. data/spec/unit/channel_manager_spec.rb +58 -0
  105. data/spec/unit/channel_spec.rb +169 -0
  106. data/spec/unit/connection_adapters/http_spec.rb +88 -0
  107. data/spec/unit/connection_adapters/web_socket_spec.rb +30 -0
  108. data/spec/unit/connection_adapters_spec.rb +259 -0
  109. data/spec/unit/connection_manager_spec.rb +148 -0
  110. data/spec/unit/controller_factory_spec.rb +76 -0
  111. data/spec/unit/data_store_spec.rb +106 -0
  112. data/spec/unit/dispatcher_spec.rb +203 -0
  113. data/spec/unit/event_map_spec.rb +120 -0
  114. data/spec/unit/event_queue_spec.rb +36 -0
  115. data/spec/unit/event_spec.rb +181 -0
  116. data/spec/unit/logging_spec.rb +162 -0
  117. data/spec/unit/synchronization_spec.rb +150 -0
  118. data/spec/unit/target_validator_spec.rb +88 -0
  119. data/spec/unit/user_manager_spec.rb +165 -0
  120. metadata +320 -0
@@ -0,0 +1,276 @@
1
+ module WebsocketRails
2
+
3
+ # Contains a Hash of all connected users. This
4
+ # can be used to trigger an event on a specific
5
+ # user from outside of a WebsocketRails controller.
6
+ #
7
+ # The key for a particular user is defined in the
8
+ # configuration as `config.user_identifier`.
9
+ #
10
+ # If there is a `current_user` method defined
11
+ # in ApplicationController and a user is signed
12
+ # in to your application when the connection is
13
+ # opened, WebsocketRails will call the method
14
+ # defined in `config.user_identifier` on the
15
+ # `current_user` object and use that value as
16
+ # the key.
17
+ #
18
+ # # In your events.rb file
19
+ # WebsocketRails.setup do |config|
20
+ # # Defaults to :id
21
+ # config.user_identifier = :name
22
+ # end
23
+ #
24
+ # # In a standard controller or background job
25
+ # name = current_user.name
26
+ # WebsocketRails.users[name].send_message :event_name, data
27
+ #
28
+ # If no `current_user` method is defined or the
29
+ # user is not signed in when the WebsocketRails
30
+ # connection is opened, the connection will not be
31
+ # stored in the UserManager.
32
+ def self.users
33
+ @user_manager ||= UserManager.new
34
+ end
35
+
36
+ class UserManager
37
+
38
+ attr_reader :users
39
+
40
+ def initialize
41
+ @users = {}
42
+ end
43
+
44
+ def [](identifier)
45
+ unless user = (@users[identifier.to_s] || find_remote_user(identifier.to_s))
46
+ user = MissingConnection.new(identifier.to_s)
47
+ end
48
+ user
49
+ end
50
+
51
+ def []=(identifier, connection)
52
+ @users[identifier.to_s] ||= LocalConnection.new
53
+ @users[identifier.to_s] << connection
54
+ Synchronization.register_user(connection) if WebsocketRails.synchronize?
55
+ end
56
+
57
+ def delete(connection)
58
+ identifier = connection.user_identifier.to_s
59
+
60
+ if (@users.has_key?(identifier) && @users[identifier].connections.count > 1)
61
+ @users[identifier].delete(connection)
62
+ else
63
+ @users.delete(identifier)
64
+ Synchronization.destroy_user(identifier) if WebsocketRails.synchronize?
65
+ end
66
+ end
67
+
68
+ # Behaves similarly to Ruby's Array#each, yielding each connection
69
+ # object stored in the {UserManager}. If synchronization is enabled,
70
+ # each connection from every active worker will be yielded.
71
+ #
72
+ # You can access the `current_user` object through the #user method.
73
+ #
74
+ # You can trigger an event on this user using the #send_message method
75
+ # which behaves identically to BaseController#send_message.
76
+ #
77
+ # If Synchronization is enabled, the state of the `current_user` object
78
+ # will be equivalent to it's state at the time the connection was opened.
79
+ # It will not reflect changes made after the connection has been opened.
80
+ def each(&block)
81
+ if WebsocketRails.synchronize?
82
+ users_hash = Synchronization.all_users || return
83
+ users_hash.each do |identifier, user_json|
84
+ connection = remote_connection_from_json(identifier, user_json)
85
+ block.call(connection) if block
86
+ end
87
+ else
88
+ users.each do |_, connection|
89
+ block.call(connection) if block
90
+ end
91
+ end
92
+ end
93
+
94
+ # Behaves similarly to Ruby's Array#map, invoking the given block with
95
+ # each active connection object and returning a new array with the results.
96
+ #
97
+ # See UserManager#each for details on the current usage and limitations.
98
+ def map(&block)
99
+ collection = []
100
+
101
+ each do |connection|
102
+ collection << block.call(connection) if block
103
+ end
104
+
105
+ collection
106
+ end
107
+
108
+ private
109
+
110
+ def find_remote_user(identifier)
111
+ return unless WebsocketRails.synchronize?
112
+ user_hash = Synchronization.find_user(identifier) || return
113
+
114
+ remote_connection identifier, user_hash
115
+ end
116
+
117
+ def remote_connection_from_json(identifier, user_json)
118
+ user_hash = JSON.parse(user_json)
119
+ remote_connection identifier, user_hash
120
+ end
121
+
122
+ def remote_connection(identifier, user_hash)
123
+ RemoteConnection.new identifier, user_hash
124
+ end
125
+
126
+ # The UserManager::LocalConnection Class serves as a proxy object
127
+ # for storing multiple connections that belong to the same
128
+ # user. It implements the same basic interface as a Connection.
129
+ # This allows you to work with the object as though it is a
130
+ # single connection, but still trigger the events on all
131
+ # active connections belonging to the user.
132
+ class LocalConnection
133
+
134
+ attr_reader :connections
135
+
136
+ def initialize
137
+ @connections = []
138
+ end
139
+
140
+ def <<(connection)
141
+ @connections << connection
142
+ end
143
+
144
+ def delete(connection)
145
+ @connections.delete(connection)
146
+ end
147
+
148
+ def connected?
149
+ true
150
+ end
151
+
152
+ def user_identifier
153
+ latest_connection.user_identifier
154
+ end
155
+
156
+ def user
157
+ latest_connection.user
158
+ end
159
+
160
+ def trigger(event)
161
+ connections.each do |connection|
162
+ connection.trigger event
163
+ end
164
+ end
165
+
166
+ def send_message(event_name, data = {}, options = {})
167
+ options.merge! :user_id => user_identifier
168
+ options[:data] = data
169
+
170
+ event = Event.new(event_name, options)
171
+
172
+ # Trigger the event on all active connections for this user.
173
+ connections.each do |connection|
174
+ connection.trigger event
175
+ end
176
+
177
+ # Still publish the event in case the user is connected to
178
+ # other workers as well.
179
+ Synchronization.publish event if WebsocketRails.synchronize?
180
+ true
181
+ end
182
+
183
+ private
184
+
185
+ def latest_connection
186
+ @connections.last
187
+ end
188
+
189
+ end
190
+
191
+ class RemoteConnection
192
+
193
+ attr_reader :user_identifier, :user
194
+
195
+ def initialize(identifier, user_hash)
196
+ @user_identifier = identifier.to_s
197
+ @user_hash = user_hash
198
+ end
199
+
200
+ def connected?
201
+ true
202
+ end
203
+
204
+ def user
205
+ @user ||= load_user
206
+ end
207
+
208
+ def send_message(event_name, data = {}, options = {})
209
+ options.merge! :user_id => @user_identifier
210
+ options[:data] = data
211
+
212
+ event = Event.new(event_name, options)
213
+
214
+ # If the user is connected to this worker, trigger the event
215
+ # immediately as the event will be ignored by the Synchronization
216
+ ## dispatcher since the server_token will match.
217
+ if connection = WebsocketRails.users.users[@user_identifier]
218
+ connection.trigger event
219
+ end
220
+
221
+ # Still publish the event in case the user is connected to
222
+ # other workers as well.
223
+ #
224
+ # No need to check for Synchronization being enabled here.
225
+ # If a RemoteConnection has been fetched, Synchronization
226
+ # must be enabled.
227
+ Synchronization.publish event
228
+ true
229
+ end
230
+
231
+ private
232
+
233
+ def load_user
234
+ user = WebsocketRails.config.user_class.new
235
+ set_user_attributes user, @user_hash
236
+ user
237
+ end
238
+
239
+ def set_user_attributes(user, attr)
240
+ attr.each do |k, v|
241
+ user.send "#{k}=", v
242
+ end
243
+ user.instance_variable_set(:@new_record, false)
244
+ user.instance_variable_set(:@destroyed, false)
245
+ end
246
+
247
+ end
248
+
249
+ class MissingConnection
250
+
251
+ attr_reader :identifier
252
+
253
+ def initialize(identifier)
254
+ @user_identifier = identifier.to_s
255
+ end
256
+
257
+ def connected?
258
+ false
259
+ end
260
+
261
+ def user
262
+ nil
263
+ end
264
+
265
+ def send_message(*args)
266
+ false
267
+ end
268
+
269
+ def nil?
270
+ true
271
+ end
272
+
273
+ end
274
+
275
+ end
276
+ end
@@ -0,0 +1,3 @@
1
+ module WebsocketRails
2
+ VERSION = "0.7.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,53 @@
1
+ class ChatController < WebsocketRails::BaseController
2
+
3
+ before_action do
4
+ if message_counter > 10
5
+ self.message_counter = 0
6
+ end
7
+ end
8
+
9
+ before_action :only => :new_message do
10
+ true
11
+ end
12
+
13
+ attr_accessor :message_counter
14
+
15
+ def initialize
16
+ # perform application setup here
17
+ @message_counter = 0
18
+ end
19
+
20
+ def client_connected
21
+ # do something when a client connects
22
+ end
23
+
24
+ def error_occurred
25
+ # do something when an error occurs
26
+ end
27
+
28
+ def new_message
29
+ @message_counter += 1
30
+ broadcast_message :new_message, message
31
+ end
32
+
33
+ def new_user
34
+ controller_store[:user] = message
35
+ broadcast_user_list
36
+ end
37
+
38
+ def change_username
39
+ controller_store[:user] = message
40
+ broadcast_user_list
41
+ end
42
+
43
+ def delete_user
44
+ controller_store[:user] = nil
45
+ broadcast_user_list
46
+ end
47
+
48
+ def broadcast_user_list
49
+ users = ['user']
50
+ broadcast_message :user_list, users
51
+ end
52
+
53
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ require "action_mailer/railtie"
8
+
9
+ Bundler.require
10
+ require "websocket-rails"
11
+
12
+ module Dummy
13
+ class Application < Rails::Application
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+
18
+ # Custom directories with classes and modules you want to be autoloadable.
19
+ # config.autoload_paths += %W(#{config.root}/extras)
20
+
21
+ # Only load the plugins named here, in the order given (default is alphabetical).
22
+ # :all can be used as a placeholder for all plugins not explicitly named.
23
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24
+
25
+ # Activate observers that should always be running.
26
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
27
+
28
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
30
+ # config.time_zone = 'Central Time (US & Canada)'
31
+
32
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
33
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
34
+ # config.i18n.default_locale = :de
35
+
36
+ # JavaScript files you want as :defaults (application.js is always included).
37
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
38
+
39
+ # Configure the default encoding used in templates for Ruby 1.9.
40
+ config.encoding = "utf-8"
41
+
42
+ # Configure sensitive parameters which will be filtered from the log file.
43
+ config.filter_parameters += [:password]
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000