Skip to content

Commit

Permalink
[major] Move wscat to it's own library/package/folder. Fixes #256
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Nov 20, 2014
1 parent 22ea90f commit 1af6323
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 89 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,6 @@ ws.on('message', function(data, flags) {
});
```

### wscat against echo.websocket.org ###

$ npm install -g ws
$ wscat -c ws://echo.websocket.org
connected (press CTRL+C to quit)
> hi there
< hi there
> are you a happy parrot?
< are you a happy parrot?

### Other examples ###

Expand Down
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"name": "ws",
"description": "simple to use, blazing fast and thoroughly tested websocket client, server and console for node.js, up-to-date against RFC-6455",
"version": "0.4.32",
"licenses" : [
"licenses": [
{
"type" : "MIT",
"url" : "https://raw.githubusercontent.com/einaros/ws/master/README.md"
"type": "MIT",
"url": "https://raw.githubusercontent.com/einaros/ws/master/README.md"
}
],
"keywords": [
Expand All @@ -22,9 +22,6 @@
"type": "git",
"url": "git://github.com/einaros/ws.git"
},
"bin": {
"wscat": "./bin/wscat"
},
"scripts": {
"test": "make test",
"install": "(node-gyp rebuild 2> builderror.log) || (exit 0)"
Expand All @@ -33,9 +30,7 @@
"node": ">=0.4.0"
},
"dependencies": {
"commander": "~2.1.0",
"nan": "~1.0.0",
"tinycolor": "0.x",
"options": ">=0.0.5"
},
"devDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions wscat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# wscat

WebSocket cat.

## Installation

This module needs to be installed globally so use the `-g` flag when installing:

```
npm install -g wscat
```

## Usage

```
$ wscat -c ws://echo.websocket.org
connected (press CTRL+C to quit)
> hi there
< hi there
> are you a happy parrot?
< are you a happy parrot?
```

## License

MIT
145 changes: 73 additions & 72 deletions bin/wscat → wscat/bin/wscat
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,37 @@
* Module dependencies.
*/

var WebSocket = require('../')
, fs = require('fs')
, program = require('commander')
, util = require('util')
var program = require('commander')
, readline = require('readline')
, events = require('events')
, readline = require('readline');
, WebSocket = require('ws')
, util = require('util')
, fs = require('fs');

/**
* InputReader - processes console input
* InputReader - processes console input.
*/

function Console() {
if (!(this instanceof Console)) return new Console();

this.stdin = process.stdin;
this.stdout = process.stdout;

this.readlineInterface = readline.createInterface(this.stdin, this.stdout);

var self = this;
this.readlineInterface.on('line', function(data) {

this.readlineInterface.on('line', function line(data) {
self.emit('line', data);
});
this.readlineInterface.on('close', function() {
}).on('close', function close() {
self.emit('close');
});

this._resetInput = function() {
self.clear();
}
};
}

util.inherits(Console, events.EventEmitter);

Console.Colors = {
Expand All @@ -49,41 +51,43 @@ Console.Colors = {
Default: '\033[39m'
};

Console.prototype.prompt = function() {
Console.prototype.prompt = function prompt() {
this.readlineInterface.prompt();
}
};

Console.prototype.print = function(msg, color) {
Console.prototype.print = function print(msg, color) {
this.clear();
color = color || Console.Colors.Default;
this.stdout.write(color + msg + Console.Colors.Default + '\n');
this.prompt();
}
};

Console.prototype.clear = function() {
Console.prototype.clear = function clear() {
this.stdout.write('\033[2K\033[E');
}
};

Console.prototype.pause = function() {
Console.prototype.pause = function pausing() {
this.stdin.on('keypress', this._resetInput);
}
};

Console.prototype.resume = function() {
Console.prototype.resume = function resume() {
this.stdin.removeListener('keypress', this._resetInput);
}
};

function appender(xs) {
xs = xs || [];

return function (x) {
xs.push(x);
return xs;
}
};
}

function into(obj, kvals) {
kvals.forEach(function (kv) {
obj[kv[0]] = kv[1];
});

return obj;
}

Expand All @@ -95,8 +99,8 @@ function splitOnce(sep, str) { // sep can be either String or RegExp
/**
* The actual application
*/
var version = require('/package.json').version;

var version = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version;
program
.version(version)
.usage('[options] <url>')
Expand All @@ -114,109 +118,106 @@ program
if (program.listen && program.connect) {
console.error('\033[33merror: use either --listen or --connect\033[39m');
process.exit(-1);
}
else if (program.listen) {
} else if (program.listen) {
var wsConsole = new Console();
wsConsole.pause();

var options = {};

if (program.protocol) options.protocolVersion = program.protocol;
if (program.origin) options.origin = program.origin;
if (program.subprotocol) options.protocol = program.subprotocol;
if (!program.check) options.rejectUnauthorized = program.check;

var ws = null;
var wss = new WebSocket.Server({port: program.listen}, function() {
var wss = new WebSocket.Server({ port: program.listen }, function listening() {
wsConsole.print('listening on port ' + program.listen + ' (press CTRL+C to quit)', Console.Colors.Green);
wsConsole.clear();
});
wsConsole.on('close', function() {
if (ws) {
try {
ws.close();
}
catch (e) {}
}

wsConsole.on('close', function close() {
try { ws.close(); }
catch (e) {}

process.exit(0);
});
wsConsole.on('line', function(data) {

wsConsole.on('line', function line(data) {
if (ws) {
ws.send(data, {mask: false});
ws.send(data, { mask: false });
wsConsole.prompt();
}
});

wss.on('connection', function(newClient) {
if (ws) {
// limit to one client
newClient.terminate();
return;
};
if (ws) return newClient.terminate();

ws = newClient;
wsConsole.resume();
wsConsole.prompt();
wsConsole.print('client connected', Console.Colors.Green);
ws.on('close', function() {

ws.on('close', function close() {
wsConsole.print('disconnected', Console.Colors.Green);
wsConsole.clear();
wsConsole.pause();
ws = null;
});
ws.on('error', function(code, description) {
}).on('error', function error(code, description) {
wsConsole.print('error: ' + code + (description ? ' ' + description : ''), Console.Colors.Yellow);
});
ws.on('message', function(data, flags) {
}).on('message', function message(data, flags) {
wsConsole.print('< ' + data, Console.Colors.Blue);
});
});
wss.on('error', function(error) {
}).on('error', function servererrror(error) {
wsConsole.print('error: ' + error.toString(), Console.Colors.Yellow);
process.exit(-1);
});
}
else if (program.connect) {
} else if (program.connect) {
var wsConsole = new Console();
var options = {};

if (program.protocol) options.protocolVersion = program.protocol;
if (program.origin) options.origin = program.origin;
if (program.subprotocol) options.protocol = program.subprotocol;
if (program.host) options.host = program.host;
if (!program.check) options.rejectUnauthorized = program.check;
var headers = into({}, (program.header || []).map(function (s) {
return splitOnce(':', s)
}));

var headers = into({}, (program.header || []).map(function split(s) {
return splitOnce(':', s);
}));

if (program.auth) {
headers['Authorization'] = 'Basic ' + new Buffer(program.auth).toString('base64');
headers.Authorization = 'Basic '+ new Buffer(program.auth).toString('base64');
}

options.headers = headers;
var ws = new WebSocket(program.connect, options);
ws.on('open', function() {

ws.on('open', function open() {
wsConsole.print('connected (press CTRL+C to quit)', Console.Colors.Green);
wsConsole.on('line', function(data) {
ws.send(data, {mask: true});
wsConsole.on('line', function line(data) {
ws.send(data, { mask: true });
wsConsole.prompt();
});
});
ws.on('close', function() {
}).on('close', function close() {
wsConsole.print('disconnected', Console.Colors.Green);
wsConsole.clear();
process.exit();
});
ws.on('error', function(code, description) {
}).on('error', function error(code, description) {
wsConsole.print('error: ' + code + (description ? ' ' + description : ''), Console.Colors.Yellow);
process.exit(-1);
});
ws.on('message', function(data, flags) {
}).on('message', function message(data, flags) {
wsConsole.print('< ' + data, Console.Colors.Blue);
});
wsConsole.on('close', function() {
if (ws) {
try {
ws.close();
}
catch(e) {}
process.exit();
}

wsConsole.on('close', function close() {
if (!ws) return;

try { ws.close(); }
catch(e) {}

process.exit();
});
}
else {
} else {
console.error('\033[33merror: use either --listen or --connect\033[39m');
process.exit(-1);
}
28 changes: 28 additions & 0 deletions wscat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "wscat",
"version": "1.0.0",
"description": "WebSocket cat",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/einaros/ws/tree/master/wscat"
},
"keywords": [
"wscat",
"websocket",
"cat"
],
"author": "Arnout Kazemier, Einar Otto Stangvik",
"license": "MIT",
"dependencies": {
"commander": "2.5.x",
"tinycolor": "0.0.x",
"ws": "0.4.x"
},
"bin": {
"wscat": "./bin/wscat"
}
}

0 comments on commit 1af6323

Please sign in to comment.