websocket-rails 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # WebsocketRails Change Log
2
2
 
3
+ ## Version 0.4.9
4
+
5
+ July 9 2013
6
+
7
+ * Updated JavaScript client to properly keep track of the connection state.
8
+ * Added .connection_stale() function to the JavaScript client for easily checking connection state.
9
+
3
10
  ## Version 0.4.8
4
11
 
5
12
  July 6 2013
@@ -26,6 +26,7 @@ class WebSocketRails.HttpConnection
26
26
  @last_pos = 0
27
27
  @message_queue = []
28
28
  @_conn.onreadystatechange = @parse_stream
29
+ @_conn.addEventListener("load", @connectionClosed, false)
29
30
  @_conn.open "GET", @_url, true
30
31
  @_conn.send()
31
32
 
@@ -59,3 +60,8 @@ class WebSocketRails.HttpConnection
59
60
  event.connection_id = @dispatcher.connection_id
60
61
  @trigger event
61
62
  @message_queue = []
63
+
64
+ connectionClosed: (event) =>
65
+ close_event = new WebSocketRails.Event(['connection_closed', event])
66
+ @dispatcher.state = 'disconnected'
67
+ @dispatcher.dispatch close_event
@@ -23,10 +23,12 @@ class WebSocketRails.WebSocketConnection
23
23
 
24
24
  on_close: (event) =>
25
25
  close_event = new WebSocketRails.Event(['connection_closed', event])
26
+ @dispatcher.state = 'disconnected'
26
27
  @dispatcher.dispatch close_event
27
28
 
28
29
  on_error: (event) =>
29
30
  error_event = new WebSocketRails.Event(['connection_error', event])
31
+ @dispatcher.state = 'disconnected'
30
32
  @dispatcher.dispatch error_event
31
33
 
32
34
  flush_queue: =>
@@ -16,7 +16,7 @@ Listening for new events from the server
16
16
  console.log(data.user_name);
17
17
  });
18
18
  ###
19
- class window.WebSocketRails
19
+ class @WebSocketRails
20
20
  constructor: (@url, @use_websockets = true) ->
21
21
  @state = 'connecting'
22
22
  @callbacks = {}
@@ -102,3 +102,6 @@ class window.WebSocketRails
102
102
  pong: =>
103
103
  pong = new WebSocketRails.Event( ['websocket_rails.pong',{},@connection_id] )
104
104
  @_conn.trigger pong
105
+
106
+ connection_stale: =>
107
+ @state != 'connected'
@@ -1,3 +1,3 @@
1
1
  module WebsocketRails
2
- VERSION = "0.4.8"
2
+ VERSION = "0.4.9"
3
3
  end
@@ -41,6 +41,7 @@
41
41
  function HttpConnection(url, dispatcher) {
42
42
  this.url = url;
43
43
  this.dispatcher = dispatcher;
44
+ this.connectionClosed = __bind(this.connectionClosed, this);
44
45
  this.flush_queue = __bind(this.flush_queue, this);
45
46
  this.trigger = __bind(this.trigger, this);
46
47
  this.parse_stream = __bind(this.parse_stream, this);
@@ -50,6 +51,7 @@
50
51
  this.last_pos = 0;
51
52
  this.message_queue = [];
52
53
  this._conn.onreadystatechange = this.parse_stream;
54
+ this._conn.addEventListener("load", this.connectionClosed, false);
53
55
  this._conn.open("GET", this._url, true);
54
56
  this._conn.send();
55
57
  }
@@ -97,6 +99,13 @@
97
99
  return this.message_queue = [];
98
100
  };
99
101
 
102
+ HttpConnection.prototype.connectionClosed = function(event) {
103
+ var close_event;
104
+ close_event = new WebSocketRails.Event(['connection_closed', event]);
105
+ this.dispatcher.state = 'disconnected';
106
+ return this.dispatcher.dispatch(close_event);
107
+ };
108
+
100
109
  return HttpConnection;
101
110
 
102
111
  })();
@@ -42,12 +42,14 @@ WebSocket Interface for the WebSocketRails client.
42
42
  WebSocketConnection.prototype.on_close = function(event) {
43
43
  var close_event;
44
44
  close_event = new WebSocketRails.Event(['connection_closed', event]);
45
+ this.dispatcher.state = 'disconnected';
45
46
  return this.dispatcher.dispatch(close_event);
46
47
  };
47
48
 
48
49
  WebSocketConnection.prototype.on_error = function(event) {
49
50
  var error_event;
50
51
  error_event = new WebSocketRails.Event(['connection_error', event]);
52
+ this.dispatcher.state = 'disconnected';
51
53
  return this.dispatcher.dispatch(error_event);
52
54
  };
53
55
 
@@ -21,10 +21,11 @@ Listening for new events from the server
21
21
  (function() {
22
22
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
23
23
 
24
- window.WebSocketRails = (function() {
24
+ this.WebSocketRails = (function() {
25
25
  function WebSocketRails(url, use_websockets) {
26
26
  this.url = url;
27
27
  this.use_websockets = use_websockets != null ? use_websockets : true;
28
+ this.connection_stale = __bind(this.connection_stale, this);
28
29
  this.pong = __bind(this.pong, this);
29
30
  this.supports_websockets = __bind(this.supports_websockets, this);
30
31
  this.dispatch_channel = __bind(this.dispatch_channel, this);
@@ -169,6 +170,10 @@ Listening for new events from the server
169
170
  return this._conn.trigger(pong);
170
171
  };
171
172
 
173
+ WebSocketRails.prototype.connection_stale = function() {
174
+ return this.state !== 'connected';
175
+ };
176
+
172
177
  return WebSocketRails;
173
178
 
174
179
  })();
@@ -81,21 +81,27 @@
81
81
  });
82
82
  });
83
83
  describe('.on_close', function() {
84
- return it('should dispatch the connection_closed event and pass the original event', function() {
84
+ it('should dispatch the connection_closed event and pass the original event', function() {
85
85
  var close_event, dispatcher, event, lastCall;
86
86
  event = new WebSocketRails.Event(['event', 'message']);
87
87
  close_event = new WebSocketRails.Event(['connection_closed', event]);
88
88
  sinon.spy(this.dispatcher, 'dispatch');
89
- this.connection.on_close(event);
89
+ this.connection.on_close(close_event);
90
90
  dispatcher = this.dispatcher.dispatch;
91
91
  lastCall = dispatcher.lastCall.args[0];
92
92
  expect(dispatcher.calledOnce).toBe(true);
93
93
  expect(lastCall.data).toEqual(event.data);
94
94
  return dispatcher.restore();
95
95
  });
96
+ return it('sets the connection state on the dispatcher to disconnected', function() {
97
+ var close_event;
98
+ close_event = new WebSocketRails.Event(['connection_closed', {}]);
99
+ this.connection.on_close(close_event);
100
+ return expect(this.dispatcher.state).toEqual('disconnected');
101
+ });
96
102
  });
97
103
  describe('.on_error', function() {
98
- return it('should dispatch the connection_error event and pass the original event', function() {
104
+ it('should dispatch the connection_error event and pass the original event', function() {
99
105
  var dispatcher, error_event, event, lastCall;
100
106
  event = new WebSocketRails.Event(['event', 'message']);
101
107
  error_event = new WebSocketRails.Event(['connection_error', event]);
@@ -107,6 +113,12 @@
107
113
  expect(lastCall.data).toEqual(event.data);
108
114
  return dispatcher.restore();
109
115
  });
116
+ return it('sets the connection state on the dispatcher to disconnected', function() {
117
+ var close_event;
118
+ close_event = new WebSocketRails.Event(['connection_closed', {}]);
119
+ this.connection.on_error(close_event);
120
+ return expect(this.dispatcher.state).toEqual('disconnected');
121
+ });
110
122
  });
111
123
  return describe('.flush_queue', function() {
112
124
  beforeEach(function() {
@@ -178,6 +178,20 @@
178
178
  });
179
179
  });
180
180
  });
181
+ describe('.connection_stale', function() {
182
+ describe('when state is connected', function() {
183
+ return it('should return false', function() {
184
+ this.dispatcher.state = 'connected';
185
+ return expect(this.dispatcher.connection_stale()).toEqual(false);
186
+ });
187
+ });
188
+ return describe('when state is disconnected', function() {
189
+ return it('should return true', function() {
190
+ this.dispatcher.state = 'disconnected';
191
+ return expect(this.dispatcher.connection_stale()).toEqual(true);
192
+ });
193
+ });
194
+ });
181
195
  return describe('working with channels', function() {
182
196
  beforeEach(function() {
183
197
  return WebSocketRails.Channel = function(name, dispatcher, is_private) {
@@ -9,10 +9,10 @@ describe 'WebsocketRails.WebSocketConnection:', ->
9
9
  @url = url
10
10
  @send = -> true
11
11
  @dispatcher = dispatcher
12
- @connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket',dispatcher)
12
+ @connection = new WebSocketRails.WebSocketConnection('localhost:3000/websocket', dispatcher)
13
13
 
14
14
  describe 'constructor', ->
15
-
15
+
16
16
  it 'should set the onmessage event on the WebSocket object to this.on_message', ->
17
17
  expect(@connection._conn.onmessage).toEqual @connection.on_message
18
18
 
@@ -61,19 +61,24 @@ describe 'WebsocketRails.WebSocketConnection:', ->
61
61
 
62
62
  describe '.on_close', ->
63
63
  it 'should dispatch the connection_closed event and pass the original event', ->
64
-
65
64
  event = new WebSocketRails.Event ['event','message']
66
65
  close_event = new WebSocketRails.Event(['connection_closed', event ])
67
66
  sinon.spy @dispatcher, 'dispatch'
68
- @connection.on_close event
67
+ @connection.on_close close_event
69
68
 
70
69
  dispatcher = @dispatcher.dispatch
71
70
  lastCall = dispatcher.lastCall.args[0]
72
71
  expect(dispatcher.calledOnce).toBe(true)
73
72
  expect(lastCall.data).toEqual event.data
74
-
73
+
75
74
  dispatcher.restore()
76
75
 
76
+ it 'sets the connection state on the dispatcher to disconnected', ->
77
+ close_event = new WebSocketRails.Event(['connection_closed', {} ])
78
+ @connection.on_close close_event
79
+
80
+ expect(@dispatcher.state).toEqual('disconnected')
81
+
77
82
  describe '.on_error', ->
78
83
  it 'should dispatch the connection_error event and pass the original event', ->
79
84
 
@@ -86,16 +91,22 @@ describe 'WebsocketRails.WebSocketConnection:', ->
86
91
  lastCall = dispatcher.lastCall.args[0]
87
92
  expect(dispatcher.calledOnce).toBe(true)
88
93
  expect(lastCall.data).toEqual event.data
89
-
94
+
90
95
  dispatcher.restore()
91
96
 
97
+ it 'sets the connection state on the dispatcher to disconnected', ->
98
+ close_event = new WebSocketRails.Event(['connection_closed', {} ])
99
+ @connection.on_error close_event
100
+
101
+ expect(@dispatcher.state).toEqual('disconnected')
102
+
92
103
  describe '.flush_queue', ->
93
104
  beforeEach ->
94
105
  @event = new WebSocketRails.Event ['event','message']
95
106
  @connection.message_queue.push @event
96
107
  @connection._conn =
97
108
  send: -> true
98
-
109
+
99
110
  it 'should send out all of the messages in the queue', ->
100
111
  mock_connection = sinon.mock @connection._conn
101
112
  mock_connection.expects('send').once().withArgs @event.serialize()
@@ -134,6 +134,17 @@ describe 'WebSocketRails:', ->
134
134
  event = new WebSocketRails.Event ['websocket_rails.subscribe', {channel: 'awesome'}, 123]
135
135
  expect(con_trigger.called).toEqual true
136
136
 
137
+ describe '.connection_stale', ->
138
+ describe 'when state is connected', ->
139
+ it 'should return false', ->
140
+ @dispatcher.state = 'connected'
141
+ expect(@dispatcher.connection_stale()).toEqual false
142
+
143
+ describe 'when state is disconnected', ->
144
+ it 'should return true', ->
145
+ @dispatcher.state = 'disconnected'
146
+ expect(@dispatcher.connection_stale()).toEqual true
147
+
137
148
  describe 'working with channels', ->
138
149
  beforeEach ->
139
150
  WebSocketRails.Channel = (@name,@dispatcher,@is_private) ->
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.8
4
+ version: 0.4.9
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-07-06 00:00:00.000000000 Z
14
+ date: 2013-07-11 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -301,7 +301,7 @@ files:
301
301
  - CHANGELOG.md
302
302
  homepage: http://danknox.github.com/websocket-rails/
303
303
  licenses: []
304
- post_install_message: Welcome to WebsocketRails v0.4.8!
304
+ post_install_message: Welcome to WebsocketRails v0.4.9!
305
305
  rdoc_options: []
306
306
  require_paths:
307
307
  - lib
@@ -313,7 +313,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
313
313
  version: '0'
314
314
  segments:
315
315
  - 0
316
- hash: 1588461271210117573
316
+ hash: -2441396343706207267
317
317
  required_rubygems_version: !ruby/object:Gem::Requirement
318
318
  none: false
319
319
  requirements:
@@ -322,7 +322,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
322
322
  version: '0'
323
323
  segments:
324
324
  - 0
325
- hash: 1588461271210117573
325
+ hash: -2441396343706207267
326
326
  requirements: []
327
327
  rubyforge_project: websocket-rails
328
328
  rubygems_version: 1.8.25