Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ws-additions

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ws-additions - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

src/hunting-websocket.litcoffee

1

index.js
module.exports = require("./src/reconnecting-websocket.js");
module.exports.HuntingWebSocket = require("./src/hunting-websocket.litcoffee");

45

package.json
{
"name": "ws-additions",
"version": "0.0.2",
"version": "0.0.3",
"description": "Helpful additional functionality for the vanilla WebSocket.",
"keywords": ["WebSocket","reconnect","reconnecting","resend"],
"keywords": [
"WebSocket",
"reconnect",
"reconnecting",
"resend"
],
"main": "index.js",
"author": {"name": "Ian Groff"},
"author": {
"name": "Ian Groff"
},
"contributors": [
{"name": "Ian Groff", "url": "https://github.com/igroff"},
{"name": "Ryan Fairchild", "url": "https://github.com/unscene"},
{"name": "David Hayes", "url": "https://drhayes.io"}
{
"name": "Ian Groff",
"url": "https://github.com/igroff"
},
{
"name": "Ryan Fairchild",
"url": "https://github.com/unscene"
},
{
"name": "David Hayes",
"url": "https://drhayes.io"
}
],
"license": "ISC",
"repository": {"type": "git", "url": "https://github.com/igroff/ws-additions.git"},
"repository": {
"type": "git",
"url": "https://github.com/igroff/ws-additions.git"
},
"dependencies": {
"ws": "0.4.31",
"simplog": "0.1.0"
"simplog": "0.1.0",
"lodash": "^2.4.1"
},

@@ -26,4 +46,11 @@ "devDependencies": {

"serve-static": "1.3.0",
"supervisor": "0.6.0"
"supervisor": "0.6.0",
"q": "^1.0.1",
"coffeeify": "^0.6.0"
},
"browserify": {
"transform": [
"coffeeify"
]
}
}
# WebSocket Additions - `Documentation Woefully Incomplete (DWI)`
# WebSocket Additions

@@ -28,5 +28,5 @@ ### What is this thing?

isn't connected when you call 'send'.
* Hunting ( future ) - given a list of hosts, find the fastest available and use
it. Of course, if a problem is encountered with a 'connected' host drop it like
a ton of bricks and go find the new 'best one'.
* Hunting - given a list of hosts, connect to them and send messages to which
ever one is available, switching to another if the 'active' connection becomes
unavailable. Dumb-as-dirt client side fail over.

@@ -56,3 +56,9 @@ ### ASSumptions

Once you've done that successfully you should find a test page at `http://localhost:8080/index.html`
Once you've done that successfully you should find a test pages at
* `http://localhost:8080/index.html`
* `http://localhost:8080/hunting.html`
* `http://localhost:8080/controlled.html`
* `http://localhost:8080/controlled-resending.html`
A bunch of these tests blow up the server ( by design ) so it's hard to get them
all to run at the same time ( hence the multiple pages ).

@@ -180,1 +186,15 @@ ### Usage!

The hunting websocket is so unlike anything else, it seems unlikely that a polyfill
would be valuable, so he's a little more basic.
```html
<script src="js/reconn.js"></script>
<script>
var HuntingWebSocket = require("reconnecting-websocket").HuntingWebSocket;
var testWs = new HuntingWebSocket([
"ws://localhost:8085/socket",
"ws://localhost:8086/socket"
]);
testWs.send("this message is AWESOME!");
</script>
```

@@ -24,8 +24,6 @@ log = require('simplog');

// error
if (this.underlyingWs.readyState != OriginalWebSocket.OPEN){
if (this.underlyingWs == null || this.underlyingWs.readyState != OriginalWebSocket.OPEN){
log.info("this.underlyingWs not open, reconnecting");
this.ondatanotsent(new MessageEvent("datanotsent", {data:data}));
this.reconnect();
var e = new MessageEvent('datanotsent');
e.data = data;
this.ondatanotsent(e);
} else {

@@ -36,12 +34,12 @@ // otherwise we try to send, and if we have a failure

try {
log.info("sending: ", data);
log.debug("sending to(%s) : %j", this.underlyingWs.url, data);
this.underlyingWs.send(data);
return true; //sent
} catch (error) {
log.error("error during send on this.underlyingWs", e);
log.error(error);
this.ondatanotsent(new MessageEvent("datanotsent", {data:data}));
this.reconnect();
var e = new MessageEvent('datanotsent');
e.data = data;
this.ondatanotsent(e);
}
}
return false; // not sent
}

@@ -68,6 +66,6 @@

this.reconnect = function() {
log.info("reconnecting");
log.debug("reconnecting: ", url);
if ( readyState === OriginalWebSocket.CONNECTING ||
readyState === RECONNECTING ||
this.underlyingWs.readyState === OriginalWebSocket.CONNECTING )
(this.underlyingWs != null && this.underlyingWs.readyState === OriginalWebSocket.CONNECTING ))
{

@@ -74,0 +72,0 @@ return;

#! /usr/bin/env node
var express = require('express');
var child = require('child_process');
var http = require('http');

@@ -7,2 +8,4 @@ var path = require('path');

var errorhandler = require('errorhandler');
var log = require('simplog');
var _ = require('lodash');

@@ -19,17 +22,65 @@ var app = express();

var wss = new WebSocketServer({server: server, path: "/socket"});
var serverPort = Number(process.env.PORT || 8080);
var allMyChildren = [];
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
function sendMessage(type, data){
log.info("(%s) sending: %j", serverPort, data || type);
if ( typeof(type) === "object" ){
type.port = serverPort;
ws.send(JSON.stringify(type));
} else if ( data ) {
ws.send(JSON.stringify({type: type, data: data, port:serverPort}));
} else {
ws.send(JSON.stringify({type: type, port: serverPort}));
}
}
console.log("handling: " + message);
if ( message === "ping" ){
console.log("pongin'");
ws.send("pong");
log.info("pongin'");
sendMessage("pong");
} else if ( message === "die" ){
console.log(wss);
console.log("et, tu, Brute?");
process.exit(1);
log.info(" (" + serverPort + ") et, tu, Brute?");
sendMessage({type: "dying", port:serverPort});
process.exit(0);
} else if ( message === "what's your pid" ){
sendMessage({pid: process.pid});
} else if ( message === "what's your port" ){
sendMessage({port: serverPort});
} else if ( JSON.parse(message) === null ){
log.info("null message!");
} else {
var action = JSON.parse(message);
if ( action.type === "kill" ){
if ( action.port ){
_.each(allMyChildren, function(child) { (child.onPort == action.port) && child.kill(); });
allMyChildren = _.reject(allMyChildren, function(child){ return child.onPort == action.port; });
} else if ( action.signal ){
process.kill(action.pid, action.signal);
} else {
process.kill(action.pid);
}
} else if ( action.type === "start" ){
var childPort = action.port;
myChild = child.fork(__filename, {env: {PORT: action.port}});
myChild.on('exit', function(){
log.info('child exited');
sendMessage({type: "dead", childPort: childPort});
});
myChild.onPort = childPort;
allMyChildren.push(myChild);
sendMessage( { type: "started", childPort: action.port, port: action.port, pid: myChild.pid });
} else if ( action.type === "echo" ){
sendMessage(action);
}
}
});
ws.send('connected');
ws.send(JSON.stringify({pid: process.pid, type: 'connected', port: serverPort}));
});
server.listen(process.env.PORT || 8080);
process.on('uncaughtException', function (e){
console.log("ouch, exception port " + serverPort);
console.log(e.stack);
});
server.listen(serverPort);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc