Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ There are custom events fired by ngWebsocket.
They are useful to setup a listener for certain situations and behaviours:

- **$open**: the websocket gets open
- **$close**: the websocket gets closed
- **$close**: the websocket gets closed (callback params: {CloseEvent} closeEvent)
- **$error**: an error occurred (callback params: {Error} error)
- **$message**: the original message sent from the server (callback params: {String} message). Usually, it's a JSON encoded string containing the event to fire and the data to pass ({"event": "an event", "data": "some data"})

Expand Down Expand Up @@ -784,9 +784,16 @@ angular.run(function ($websocket) {
**Usage**

```javascript
$close()
$close(code, reason)
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| code | Number | status code, optional |
| reason | String | explanation, optional |

**Returns**

| **Type** | **Details** |
Expand Down
12 changes: 6 additions & 6 deletions ng-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@
me.$$fireEvent('$open');
};

me.$$ws.onclose = function () {
me.$$ws.onclose = function (closeEvent) {
// Activate the reconnect task
if (me.$$config.reconnect) {
me.$$reconnectTask = setInterval(function () {
if (me.$status() === me.$CLOSED) me.$open();
}, me.$$config.reconnectInterval);
}

me.$$fireEvent('$close');
me.$$fireEvent('$close', closeEvent);
};

return me;
Expand Down Expand Up @@ -239,8 +239,8 @@
return me;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not working

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was working couple of years ago. It looks like the project is not supported, so close this PR if not needed.

};

me.$close = function () {
if (me.$status() !== me.$CLOSED) me.$$ws.close();
me.$close = function (code, reason) {
if (me.$status() !== me.$CLOSED) me.$$ws.close(code, reason);

if (me.$$reconnectTask) {
clearInterval(me.$$reconnectTask);
Expand Down Expand Up @@ -299,14 +299,14 @@
else throw new Error('WebSocket is already in CLOSING or CLOSED state.');
};

me.close = function () {
me.close = function (code, reason) {
if (me.readyState === me.OPEN) {
me.readyState = me.CLOSING;

setTimeout(function () {
me.readyState = me.CLOSED;

me.onclose();
me.onclose({code: code, reason: reason}); // mock CloseEvent object
}, closeTimeout);
}

Expand Down
34 changes: 34 additions & 0 deletions test/unit/ng-websocket-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,40 @@ describe('Testing ng-websocket', function () {
});
});

describe('Testing close code, reason and CloseEvent', function () {
var ws;

beforeEach(function (done) {
ws = $websocket.$new({
url: 'ws://localhost:12345',
mock: true
});

ws.$on('$open', function () {
done();
});
});

afterEach(function () {
ws.$close();
});

it('should receive a CloseEvent on $close event', function (done) {
var code = 5000;
var reason = 'close reason';

ws.$close(code, reason);

ws.$on('$close', function (closeEvent) {
expect(closeEvent).not.toBeUndefined();
expect(closeEvent.code).toEqual(code);
expect(closeEvent.reason).toEqual(reason);

done();
});
});
});

describe('Testing $on, $un, $emit functions', function () {
var ws;

Expand Down