websocket-rails 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +11 -0
- data/Gemfile +7 -0
- data/README.md +15 -16
- data/lib/assets/javascripts/websocket_rails/channel.js +35 -0
- data/lib/assets/javascripts/websocket_rails/http_connection.js +68 -0
- data/lib/assets/javascripts/websocket_rails/main.js +4 -0
- data/lib/assets/javascripts/websocket_rails/websocket_connection.js +38 -0
- data/lib/assets/javascripts/websocket_rails/websocket_rails.js +100 -0
- data/lib/generators/websocket_rails/install/install_generator.rb +32 -0
- data/lib/generators/websocket_rails/install/templates/events.rb +14 -0
- data/lib/websocket-rails.rb +4 -0
- data/lib/websocket_rails/base_controller.rb +4 -2
- data/lib/websocket_rails/channel.rb +35 -0
- data/lib/websocket_rails/channel_manager.rb +28 -0
- data/lib/websocket_rails/connection_adapters.rb +35 -4
- data/lib/websocket_rails/connection_manager.rb +3 -1
- data/lib/websocket_rails/dispatcher.rb +22 -12
- data/lib/websocket_rails/event.rb +31 -17
- data/lib/websocket_rails/event_map.rb +14 -6
- data/lib/websocket_rails/event_queue.rb +27 -0
- data/lib/websocket_rails/internal_events.rb +19 -0
- data/lib/websocket_rails/version.rb +1 -1
- data/spec/dummy/config/initializers/events.rb +5 -5
- data/spec/dummy/log/test.log +180 -0
- data/spec/integration/connection_manager_spec.rb +21 -8
- data/spec/support/helper_methods.rb +2 -2
- data/spec/support/mock_web_socket.rb +4 -0
- data/spec/unit/channel_manager_spec.rb +29 -0
- data/spec/unit/channel_spec.rb +46 -0
- data/spec/unit/connection_adapters_spec.rb +20 -0
- data/spec/unit/dispatcher_spec.rb +13 -2
- data/spec/unit/event_map_spec.rb +2 -2
- data/spec/unit/event_queue_spec.rb +36 -0
- data/spec/unit/event_spec.rb +26 -3
- metadata +24 -28
- data/assets/javascripts/http_dispatcher.js +0 -78
- data/assets/javascripts/websocket_dispatcher.js +0 -66
@@ -1,78 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* Example HTTP event dispatcher.
|
3
|
-
*
|
4
|
-
* Setting up the dispatcher:
|
5
|
-
* var dispatcher = new ServerEventsDispatcher()
|
6
|
-
* dispatcher.onopen(function() {
|
7
|
-
* // trigger a server event immediately after opening connection
|
8
|
-
* dispatcher.trigger('new_user',{user_name: 'guest'})
|
9
|
-
* })
|
10
|
-
*
|
11
|
-
* Triggering a new event on the server
|
12
|
-
* dispatcher.trigger('event_name',object_to_be_serialized_to_json)
|
13
|
-
*
|
14
|
-
* Listening for new events from the server
|
15
|
-
* dispatcher.bind('event_name', function(data) {
|
16
|
-
* alert(data.user_name)
|
17
|
-
* })
|
18
|
-
*/
|
19
|
-
var ServerEventsDispatcher = function(){
|
20
|
-
var conn = new XMLHttpRequest(),
|
21
|
-
open_handler = function(){},
|
22
|
-
loaded = false,
|
23
|
-
lastPos = 0,
|
24
|
-
client_id = 0;
|
25
|
-
|
26
|
-
conn.onreadystatechange = function() {
|
27
|
-
if (conn.readyState == 3) {
|
28
|
-
var data = conn.responseText.substring(lastPos);
|
29
|
-
lastPos = conn.responseText.length;
|
30
|
-
var json_data = JSON.parse(data),
|
31
|
-
event_name = json_data[0],
|
32
|
-
message = json_data[1];
|
33
|
-
|
34
|
-
if (client_id == 0 && event_name == 'client_connected') {
|
35
|
-
client_id = message.connection_id
|
36
|
-
}
|
37
|
-
|
38
|
-
if (loaded == false) {
|
39
|
-
open_handler();
|
40
|
-
loaded = true
|
41
|
-
}
|
42
|
-
console.log(json_data)
|
43
|
-
dispatch(event_name, message)
|
44
|
-
}
|
45
|
-
}
|
46
|
-
conn.open("GET","/websocket",true)
|
47
|
-
conn.send()
|
48
|
-
|
49
|
-
var callbacks = {}
|
50
|
-
|
51
|
-
this.bind = function(event_name, callback) {
|
52
|
-
callbacks[event_name] = callbacks[event_name] || [];
|
53
|
-
callbacks[event_name].push(callback)
|
54
|
-
}
|
55
|
-
|
56
|
-
this.trigger = function(event_name, data) {
|
57
|
-
var payload = JSON.stringify([event_name,data])
|
58
|
-
$.ajax({
|
59
|
-
type: 'POST',
|
60
|
-
url: '/websocket',
|
61
|
-
data: {client_id: client_id, data: payload},
|
62
|
-
success: function(){console.log('success');}
|
63
|
-
});
|
64
|
-
return this;
|
65
|
-
}
|
66
|
-
|
67
|
-
this.onopen = function(handler) {
|
68
|
-
open_handler = handler
|
69
|
-
}
|
70
|
-
|
71
|
-
var dispatch = function(event_name, message) {
|
72
|
-
var chain = callbacks[event_name]
|
73
|
-
if (typeof chain == 'undefined') return;
|
74
|
-
for(var i = 0; i < chain.length; i++) {
|
75
|
-
chain[i]( message )
|
76
|
-
}
|
77
|
-
}
|
78
|
-
}
|
@@ -1,66 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* Example WebSocket event dispatcher.
|
3
|
-
*
|
4
|
-
* Setting up the dispatcher:
|
5
|
-
* var dispatcher = new ServerEventsDispatcher()
|
6
|
-
* dispatcher.onopen(function() {
|
7
|
-
* // trigger a server event immediately after opening connection
|
8
|
-
* dispatcher.trigger('new_user',{user_name: 'guest'})
|
9
|
-
* })
|
10
|
-
*
|
11
|
-
* Triggering a new event on the server
|
12
|
-
* dispatcher.trigger('event_name',object_to_be_serialized_to_json)
|
13
|
-
*
|
14
|
-
* Listening for new events from the server
|
15
|
-
* dispatcher.bind('event_name', function(data) {
|
16
|
-
* alert(data.user_name)
|
17
|
-
* })
|
18
|
-
*/
|
19
|
-
var ServerEventsDispatcher = function(){
|
20
|
-
var conn = new WebSocket("ws://localhost:3000/websocket"),
|
21
|
-
open_handler = function(){},
|
22
|
-
callbacks = {},
|
23
|
-
client_id = '';
|
24
|
-
|
25
|
-
this.bind = function(event_name, callback) {
|
26
|
-
callbacks[event_name] = callbacks[event_name] || [];
|
27
|
-
callbacks[event_name].push(callback)
|
28
|
-
}
|
29
|
-
|
30
|
-
this.trigger = function(event_name, data) {
|
31
|
-
var payload = JSON.stringify([event_name,data])
|
32
|
-
conn.send( payload )
|
33
|
-
return this;
|
34
|
-
}
|
35
|
-
|
36
|
-
conn.onopen = function(evt) {
|
37
|
-
open_handler()
|
38
|
-
}
|
39
|
-
this.onopen = function(handler) {
|
40
|
-
open_handler = handler
|
41
|
-
}
|
42
|
-
|
43
|
-
conn.onmessage = function(evt) {
|
44
|
-
var data = JSON.parse(evt.data),
|
45
|
-
event_name = data[0],
|
46
|
-
message = data[1];
|
47
|
-
|
48
|
-
if (client_id === '' && event_name === 'client_connected') {
|
49
|
-
client_id = message.connection_id
|
50
|
-
}
|
51
|
-
console.log(data)
|
52
|
-
dispatch(event_name, message)
|
53
|
-
}
|
54
|
-
|
55
|
-
conn.onclose = function(evt) {
|
56
|
-
dispatch('connection_closed', '')
|
57
|
-
}
|
58
|
-
|
59
|
-
var dispatch = function(event_name, message) {
|
60
|
-
var chain = callbacks[event_name]
|
61
|
-
if (typeof chain == 'undefined') return;
|
62
|
-
for(var i = 0; i < chain.length; i++) {
|
63
|
-
chain[i]( message )
|
64
|
-
}
|
65
|
-
}
|
66
|
-
}
|