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'); ...@@ -4,9 +4,10 @@ const url = require('url');
const WebSocket = require('ws'); const WebSocket = require('ws');
const fs = require('fs'); const fs = require('fs');
const schedule = require('node-schedule'); const schedule = require('node-schedule');
const request = require('request');
var myArgs = require('optimist').argv, 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 * some validation
...@@ -39,10 +40,25 @@ var index = 0; ...@@ -39,10 +40,25 @@ var index = 0;
var sendJson = function(){ var sendJson = function(){
wss.clients.forEach(function each(client) { wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) { if (myArgs.postUrl == null){
var currentObj = jsonFile[index]; if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(currentObj)); var currentObj = jsonFile[index];
index = ++index % jsonFile.length; 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 @@ ...@@ -18,10 +18,10 @@
"http": "0.0.0", "http": "0.0.0",
"node-schedule": "^1.2.5", "node-schedule": "^1.2.5",
"optimist": "^0.6.1", "optimist": "^0.6.1",
"request": "^2.83.0",
"ws": "^3.2.0" "ws": "^3.2.0"
}, },
"bin": { "bin": {
"ws-event-emulator": "./app.js" "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