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');
const axios = require('axios');
var hf = require('./helpFunctions');
// creates the server
var server = net.createServer();
......@@ -73,25 +73,27 @@ server.on('connection',function(socket){
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Data sent to server : ' + data);
axios
.post('http://localhost:8080/setVote', {
mapId: 'abcdef',
name: 'alphabet',
userId: 'xyz',
vote: '30'
})
.then(res => {
//echo data
var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n');
if(is_kernel_buffer_full){
console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
}else{
socket.pause();
if(hf.isJsonString(data)) {
let url;
if (hf.checkIfRequestHasVote(data)) {
url = 'http://localhost:8080/setVote';
} else {
url ='http://localhost:8080/getMapInfo';
}
hf.sendRequest(url, data, res => {
if (res) {
var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n');
if(is_kernel_buffer_full){
console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
} else {
socket.pause();
}
} else {
console.log("Res was null");
}
})
.catch(error => {
console.error(error)
});
}
});
socket.on('drain',function(){
......@@ -236,9 +238,14 @@ client.on('data',function(data){
// 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!');
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);
});
......
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