Commit d7b249f7 authored by Dennis Willers's avatar Dennis Willers 🏀

Add Response Logik for socket. Should give now real responses.

parent 58ce6b0e
Pipeline #421 passed with stages
in 2 minutes and 47 seconds
const axios = require('axios');
/**
* @return {boolean}
*/
function isJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function checkIfRequestHasVote(json) {
return json.hasOwnProperty('vote');
}
function sendRequest(url, json, callback) {
//make post request
axios
.post(url, JSON.parse(json))
.then(res => {
//echo data
callback(res);
})
.catch(error => {
console.error(error);
callback(null);
});
}
module.exports = {
isJsonString,
checkIfRequestHasVote,
sendRequest
};
var net = require('net'); var net = require('net');
const axios = require('axios'); var hf = require('./helpFunctions');
// creates the server // creates the server
var server = net.createServer(); var server = net.createServer();
...@@ -73,25 +73,27 @@ server.on('connection',function(socket){ ...@@ -73,25 +73,27 @@ server.on('connection',function(socket){
console.log('Bytes read : ' + bread); console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite); console.log('Bytes written : ' + bwrite);
console.log('Data sent to server : ' + data); console.log('Data sent to server : ' + data);
axios
.post('http://localhost:8080/setVote', { if(hf.isJsonString(data)) {
mapId: 'abcdef', let url;
name: 'alphabet', if (hf.checkIfRequestHasVote(data)) {
userId: 'xyz', url = 'http://localhost:8080/setVote';
vote: '30' } else {
}) url ='http://localhost:8080/getMapInfo';
.then(res => { }
//echo data hf.sendRequest(url, data, res => {
var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n'); if (res) {
if(is_kernel_buffer_full){ var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n');
console.log('Data was flushed successfully from kernel buffer i.e written successfully!'); if(is_kernel_buffer_full){
}else{ console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
socket.pause(); } else {
socket.pause();
}
} else {
console.log("Res was null");
} }
})
.catch(error => {
console.error(error)
}); });
}
}); });
socket.on('drain',function(){ socket.on('drain',function(){
...@@ -236,9 +238,14 @@ client.on('data',function(data){ ...@@ -236,9 +238,14 @@ client.on('data',function(data){
// 87.78.129.86 | localhost // 87.78.129.86 | localhost
const testClient = net.connect({host: '87.78.129.86', port: 3201}, () => { const testClient = net.connect({host: 'localhost', port: 3000}, () => {
console.log('connected to server!'); console.log('connected to server!');
testClient.write('Hello World Client!\r\n'); //testClient.write('Hello World Client!\r\n');
testClient.write(JSON.stringify({
"mapId": "keineBekannteMap1",
"name": "keinBekannterMapName1",
"userId": "keinBekannterUser1"
}));
//test(testClient); //test(testClient);
}); });
......
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 to comment