Skip to content

Commit f13a3e9

Browse files
committedMar 5, 2015
Merge pull request #25 from Jenselme/mockWebsocket
feat: can mock a websocket server for testing purpose.
2 parents 1aa1fd6 + 0716fab commit f13a3e9

8 files changed

+3579
-3509
lines changed
 

‎README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,25 @@ then | resolve:Function, reject:Function | Resolves when message has been
109109

110110
### Service: `$websocketBackend` (in module `ngWebSocketMock`)
111111

112-
Similar to [`httpBackend`](https://ng-click.com/$httpBackend) mock in AngularJS's `ngMock` module
112+
Similar to [`httpBackend`](https://ng-click.com/$httpBackend) mock in
113+
AngularJS's `ngMock` module. You can use `ngWebSocketMock` to mock a websocket
114+
server in order to test your applications:
115+
116+
```javascript
117+
var $websocketBackend;
118+
119+
beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock');
120+
121+
beforeEach(inject(function (_$websocketBackend_) {
122+
$websocketBackend = _$websocketBackend_;
123+
$websocketBackend.mock();
124+
125+
$websocketBackend.expectConnect('ws://localhost:8080/api');
126+
$websocketBackend.expectSend({data: JSON.stringify({test: true})});
127+
128+
$websocketBackend.expectConnect(playUrl);
129+
}));
130+
```
113131
114132
### Methods
115133
@@ -165,4 +183,3 @@ You can contact me either by: [submitting](https://github.com/gdi2290/angular-we
165183
166184
## License
167185
[MIT](https://github.com/gdi2290/angular-websocket/blob/master/LICENSE)
168-

‎angular-websocket.js

+5-27
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,6 @@
2727
return -1;
2828
};
2929
}
30-
// ie8 wat
31-
if (!Function.prototype.bind) {
32-
Function.prototype.bind = function(oThis) {
33-
if (typeof this !== 'function') {
34-
// closest thing possible to the ECMAScript 5
35-
// internal IsCallable function
36-
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
37-
}
38-
39-
var aArgs = arraySlice.call(arguments, 1),
40-
fToBind = this,
41-
FNOP = function() {},
42-
fBound = function() {
43-
return fToBind.apply(this instanceof FNOP && oThis ? this : oThis, aArgs.concat(arraySlice.call(arguments)));
44-
};
45-
46-
FNOP.prototype = this.prototype;
47-
fBound.prototype = new FNOP();
48-
49-
return fBound;
50-
};
51-
}
5230

5331
// $WebSocketProvider.$inject = ['$rootScope', '$q', '$timeout', '$websocketBackend'];
5432
function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
@@ -127,10 +105,10 @@
127105
$WebSocket.prototype._connect = function _connect(force) {
128106
if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
129107
this.socket = $websocketBackend.create(this.url, this.protocols);
130-
this.socket.onmessage = this._onMessageHandler.bind(this);
131-
this.socket.onopen = this._onOpenHandler.bind(this);
132-
this.socket.onerror = this._onErrorHandler.bind(this);
133-
this.socket.onclose = this._onCloseHandler.bind(this);
108+
this.socket.onmessage = angular.bind(this, this._onMessageHandler);
109+
this.socket.onopen = angular.bind(this, this._onOpenHandler);
110+
this.socket.onerror = angular.bind(this, this._onErrorHandler);
111+
this.socket.onclose = angular.bind(this, this._onCloseHandler);
134112
}
135113
};
136114

@@ -293,7 +271,7 @@
293271
$WebSocket.prototype.reconnect = function reconnect() {
294272
this.close();
295273

296-
$timeout(this._connect.bind(this), this._getBackoffDelay(++this._reconnectAttempts));
274+
$timeout(angular.bind(this, this._connect), this._getBackoffDelay(++this._reconnectAttempts));
297275

298276
return this;
299277
};

‎angular-websocket.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.