websocket-rails 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +14 -0
- data/README.md +5 -0
- data/lib/websocket-rails.rb +4 -4
- data/lib/websocket_rails/base_controller.rb +12 -8
- data/lib/websocket_rails/connection_adapters.rb +1 -1
- data/lib/websocket_rails/dispatcher.rb +1 -3
- data/lib/websocket_rails/logging.rb +12 -1
- data/lib/websocket_rails/synchronization.rb +1 -0
- data/lib/websocket_rails/version.rb +1 -1
- data/spec/dummy/log/test.log +24 -0
- data/spec/javascripts/generated/assets/upload_event.js +56 -0
- data/spec/javascripts/generated/specs/upload_event_spec.js +55 -0
- data/spec/unit/logging_spec.rb +18 -0
- data/spec/unit/synchronization_spec.rb +14 -0
- metadata +7 -5
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,21 @@
|
|
1
1
|
# WebsocketRails Change Log
|
2
2
|
|
3
|
+
## Version 0.4.7
|
4
|
+
|
5
|
+
June 6 2013
|
6
|
+
|
7
|
+
* Fix observer system - Thanks to @pitr
|
8
|
+
* Fix spelling mistake in ConnectionAdapters#inspect - Thanks to
|
9
|
+
@bmxpert1
|
10
|
+
* Prevent duplicate events from being triggered when events are added
|
11
|
+
directly to Redis from an outside process. - Thanks to @DarkSwoop
|
12
|
+
* Only log event data if it is a Hash or String to drastically reduce
|
13
|
+
the log file size. - Thanks to @florianguenther
|
14
|
+
|
3
15
|
## Version 0.4.6
|
4
16
|
|
17
|
+
May 9 2013
|
18
|
+
|
5
19
|
* Manually load the Faye::WebSocket Thin adapter to support the latest
|
6
20
|
version of the Faye::WebSocket gem. - Thanks to @Traxmaxx
|
7
21
|
|
data/README.md
CHANGED
@@ -4,6 +4,11 @@
|
|
4
4
|
|
5
5
|
If you haven't done so yet, check out the [Project Page](http://danknox.github.com/websocket-rails/) to get a feel for the project direction. Feedback is very much appreciated. Post an issue on the issue tracker or [shoot us an email](mailto:support@threedotloft.com) to give us your thoughts.
|
6
6
|
|
7
|
+
**Find us on IRC #websocket-rails**
|
8
|
+
|
9
|
+
Stop by #websocket-rails if you would like to chat or have any
|
10
|
+
questions.
|
11
|
+
|
7
12
|
## Recent Updates
|
8
13
|
|
9
14
|
Check out the [CHANGELOG](https://github.com/DanKnox/websocket-rails/blob/master/CHANGELOG.md) to find out what's new.
|
data/lib/websocket-rails.rb
CHANGED
@@ -74,10 +74,10 @@ class WebsocketRails::EventRoutingError < StandardError
|
|
74
74
|
|
75
75
|
def to_json
|
76
76
|
super({
|
77
|
-
error
|
78
|
-
event
|
79
|
-
method
|
80
|
-
reason
|
77
|
+
:error => "EventRoutingError",
|
78
|
+
:event => event.name,
|
79
|
+
:method => method,
|
80
|
+
:reason => "Controller #{controller.class} does not respond to #{method}"
|
81
81
|
})
|
82
82
|
end
|
83
83
|
|
@@ -21,7 +21,7 @@ module WebsocketRails
|
|
21
21
|
# Tell Rails that BaseController and children can be reloaded when in
|
22
22
|
# the Development environment.
|
23
23
|
def self.inherited(controller)
|
24
|
-
unloadable controller
|
24
|
+
unloadable controller unless controller.name == "WebsocketRails::InternalController"
|
25
25
|
end
|
26
26
|
|
27
27
|
# Add observers to specific events or the controller in general. This functionality is similar
|
@@ -41,16 +41,16 @@ module WebsocketRails
|
|
41
41
|
# puts 'new_message has fired!'
|
42
42
|
# }
|
43
43
|
def self.observe(event = nil, &block)
|
44
|
+
# Stores the observer Procs for the current controller. See {observe} for details.
|
45
|
+
@observers ||= Hash.new {|h,k| h[k] = Array.new}
|
46
|
+
|
44
47
|
if event
|
45
|
-
|
48
|
+
@observers[event] << block
|
46
49
|
else
|
47
|
-
|
50
|
+
@observers[:general] << block
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
51
|
-
# Stores the observer Procs for the current controller. See {observe} for details.
|
52
|
-
@@observers = Hash.new {|h,k| h[k] = Array.new}
|
53
|
-
|
54
54
|
# Provides direct access to the connection object for the client that
|
55
55
|
# initiated the event that is currently being executed.
|
56
56
|
def connection
|
@@ -154,10 +154,14 @@ module WebsocketRails
|
|
154
154
|
# first and event specific observers are executed last. Each will be executed in the order that
|
155
155
|
# they have been defined. This method is executed by the {Dispatcher}.
|
156
156
|
def execute_observers(event)
|
157
|
-
|
157
|
+
observers = self.class.instance_variable_get(:@observers)
|
158
|
+
|
159
|
+
return unless observers
|
160
|
+
|
161
|
+
observers[:general].each do |observer|
|
158
162
|
instance_eval( &observer )
|
159
163
|
end
|
160
|
-
|
164
|
+
observers[event].each do |observer|
|
161
165
|
instance_eval( &observer )
|
162
166
|
end
|
163
167
|
end
|
@@ -51,9 +51,7 @@ module WebsocketRails
|
|
51
51
|
log_event(event) do
|
52
52
|
controller = controller_factory.new_for_event(event, controller_class)
|
53
53
|
|
54
|
-
|
55
|
-
controller.send(:execute_observers, event.name)
|
56
|
-
end
|
54
|
+
controller.send(:execute_observers, event.name)
|
57
55
|
|
58
56
|
if controller.respond_to?(method)
|
59
57
|
controller.send(method)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support/core_ext/module/delegation'
|
2
|
+
require 'active_support/core_ext/hash'
|
2
3
|
require 'bigdecimal'
|
3
4
|
require 'bigdecimal/util'
|
4
5
|
|
@@ -6,6 +7,12 @@ module WebsocketRails
|
|
6
7
|
module Logging
|
7
8
|
# Logging module heavily influenced by Travis-Support library
|
8
9
|
|
10
|
+
LOGGABLE_DATA = [
|
11
|
+
String,
|
12
|
+
Hash,
|
13
|
+
ActiveSupport::HashWithIndifferentAccess
|
14
|
+
]
|
15
|
+
|
9
16
|
ANSI = {
|
10
17
|
:red => 31,
|
11
18
|
:green => 32,
|
@@ -45,7 +52,7 @@ module WebsocketRails
|
|
45
52
|
def log_event_start(event)
|
46
53
|
message = "Started Event: #{event.encoded_name}\n"
|
47
54
|
message << "#{colorize(:cyan, "Name:")} #{event.encoded_name}\n"
|
48
|
-
message << "#{colorize(:cyan, "Data:")} #{event.data.inspect}\n"
|
55
|
+
message << "#{colorize(:cyan, "Data:")} #{event.data.inspect}\n" if log_data?(event)
|
49
56
|
message << "#{colorize(:cyan, "Connection:")} #{event.connection}\n\n"
|
50
57
|
info message
|
51
58
|
end
|
@@ -73,6 +80,10 @@ module WebsocketRails
|
|
73
80
|
end
|
74
81
|
end
|
75
82
|
|
83
|
+
def log_data?(event)
|
84
|
+
LOGGABLE_DATA.include?(event.data.class)
|
85
|
+
end
|
86
|
+
|
76
87
|
def log_exception(exception)
|
77
88
|
logger.error(wrap(:error, self, "#{exception.class.name}: #{exception.message}"))
|
78
89
|
exception.backtrace.each { |line| logger.error(wrap(:error, self, line)) } if exception.backtrace
|
data/spec/dummy/log/test.log
CHANGED
@@ -129,3 +129,27 @@ Connecting to database specified by database.yml
|
|
129
129
|
Connecting to database specified by database.yml
|
130
130
|
Connecting to database specified by database.yml
|
131
131
|
Connecting to database specified by database.yml
|
132
|
+
Connecting to database specified by database.yml
|
133
|
+
Connecting to database specified by database.yml
|
134
|
+
Connecting to database specified by database.yml
|
135
|
+
Connecting to database specified by database.yml
|
136
|
+
Connecting to database specified by database.yml
|
137
|
+
Connecting to database specified by database.yml
|
138
|
+
Connecting to database specified by database.yml
|
139
|
+
Connecting to database specified by database.yml
|
140
|
+
Connecting to database specified by database.yml
|
141
|
+
Connecting to database specified by database.yml
|
142
|
+
Connecting to database specified by database.yml
|
143
|
+
Connecting to database specified by database.yml
|
144
|
+
Connecting to database specified by database.yml
|
145
|
+
Connecting to database specified by database.yml
|
146
|
+
Connecting to database specified by database.yml
|
147
|
+
Connecting to database specified by database.yml
|
148
|
+
Connecting to database specified by database.yml
|
149
|
+
Connecting to database specified by database.yml
|
150
|
+
Connecting to database specified by database.yml
|
151
|
+
Connecting to database specified by database.yml
|
152
|
+
Connecting to database specified by database.yml
|
153
|
+
Connecting to database specified by database.yml
|
154
|
+
Connecting to database specified by database.yml
|
155
|
+
Connecting to database specified by database.yml
|
@@ -0,0 +1,56 @@
|
|
1
|
+
(function() {
|
2
|
+
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
3
|
+
__hasProp = {}.hasOwnProperty,
|
4
|
+
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
5
|
+
|
6
|
+
WebSocketRails.UploadEvent = (function(_super) {
|
7
|
+
|
8
|
+
__extends(UploadEvent, _super);
|
9
|
+
|
10
|
+
function UploadEvent(eventName, file, data, connectionId, successCallback, failureCallback, _conn) {
|
11
|
+
this.file = file;
|
12
|
+
this.successCallback = successCallback;
|
13
|
+
this.failureCallback = failureCallback;
|
14
|
+
this._conn = _conn;
|
15
|
+
this.storeBinaryData = __bind(this.storeBinaryData, this);
|
16
|
+
|
17
|
+
this.attributes = __bind(this.attributes, this);
|
18
|
+
|
19
|
+
UploadEvent.__super__.constructor.call(this, [eventName, data, connectionId], this.successCallback, this.failureCallback);
|
20
|
+
this.fileReader = new FileReader;
|
21
|
+
this.fileReader.onloadend = this.storeBinaryData;
|
22
|
+
this.fileReader.readAsArrayBuffer(this.file);
|
23
|
+
}
|
24
|
+
|
25
|
+
UploadEvent.prototype.isFileUpload = function() {
|
26
|
+
return true;
|
27
|
+
};
|
28
|
+
|
29
|
+
UploadEvent.prototype.attributes = function() {
|
30
|
+
return {
|
31
|
+
id: this.id,
|
32
|
+
channel: this.channel,
|
33
|
+
data: this.data,
|
34
|
+
upload_event: true,
|
35
|
+
raw_file_data: {
|
36
|
+
filename: this.file.name,
|
37
|
+
file_size: this.file.size,
|
38
|
+
type: this.file.type,
|
39
|
+
array_buffer: this.bufferView
|
40
|
+
}
|
41
|
+
};
|
42
|
+
};
|
43
|
+
|
44
|
+
UploadEvent.prototype.storeBinaryData = function(event) {
|
45
|
+
this.arrayBuffer = event.target.result;
|
46
|
+
this.bufferView = new Uint8Array(this.arrayBuffer);
|
47
|
+
window.buffer = this.bufferView;
|
48
|
+
console.log(this.arrayBuffer);
|
49
|
+
return this._conn.trigger(this);
|
50
|
+
};
|
51
|
+
|
52
|
+
return UploadEvent;
|
53
|
+
|
54
|
+
})(WebSocketRails.Event);
|
55
|
+
|
56
|
+
}).call(this);
|
@@ -0,0 +1,55 @@
|
|
1
|
+
(function() {
|
2
|
+
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
3
|
+
|
4
|
+
window.FileReader = (function() {
|
5
|
+
|
6
|
+
function FileReader() {
|
7
|
+
this.readAsArrayBuffer = __bind(this.readAsArrayBuffer, this);
|
8
|
+
|
9
|
+
}
|
10
|
+
|
11
|
+
FileReader.prototype.readAsArrayBuffer = function(file) {
|
12
|
+
return this.onloadend({
|
13
|
+
target: {
|
14
|
+
result: 'xyz'
|
15
|
+
}
|
16
|
+
});
|
17
|
+
};
|
18
|
+
|
19
|
+
return FileReader;
|
20
|
+
|
21
|
+
})();
|
22
|
+
|
23
|
+
describe('WebSocketRails.UploadEvent', function() {
|
24
|
+
beforeEach(function() {
|
25
|
+
this.file = {
|
26
|
+
name: 'test_file.jpg',
|
27
|
+
size: 4321,
|
28
|
+
type: 'image/jpeg'
|
29
|
+
};
|
30
|
+
this.conn = {
|
31
|
+
trigger: function(event) {
|
32
|
+
return this.called = true;
|
33
|
+
}
|
34
|
+
};
|
35
|
+
return this.event = new WebSocketRails.UploadEvent('test_upload', this.file, {}, 12345, {}, {}, this.conn);
|
36
|
+
});
|
37
|
+
it('should call the triggerCallback function after extracting the binary data', function() {
|
38
|
+
return expect(this.conn.called).toEqual(true);
|
39
|
+
});
|
40
|
+
describe('.isFileUpload()', function() {
|
41
|
+
return it('should be true', function() {
|
42
|
+
return expect(this.event.isFileUpload()).toEqual(true);
|
43
|
+
});
|
44
|
+
});
|
45
|
+
return describe('.attributes()', function() {
|
46
|
+
return it('should include the raw_file_data', function() {
|
47
|
+
expect(this.event.attributes().raw_file_data.filename).toEqual('test_file.jpg');
|
48
|
+
expect(this.event.attributes().raw_file_data.file_size).toEqual(4321);
|
49
|
+
expect(this.event.attributes().raw_file_data.type).toEqual('image/jpeg');
|
50
|
+
return expect(this.event.attributes().raw_file_data.array_buffer).toEqual('xyz');
|
51
|
+
});
|
52
|
+
});
|
53
|
+
});
|
54
|
+
|
55
|
+
}).call(this);
|
data/spec/unit/logging_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require "ostruct"
|
2
3
|
|
3
4
|
module WebsocketRails
|
4
5
|
describe Logging do
|
@@ -97,6 +98,23 @@ module WebsocketRails
|
|
97
98
|
end
|
98
99
|
end
|
99
100
|
|
101
|
+
describe "#log_data?" do
|
102
|
+
before do
|
103
|
+
@hash_event = Event.new(:log_test, :data => {test: true})
|
104
|
+
@string_event = Event.new(:log_test, :data => "message")
|
105
|
+
@object_event = Event.new(:log_test, :data => OpenStruct.new)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "returns true if data is an allowed type" do
|
109
|
+
object.log_data?(@hash_event).should == true
|
110
|
+
object.log_data?(@string_event).should == true
|
111
|
+
end
|
112
|
+
|
113
|
+
it "returns false if the data is not an allows type" do
|
114
|
+
object.log_data?(@object_event).should == false
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
100
118
|
describe "#log_event?" do
|
101
119
|
context "with an internal event" do
|
102
120
|
before do
|
@@ -47,6 +47,20 @@ module WebsocketRails
|
|
47
47
|
# redis = @redis
|
48
48
|
# EM.next_tick { redis.publish "websocket_rails.events", event.serialize }
|
49
49
|
#end
|
50
|
+
it "should set server_token to stop circular publishing" do
|
51
|
+
event = Event.new(:redis_event, :channel => 'synchrony', :data => 'hello from another process')
|
52
|
+
if event.server_token.nil?
|
53
|
+
event.server_token = subject.server_token
|
54
|
+
end
|
55
|
+
event.server_token.should == subject.server_token
|
56
|
+
end
|
57
|
+
it "should not set server_token if it is present" do
|
58
|
+
event = Event.new(:redis_event, :channel => 'synchrony', :data => 'hello from another process', :server_token => '1234')
|
59
|
+
if event.server_token.nil?
|
60
|
+
event.server_token = subject.server_token
|
61
|
+
end
|
62
|
+
event.server_token.should == '1234'
|
63
|
+
end
|
50
64
|
end
|
51
65
|
|
52
66
|
describe "#generate_unique_token" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: websocket-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-06-06 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -259,10 +259,12 @@ files:
|
|
259
259
|
- spec/javascripts/generated/assets/channel.js
|
260
260
|
- spec/javascripts/generated/assets/event.js
|
261
261
|
- spec/javascripts/generated/assets/http_connection.js
|
262
|
+
- spec/javascripts/generated/assets/upload_event.js
|
262
263
|
- spec/javascripts/generated/assets/websocket_connection.js
|
263
264
|
- spec/javascripts/generated/assets/websocket_rails.js
|
264
265
|
- spec/javascripts/generated/specs/channel_spec.js
|
265
266
|
- spec/javascripts/generated/specs/event_spec.js
|
267
|
+
- spec/javascripts/generated/specs/upload_event_spec.js
|
266
268
|
- spec/javascripts/generated/specs/websocket_connection_spec.js
|
267
269
|
- spec/javascripts/generated/specs/websocket_rails_spec.js
|
268
270
|
- spec/javascripts/support/jasmine.yml
|
@@ -300,7 +302,7 @@ files:
|
|
300
302
|
- CHANGELOG.md
|
301
303
|
homepage: http://danknox.github.com/websocket-rails/
|
302
304
|
licenses: []
|
303
|
-
post_install_message: ! " Welcome to WebsocketRails v0.4.
|
305
|
+
post_install_message: ! " Welcome to WebsocketRails v0.4.7!\n\n There have been
|
304
306
|
a few significant changes in the public\n API, so if you are upgrading please be
|
305
307
|
sure to read the\n CHANGELOG located at:\n\n http://github.com/DanKnox/websocket-rails/CHANGELOG.md\n"
|
306
308
|
rdoc_options: []
|
@@ -314,7 +316,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
314
316
|
version: '0'
|
315
317
|
segments:
|
316
318
|
- 0
|
317
|
-
hash:
|
319
|
+
hash: -1865655959983006042
|
318
320
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
319
321
|
none: false
|
320
322
|
requirements:
|
@@ -323,7 +325,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
323
325
|
version: '0'
|
324
326
|
segments:
|
325
327
|
- 0
|
326
|
-
hash:
|
328
|
+
hash: -1865655959983006042
|
327
329
|
requirements: []
|
328
330
|
rubyforge_project: websocket-rails
|
329
331
|
rubygems_version: 1.8.25
|