123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #!/usr/bin/env node
- const path = require('path');
- const pkg = require('../package.json');
- const fs = require('fs');
- const version = pkg.version;
- const PeerServer = require('../src').PeerServer;
- const opts = require('optimist')
- .usage('Usage: $0')
- .options({
- debug: {
- demand: false,
- alias: 'd',
- description: 'debug',
- default: false
- },
- timeout: {
- demand: false,
- alias: 't',
- description: 'timeout (milliseconds)',
- default: 5000
- },
- ip_limit: {
- demand: false,
- alias: 'i',
- description: 'IP limit',
- default: 5000
- },
- concurrent_limit: {
- demand: false,
- alias: 'c',
- description: 'concurrent limit',
- default: 5000
- },
- key: {
- demand: false,
- alias: 'k',
- description: 'connection key',
- default: 'peerjs'
- },
- sslkey: {
- demand: false,
- description: 'path to SSL key'
- },
- sslcert: {
- demand: false,
- description: 'path to SSL certificate'
- },
- port: {
- demand: true,
- alias: 'p',
- description: 'port'
- },
- path: {
- demand: false,
- description: 'custom path',
- default: '/'
- },
- allow_discovery: {
- demand: false,
- description: 'allow discovery of peers'
- }
- })
- .boolean('allow_discovery')
- .argv;
- process.on('uncaughtException', function (e) {
- console.error('Error: ' + e);
- });
- if (opts.sslkey || opts.sslcert) {
- if (opts.sslkey && opts.sslcert) {
- opts.ssl = {
- key: fs.readFileSync(path.resolve(opts.sslkey)),
- cert: fs.readFileSync(path.resolve(opts.sslcert))
- };
- delete opts.sslkey;
- delete opts.sslcert;
- } else {
- console.error('Warning: PeerServer will not run because either ' +
- 'the key or the certificate has not been provided.');
- process.exit(1);
- }
- }
- const userPath = opts.path;
- const server = PeerServer(opts, server => {
- var host = server.address().address;
- var port = server.address().port;
- console.log(
- 'Started PeerServer on %s, port: %s, path: %s (v. %s)',
- host, port, userPath || '/', version
- );
- });
- server.on('connection', client => {
- console.log(`Client connected: ${client.getId()}`);
- });
- server.on('disconnect', client => {
- console.log(`Client disconnected: ${client.getId()}`);
- });
|