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.
Files changed (37) hide show
  1. data/CHANGELOG.md +11 -0
  2. data/Gemfile +7 -0
  3. data/README.md +15 -16
  4. data/lib/assets/javascripts/websocket_rails/channel.js +35 -0
  5. data/lib/assets/javascripts/websocket_rails/http_connection.js +68 -0
  6. data/lib/assets/javascripts/websocket_rails/main.js +4 -0
  7. data/lib/assets/javascripts/websocket_rails/websocket_connection.js +38 -0
  8. data/lib/assets/javascripts/websocket_rails/websocket_rails.js +100 -0
  9. data/lib/generators/websocket_rails/install/install_generator.rb +32 -0
  10. data/lib/generators/websocket_rails/install/templates/events.rb +14 -0
  11. data/lib/websocket-rails.rb +4 -0
  12. data/lib/websocket_rails/base_controller.rb +4 -2
  13. data/lib/websocket_rails/channel.rb +35 -0
  14. data/lib/websocket_rails/channel_manager.rb +28 -0
  15. data/lib/websocket_rails/connection_adapters.rb +35 -4
  16. data/lib/websocket_rails/connection_manager.rb +3 -1
  17. data/lib/websocket_rails/dispatcher.rb +22 -12
  18. data/lib/websocket_rails/event.rb +31 -17
  19. data/lib/websocket_rails/event_map.rb +14 -6
  20. data/lib/websocket_rails/event_queue.rb +27 -0
  21. data/lib/websocket_rails/internal_events.rb +19 -0
  22. data/lib/websocket_rails/version.rb +1 -1
  23. data/spec/dummy/config/initializers/events.rb +5 -5
  24. data/spec/dummy/log/test.log +180 -0
  25. data/spec/integration/connection_manager_spec.rb +21 -8
  26. data/spec/support/helper_methods.rb +2 -2
  27. data/spec/support/mock_web_socket.rb +4 -0
  28. data/spec/unit/channel_manager_spec.rb +29 -0
  29. data/spec/unit/channel_spec.rb +46 -0
  30. data/spec/unit/connection_adapters_spec.rb +20 -0
  31. data/spec/unit/dispatcher_spec.rb +13 -2
  32. data/spec/unit/event_map_spec.rb +2 -2
  33. data/spec/unit/event_queue_spec.rb +36 -0
  34. data/spec/unit/event_spec.rb +26 -3
  35. metadata +24 -28
  36. data/assets/javascripts/http_dispatcher.js +0 -78
  37. 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
- }