Commit 332b9d78 authored by Dennis Willers's avatar Dennis Willers 🏀

Change socket.js to new API

parent 93974b59
...@@ -18,7 +18,21 @@ function checkIfRequestHasVote(json) { ...@@ -18,7 +18,21 @@ function checkIfRequestHasVote(json) {
return JSON.parse(json).hasOwnProperty('vote'); return JSON.parse(json).hasOwnProperty('vote');
} }
function sendRequest(url, json, callback) { function sendGetRequest(url, callback) {
//make post request
axios
.get(url)
.then(res => {
//echo data
callback(res);
})
.catch(error => {
console.error(error);
callback(null);
});
}
function sendPostRequest(url, json, callback) {
//make post request //make post request
axios axios
.post(url, JSON.parse(json)) .post(url, JSON.parse(json))
...@@ -45,6 +59,7 @@ function addMapToClient(clientList, currentClient, mapId, callback) { ...@@ -45,6 +59,7 @@ function addMapToClient(clientList, currentClient, mapId, callback) {
module.exports = { module.exports = {
isJsonString, isJsonString,
checkIfRequestHasVote, checkIfRequestHasVote,
sendRequest, sendGetRequest,
sendPostRequest,
addMapToClient addMapToClient
}; };
...@@ -63,7 +63,7 @@ app.use(function(req, res, next){ ...@@ -63,7 +63,7 @@ app.use(function(req, res, next){
if (ua.toLowerCase().includes(bUA.toLowerCase())){ if (ua.toLowerCase().includes(bUA.toLowerCase())){
blacklisted = true blacklisted = true
} }
}) });
if (blacklisted) { if (blacklisted) {
console.log('BLACKLISTED UA: ' + ua) console.log('BLACKLISTED UA: ' + ua)
...@@ -72,7 +72,7 @@ app.use(function(req, res, next){ ...@@ -72,7 +72,7 @@ app.use(function(req, res, next){
next() next()
} }
} }
}) });
app.get('/', (req, res) => { app.get('/', (req, res) => {
let data = {online: true, routes: [], docs: 'https://git.willers.digital/Dennis.Willers/voting/blob/master/README.md'}; let data = {online: true, routes: [], docs: 'https://git.willers.digital/Dennis.Willers/voting/blob/master/README.md'};
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"valid-url": "^1.0.9",
"trackmania.io": "^2.4.0" "trackmania.io": "^2.4.0"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -72,32 +72,28 @@ server.on('connection',function(socket){ ...@@ -72,32 +72,28 @@ server.on('connection',function(socket){
console.log('Data sent to server : ' + data); console.log('Data sent to server : ' + data);
if(hf.isJsonString(data)) { 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) {
const req = JSON.parse(data); const req = JSON.parse(data);
hf.addMapToClient(clients, socket, req['mapId'], newClientList => { if (req['http'] === 'GET') {
hf.sendGetRequest('http://localhost:8080/' + req['url'], res => {
hf.addMapToClient(clients, socket, req['map'], newClientList => {
clients = newClientList.slice(); clients = newClientList.slice();
socket.write(JSON.stringify(res.data) + '\n'); socket.write(JSON.stringify(res.data) + '\n');
if (hf.checkIfRequestHasVote(data)) {
sendToAllClientsAtSameMap(res, req['mapId'], socket);
}
}); });
//var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n'); })
//if(is_kernel_buffer_full){ } else if (req['http'] === 'POST') {
// console.log('Data was flushed successfully from kernel buffer i.e written successfully!'); hf.sendPostRequest('http://localhost:8080/setVote', data, res => {
//} else { if (res) {
// socket.pause(); hf.addMapToClient(clients, socket, req['map'], newClientList => {
//} clients = newClientList.slice();
socket.write(JSON.stringify(res.data) + '\n');
sendToAllClientsAtSameMap(req['map'], socket);
});
} else { } else {
console.log("Res was null"); console.log("Res was null");
} }
}); });
}
} }
}); });
...@@ -150,7 +146,8 @@ server.on('connection',function(socket){ ...@@ -150,7 +146,8 @@ server.on('connection',function(socket){
} }
// Send a message to all clients playing the same map // Send a message to all clients playing the same map
function sendToAllClientsAtSameMap(res, mapId, currentClient) { function sendToAllClientsAtSameMap(mapId, currentClient) {
hf.sendGetRequest('http://localhost:8080/getVotes?map='+mapId, res => {
console.log("JSON: ", JSON.stringify(res.data)); console.log("JSON: ", JSON.stringify(res.data));
clients.forEach(function (client) { clients.forEach(function (client) {
const resJson = res.data; const resJson = res.data;
...@@ -158,6 +155,7 @@ server.on('connection',function(socket){ ...@@ -158,6 +155,7 @@ server.on('connection',function(socket){
if (client.mapId === mapId && client.name !== currentClient) client.write(JSON.stringify(resJson) + '\n'); if (client.mapId === mapId && client.name !== currentClient) client.write(JSON.stringify(resJson) + '\n');
}); });
console.log("Write Message to all at " + mapId +": " + JSON.stringify(res.data)); console.log("Write Message to all at " + mapId +": " + JSON.stringify(res.data));
});
} }
}); });
...@@ -253,11 +251,15 @@ client.on('data',function(data){ ...@@ -253,11 +251,15 @@ client.on('data',function(data){
// open new node instance => and run it... // open new node instance => and run it...
// 87.78.129.86 | localhost // 87.78.129.86 | localhost
// Examples:
// getVotes: {"map":"OkNwgzSwDLZSWSRPsUKY7EA1Cg5","http":"GET","url":"getVotes?map=OkNwgzSwDLZSWSRPsUKY7EA1Cg5"}
// setVotes: {"http":"POST", "map": "OkNwgzSwDLZSWSRPsUKY7EA1Cg5", "player": "Jtmn3kBnSSadky_mLNhp_A", "vote": 50}
// getPlayerVote: {"map":"OkNwgzSwDLZSWSRPsUKY7EA1Cg5","http":"GET","url":"getPlayerVote?map=OkNwgzSwDLZSWSRPsUKY7EA1Cg5&player=Jtmn3kBnSSadky_mLNhp_A"}
/*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":"7fsfRSUCQ7YwfBEdRk_GivW6qzj","userId":"aKBNg9fbReqLedLiz1KFfA","name":"Summer 2021 - 01"})); testClient.write(JSON.stringify({"http":"POST", "map": "OkNwgzSwDLZSWSRPsUKY7EA1Cg5", "player": "Jtmn3kBnSSadky_mLNhp_A", "vote": 80}));
//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