Loading helpFunctions.js +11 −4 Original line number Original line Diff line number Diff line Loading @@ -31,12 +31,19 @@ function sendRequest(url, json, callback) { }); }); } } /*function addMapToClient(clintList, client, mapId) { function addMapToClient(clientList, currentClient, mapId, callback) { const clientId = client.name; clientList.forEach(client => { }*/ if (client.name === currentClient.name) { client.mapId = mapId; console.log("Client Map ID found"); } }); return callback(clientList); } module.exports = { module.exports = { isJsonString, isJsonString, checkIfRequestHasVote, checkIfRequestHasVote, sendRequest sendRequest, addMapToClient }; }; socket.js +19 −7 Original line number Original line Diff line number Diff line Loading @@ -15,6 +15,7 @@ server.on('close',function(){ server.on('connection',function(socket){ server.on('connection',function(socket){ // Identify this client // Identify this client socket.name = socket.remoteAddress + ":" + socket.remotePort; socket.name = socket.remoteAddress + ":" + socket.remotePort; socket.mapId = "null"; clients.push(socket); clients.push(socket); // Send a nice welcome message and announce // Send a nice welcome message and announce Loading Loading @@ -68,10 +69,6 @@ server.on('connection',function(socket){ }); }); socket.on('data',function(data){ socket.on('data',function(data){ var bread = socket.bytesRead; var bwrite = socket.bytesWritten; console.log('Bytes read : ' + bread); console.log('Bytes written : ' + bwrite); console.log('Data sent to server : ' + data); console.log('Data sent to server : ' + data); if(hf.isJsonString(data)) { if(hf.isJsonString(data)) { Loading @@ -83,12 +80,17 @@ server.on('connection',function(socket){ } } hf.sendRequest(url, data, res => { hf.sendRequest(url, data, res => { if (res) { if (res) { var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n'); const req = JSON.parse(data); hf.addMapToClient(clients, socket, req['mapId'], newClientList => { clients = newClientList.slice(); sendToAllClientsAtSameMap(res, req['mapId']); }); /*var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n'); 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(); } }*/ } else { } else { console.log("Res was null"); console.log("Res was null"); } } Loading Loading @@ -144,6 +146,16 @@ server.on('connection',function(socket){ process.stdout.write(message) process.stdout.write(message) } } // Send a message to all clients playing the same map function sendToAllClientsAtSameMap(res, mapId) { console.log("JSON: ", JSON.stringify(res.data)); clients.forEach(function (client) { // Don't want to send it to sender if (client.mapId === mapId) client.write(JSON.stringify(res.data) + '\n'); }); console.log("Write Message to all at " + mapId +": " + JSON.stringify(res.data)); } }); }); // emits when any error occurs -> calls closed event immediately after this. // emits when any error occurs -> calls closed event immediately after this. Loading Loading @@ -238,7 +250,7 @@ client.on('data',function(data){ // 87.78.129.86 | localhost // 87.78.129.86 | localhost /*const testClient = net.connect({host: 'localhost', port: 3000}, () => { 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({"mapId":"7fsfRSUCQ7YwfBEdRk_GivW6qzj","userId":"aKBNg9fbReqLedLiz1KFfA","name":"Summer 2021 - 01"})); Loading Loading
helpFunctions.js +11 −4 Original line number Original line Diff line number Diff line Loading @@ -31,12 +31,19 @@ function sendRequest(url, json, callback) { }); }); } } /*function addMapToClient(clintList, client, mapId) { function addMapToClient(clientList, currentClient, mapId, callback) { const clientId = client.name; clientList.forEach(client => { }*/ if (client.name === currentClient.name) { client.mapId = mapId; console.log("Client Map ID found"); } }); return callback(clientList); } module.exports = { module.exports = { isJsonString, isJsonString, checkIfRequestHasVote, checkIfRequestHasVote, sendRequest sendRequest, addMapToClient }; };
socket.js +19 −7 Original line number Original line Diff line number Diff line Loading @@ -15,6 +15,7 @@ server.on('close',function(){ server.on('connection',function(socket){ server.on('connection',function(socket){ // Identify this client // Identify this client socket.name = socket.remoteAddress + ":" + socket.remotePort; socket.name = socket.remoteAddress + ":" + socket.remotePort; socket.mapId = "null"; clients.push(socket); clients.push(socket); // Send a nice welcome message and announce // Send a nice welcome message and announce Loading Loading @@ -68,10 +69,6 @@ server.on('connection',function(socket){ }); }); socket.on('data',function(data){ socket.on('data',function(data){ var bread = socket.bytesRead; var bwrite = socket.bytesWritten; console.log('Bytes read : ' + bread); console.log('Bytes written : ' + bwrite); console.log('Data sent to server : ' + data); console.log('Data sent to server : ' + data); if(hf.isJsonString(data)) { if(hf.isJsonString(data)) { Loading @@ -83,12 +80,17 @@ server.on('connection',function(socket){ } } hf.sendRequest(url, data, res => { hf.sendRequest(url, data, res => { if (res) { if (res) { var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n'); const req = JSON.parse(data); hf.addMapToClient(clients, socket, req['mapId'], newClientList => { clients = newClientList.slice(); sendToAllClientsAtSameMap(res, req['mapId']); }); /*var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n'); 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(); } }*/ } else { } else { console.log("Res was null"); console.log("Res was null"); } } Loading Loading @@ -144,6 +146,16 @@ server.on('connection',function(socket){ process.stdout.write(message) process.stdout.write(message) } } // Send a message to all clients playing the same map function sendToAllClientsAtSameMap(res, mapId) { console.log("JSON: ", JSON.stringify(res.data)); clients.forEach(function (client) { // Don't want to send it to sender if (client.mapId === mapId) client.write(JSON.stringify(res.data) + '\n'); }); console.log("Write Message to all at " + mapId +": " + JSON.stringify(res.data)); } }); }); // emits when any error occurs -> calls closed event immediately after this. // emits when any error occurs -> calls closed event immediately after this. Loading Loading @@ -238,7 +250,7 @@ client.on('data',function(data){ // 87.78.129.86 | localhost // 87.78.129.86 | localhost /*const testClient = net.connect({host: 'localhost', port: 3000}, () => { 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({"mapId":"7fsfRSUCQ7YwfBEdRk_GivW6qzj","userId":"aKBNg9fbReqLedLiz1KFfA","name":"Summer 2021 - 01"})); Loading