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

Call setVote with fixed data for testing if socket works.

parent 28440615
Pipeline #414 passed with stages
in 2 minutes and 48 seconds
...@@ -147,7 +147,7 @@ function createRouter(db) { ...@@ -147,7 +147,7 @@ function createRouter(db) {
const average = results[0].average; const average = results[0].average;
const votes = results[0].votes; const votes = results[0].votes;
if (average !== null && votes !== null) { if (average !== null && votes !== null) {
res.header('Access-Control-Allow-Origin', "*").status(500).json({votes: votes, average: average, vote: vote}); res.header('Access-Control-Allow-Origin', "*").status(200).json({votes: votes, average: average, vote: vote});
return; return;
} }
} }
...@@ -195,7 +195,7 @@ function createRouter(db) { ...@@ -195,7 +195,7 @@ function createRouter(db) {
// the routes are defined here // the routes are defined here
router.get('/test', [test]); router.get('/test', [test]);
router.post('/addMap', [hasMap, hasVote, getMapInfo]); router.post('/setVote', [hasMap, hasVote, getMapInfo]);
router.post('/getMapInfo', [getVote]); router.post('/getMapInfo', [getVote]);
return router; return router;
......
This diff is collapsed.
...@@ -12,11 +12,10 @@ ...@@ -12,11 +12,10 @@
"author": "Dennis Willers", "author": "Dennis Willers",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^0.21.1",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"mysql": "^2.18.1", "mysql": "^2.18.1"
"socket.io": "^3.0.4",
"socket.io-client": "^3.0.4"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^2.0.7" "nodemon": "^2.0.7"
......
var net = require('net'); var net = require('net');
const axios = require('axios');
// creates the server // creates the server
var server = net.createServer(); var server = net.createServer();
...@@ -57,25 +58,32 @@ server.on('connection',function(socket){ ...@@ -57,25 +58,32 @@ server.on('connection',function(socket){
console.log('Socket timed out'); console.log('Socket timed out');
}); });
socket.on('map', function (data) {
console.log('MAP Client Connect to: ', data);
});
socket.on('data',function(data){ socket.on('data',function(data){
var bread = socket.bytesRead; var bread = socket.bytesRead;
var bwrite = socket.bytesWritten; var bwrite = socket.bytesWritten;
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', {
mapId: 'abcdef',
name: 'alphabet',
userId: 'xyz',
vote: '30'
})
.then(res => {
console.log('Prepare: ', res.headers, res.data);
//echo data //echo data
var is_kernel_buffer_full = socket.write('FROMSERVER: ' + data); var is_kernel_buffer_full = socket.write('FROMSERVER: ' + JSON.stringify(res.headers) + JSON.stringify(res.data));
if(is_kernel_buffer_full){ if(is_kernel_buffer_full){
console.log('Data was flushed successfully from kernel buffer i.e written successfully!'); console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
}else{ }else{
socket.pause(); socket.pause();
} }
})
.catch(error => {
console.error(error)
});
}); });
socket.on('drain',function(){ socket.on('drain',function(){
...@@ -207,21 +215,31 @@ client.on('data',function(data){ ...@@ -207,21 +215,31 @@ client.on('data',function(data){
// u can also => write the below code in seperate js file // u can also => write the below code in seperate js file
// open new node instance => and run it... // open new node instance => and run it...
/*const clients = net.connect({port: 3000}, () => { // 87.78.129.86
// 'connect' 3201
const clients = net.connect({host: 'localhost', port: 3000}, () => {
console.log('connected to server!'); console.log('connected to server!');
clients.write('world!\r\n'); clients.write('Hello World Client!\r\n');
//test(clients);
}); });
clients.on('data', (data) => { clients.on('data', (data) => {
console.log(data.toString()); console.log(data.toString());
clients.end(); clients.end();
}); });
clients.on('end', () => { /*clients.on('end', () => {
console.log('disconnected from server'); console.log('disconnected from server');
}); });
//*/ //*/
async function test(clients) {
let i = 0;
while (true) {
await new Promise(resolve => setTimeout(resolve, 5000));
i = i+1;
clients.write("Test: "+ i);
}
}
// ---------------------------------------------- // // ---------------------------------------------- //
......
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