Commit 82a58b60 by amir

add http posting

parent c548a669
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
}
]
}
\ No newline at end of file
......@@ -4,9 +4,10 @@ const url = require('url');
const WebSocket = require('ws');
const fs = require('fs');
const schedule = require('node-schedule');
const request = require('request');
var myArgs = require('optimist').argv,
help = './websocketExpress -p port -f file-path [-d delay-between-samples]';
help = './websocketExpress -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]';
/**
* some validation
......@@ -39,10 +40,25 @@ var index = 0;
var sendJson = function(){
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
var currentObj = jsonFile[index];
client.send(JSON.stringify(currentObj));
index = ++index % jsonFile.length;
if (myArgs.postUrl == null){
if (client.readyState === WebSocket.OPEN) {
var currentObj = jsonFile[index];
client.send(JSON.stringify(currentObj));
index = ++index % jsonFile.length;
}
} else {
request.post({
url: myArgs.postUrl,
headers: {
"Content-Type": "application/json"
},
body: jsonFile[index],
json:true
}, function(error, response, body){
console.log(error);
console.log(JSON.stringify(response));
console.log(body);
});
}
});
}
......
......@@ -18,10 +18,10 @@
"http": "0.0.0",
"node-schedule": "^1.2.5",
"optimist": "^0.6.1",
"request": "^2.83.0",
"ws": "^3.2.0"
},
"bin": {
"ws-event-emulator": "./app.js"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment