Skip to content

Commit

Permalink
feat: available sigver as command line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Aug 10, 2016
1 parent c6a3624 commit 497a0a3
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 326 deletions.
53 changes: 36 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,54 @@

Very simple signaling server based on WebSocket to test WebRTC.

| [<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Node.js_logo.svg/32px-Node.js_logo.svg.png" alt="NodeJS" width="32px" height="20px" />](http://godban.github.io/browsers-support-badges/)</br>NodeJS |
| --------- |
| 4 and above

## How to use
For Node 6 and above:
```
node server.js [OPTIONS]
```
For the previous versions of node (this by default for `npm start`):
```
node server.es5.js [OPTIONS]
npm install -g sigver

### As command line tool
Usage: sigver [options]

Options:
-h, --help output usage information
-v, --version output the version number
-h, --host <n> specify host (DEFAULT: process.env.NODE_IP || "localhost")
-p, --port <n> specify port (DEFAULT: process.env.NODE_PORT || 8000)

Examples:
$ sigver
$ sigver -h 192.168.0.1 -p 9000

### As library
```javascript
const sigver = require('sigver')
sigver.start({
host: process.env.NODE_IP || "localhost"
port: process.env.NODE_PORT || 8000
onStart: () => {

// Do something...

sigver.stop()
}
})
```
-h, --host
- Specify host for the server. `localhost` by default.

-p, --port
- Specify port for the server. `8000` by default.

## Message protocol
## Protocol
Message is a JSON string.

### Income messages
#### From peer who triggered connection
- When you wants to establish a connection with someone (you need to provide him this `key`).
- When you want to establish a connection with someone (you need to provide him this `key` and wait until he send the `join` message to the server).
```json
{"key": "[some unique key]"}
```
- When you wants to forward `data` to the peer identified by `id`.
```json
{"id": "[identifier]",
"data": "[any data]"}
{"id": "[identifier]", "data": "[any data]"}
```


Expand All @@ -60,8 +80,7 @@ Message is a JSON string.
```
- Server notify that the peer with `id` is no longer available.
```json
{"id": "[identifier of the unavailable peer]",
"unavailable": "true"}
{"id": "[identifier of the unavailable peer]", "unavailable": "true"}
```

#### To peer wishing to connect
Expand Down
1 change: 1 addition & 0 deletions bin/index.es5.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

172 changes: 172 additions & 0 deletions dist/sigver.es5.umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.sigver = global.sigver || {})));
}(this, function (exports) { 'use strict';

var WebSocketServer = require('ws').Server;
var WebSocket = require('ws');

// CloseEvent codes
var DATA_SYNTAX_ERROR = 4000;
var DATA_UNKNOWN_ATTRIBUTE = 4001;
var KEY_ALREADY_EXISTS = 4002;
var KEY_UNKNOWN = 4003;
var KEY_NO_LONGER_AVAILABLE = 4004;

var settings = {
host: process.env.NODE_IP || 'localhost',
port: process.env.NODE_PORT || 8000,
onStart: function onStart() {}
};
var server = void 0;

function error(socket, code, msg) {
console.log('Error ' + code + ': ' + msg);
socket.close(code, msg);
}

function start(options) {
Object.assign(settings, options);
server = new WebSocketServer(settings, function () {
console.log('Server runs on: ' + settings.host + ':' + settings.port);
settings.onStart();
});

server.on('connection', function (socket) {
socket.on('message', function (data) {
var msg = void 0;
try {
msg = JSON.parse(data);
} catch (event) {
error(socket, DATA_SYNTAX_ERROR, 'Server accepts only JSON');
}
try {
if ('key' in msg) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = server.clients[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var master = _step.value;

if (master.key === msg.key) {
error(socket, KEY_ALREADY_EXISTS, 'The key already exists');
return;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

socket.key = msg.key;
socket.joiningClients = [];
} else if ('id' in msg) {
for (var index in socket.joiningClients) {
if (index === msg.id.toString()) {
socket.joiningClients[index].send(JSON.stringify({ data: msg.data }));
return;
}
}
socket.send(JSON.stringify({ id: msg.id, unavailable: true }));
} else if ('join' in msg) {
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;

try {
for (var _iterator2 = server.clients[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _master = _step2.value;

if (_master.key === msg.join) {
socket.master = _master;
_master.joiningClients.push(socket);
var id = _master.joiningClients.length - 1;
_master.send(JSON.stringify({ id: id, data: msg.data }));
return;
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}

error(socket, KEY_UNKNOWN, 'Unknown key');
} else if ('data' in msg && 'master' in socket) {
var _id = socket.master.joiningClients.indexOf(socket);
if (socket.master.readyState === WebSocket.OPEN) {
socket.master.send(JSON.stringify({ id: _id, data: msg.data }));
}
} else {
console.log('data: ', msg);
error(socket, DATA_UNKNOWN_ATTRIBUTE, 'Unsupported message format');
}
} catch (event) {
error(socket, DATA_SYNTAX_ERROR, 'server accepts only JSON');
}
});

socket.on('close', function (event) {
if ('joiningClients' in socket) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;

try {
for (var _iterator3 = socket.joiningClients[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var client = _step3.value;

client.close(KEY_NO_LONGER_AVAILABLE, 'The peer is no longer available');
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
});
});
}

function stop() {
console.log('Server has stopped successfully');
server.close();
}

exports.start = start;
exports.stop = stop;

Object.defineProperty(exports, '__esModule', { value: true });

}));
25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
{
"name": "sigver",
"description": "webRTC signaling server",
"description": "WebRTC signaling server",
"version": "0.0.0-sementically-released",
"main": "server.es5.js",
"main": "es5/sigver.umd.js",
"jsnext:main": "sigver.js",
"bin": {
"sigver": "bin/index.es5.min.js"
},
"scripts": {
"start": "node server.es5.js",
"build": "babel --presets es2015 -o server.es5.js server.js",
"test": "node test.js",
"start": "node bin/index.es5.min.js",
"build": "rm -rf dist bin & node rollup.config.js",
"pretest": "npm run build",
"test": "node test/sigver.test.js",
"commit": "git-cz",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"keywords": [
"webRTC",
"WebRTC",
"signaling",
"server"
],
Expand All @@ -22,18 +27,22 @@
"url": "https://github.com/coast-team/sigver.git"
},
"dependencies": {
"commander": "^2.9.0",
"ws": "^1.1.1"
},
"optionalDependencies": {
"bufferutil": "^1.2.1",
"utf-8-validate": "^1.2.1"
},
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-preset-es2015": "^6.9.0",
"babel-preset-es2015-rollup": "^1.2.0",
"commitizen": "^2.5.0",
"cz-conventional-changelog": "^1.1.5",
"ghooks": "^1.0.3",
"rollup": "^0.34.7",
"rollup-plugin-babel": "^2.6.1",
"rollup-plugin-filesize": "^0.4.4",
"rollup-plugin-uglify": "^1.0.1",
"semantic-release": "^4.3.5",
"standard": "^7.1.1"
},
Expand Down
40 changes: 40 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

let rollup = require('rollup')
let babel = require('rollup-plugin-babel')
let uglify = require('rollup-plugin-uglify')
let filesize = require('rollup-plugin-filesize')

rollup.rollup({
entry: 'src/index.js',
banner: '#!/usr/bin/env node',
plugins: [
babel({
exclude: 'node_modules/**',
presets: ['es2015-rollup']
}),
uglify(),
filesize()
]
}).then((bundle) => {
console.log('index.es5.min.js: ')
bundle.write({dest: 'bin/index.es5.min.js'})
})

rollup.rollup({
entry: 'src/sigver.js',
plugins: [
babel({
exclude: 'node_modules/**',
presets: ['es2015-rollup']
}),
filesize()
]
}).then((bundle) => {
console.log('sigver.es5.umd.js: ')
bundle.write({
format: 'umd',
moduleName: 'sigver',
dest: 'dist/sigver.es5.umd.js'
})
})
Loading

0 comments on commit 497a0a3

Please sign in to comment.