Skip to content

Commit

Permalink
documentation update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Maselj committed Apr 1, 2016
1 parent eeffb56 commit 303b2f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 61 deletions.
82 changes: 24 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
Angular websocket factory
Angular websocket wrapper
=========================

Websocket client factory is intended to have subscribers to socket, and multiple clients.

## Getting started

### Usage

- add `am-ws` module to your application module dependencies
- add `websocket.wrapper` module to your application module dependencies

```javascript
angular.module('myApp', ['am-ws', ...]);
angular.module('myApp', ['websocket.wrapper', ...]);
```

- create ws client

```javascript
var WebsocketsClient = function (wsClient) {
var WebsocketsClient = function (websocketWrapper) {

var options = {
url: 'wss://localhost:58443/Game?id=gui'
};
var client = new wsClient(options);
WebsocketsClient.client = new websocketWrapper(options);

return {
init: function () {
client.connect();
},
send: function (message) {
client.send(message);
},
disconnect: function () {
client.close(1000);
},
subscribe: function (subscriber, callback) {
client.subscribe(subscriber, callback);
},
unsubscribe: function () {
client.unsubscribe();
},
connection: function(){
return client.connectionState();
}
};
return WebsocketsClient;

};

Expand All @@ -52,53 +31,40 @@ angular.module('myApp', ['am-ws', ...]);
- use client

```javascript
angular.module('myApp').controller('myCtrl', function($scope, WebsocketsClient, $log){

// define callback function
var _callback = function(message){
console.debug(message);
};

// lets subscribe to websockets messages and pass reference of our callback to socket client.
WebsocketsClient.subscribe('MyKeyForThisSubcription', _callback);
angular.module('myApp').controller('myCtrl', function($scope, WebsocketsClient, $log, $rootScope){

WebsocketsClient.init();
$rootScope.$on(WebsocketsClient.socket.onMessageBroadcastEvent, function(event, message){
//
});

// clean references
$scope.$on('$destroy', function(){
WebsocketsClient.unsubscribe('MyKeyForThisSubcription');
WebsocketsClient.socket.close();
});
});
```
```javascript
// You can add snipet to run method for socket responsible cleanup
// socket clean up
$window.onbeforeunload = function () {
WebsocketsClient.close();
};
```

### API

| Method | Definition |
| :----------- |:-------------:|
| connect | |
| send | |
| close | |
| subscribe | |
| unsubscribe | |
| connectionState | |

### Options

| Option | Default |
|:------|:--------|
| url | null|
| reconnect | true|
| reconnectIntervalTimeout | 5000|
| callbacks | { onOpen: null, onClose: null, onError: null}|
| keepAlive | false|
| keepAliveMessage | '{ping:true}'|
| keepAliveIntervalTime | 10000|

### Callbacks
Callback functions are called by reference, so cleanup mandatory to avoid memory leak.
|url| null|
|id| 'websocket'|
|onOpenBroadcast| true|
|onErrorBroadcast| true|
|onCloseBroadcast| true|
|onFailedSendBroadcast| true|
|reconnect| true|
|reconnectIntervalTimeout| 5000|
|keepAlive| false|
|keepAliveMessage| '{ping:true}'|
|keepAliveIntervalTime| 10000|
|onMessageBroadcastOnlyData| true|
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "angular-ws-factory",
"main": ["src/wsClient.js"],
"version": "0.0.1",
"description": "Web socket client factory.",
"main": ["src/websocket-wrapper.js"],
"version": "0.1.0",
"description": "Web socket wrapper.",
"license": "MIT",
"ignore": []
}

0 comments on commit 303b2f6

Please sign in to comment.