Commit 6d94378e authored by Dennis Willers's avatar Dennis Willers 🏀

try multiplayer

parent ab49d720
Pipeline #419 passed with stages
in 2 minutes and 49 seconds
...@@ -4,6 +4,8 @@ const axios = require('axios'); ...@@ -4,6 +4,8 @@ const axios = require('axios');
// creates the server // creates the server
var server = net.createServer(); var server = net.createServer();
var clients = [];
//emitted when server closes ...not emitted until all connections closes. //emitted when server closes ...not emitted until all connections closes.
server.on('close',function(){ server.on('close',function(){
console.log('Server closed !'); console.log('Server closed !');
...@@ -11,6 +13,13 @@ server.on('close',function(){ ...@@ -11,6 +13,13 @@ server.on('close',function(){
// emitted when new client connects // emitted when new client connects
server.on('connection',function(socket){ server.on('connection',function(socket){
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort;
clients.push(socket);
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the chat\n", socket);
//this property shows the number of characters currently buffered to be written. (Number of characters is approximately equal to the number of bytes to be written, but the buffer may contain strings, and the strings are lazily encoded, so the exact number of bytes is not known.) //this property shows the number of characters currently buffered to be written. (Number of characters is approximately equal to the number of bytes to be written, but the buffer may contain strings, and the strings are lazily encoded, so the exact number of bytes is not known.)
//Users who experience large or growing bufferSize should attempt to "throttle" the data flows in their program with pause() and resume(). //Users who experience large or growing bufferSize should attempt to "throttle" the data flows in their program with pause() and resume().
...@@ -122,6 +131,17 @@ server.on('connection',function(socket){ ...@@ -122,6 +131,17 @@ server.on('connection',function(socket){
socket.destroy(); socket.destroy();
},1200000); },1200000);
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}
}); });
// emits when any error occurs -> calls closed event immediately after this. // emits when any error occurs -> calls closed event immediately after this.
...@@ -216,17 +236,17 @@ client.on('data',function(data){ ...@@ -216,17 +236,17 @@ client.on('data',function(data){
// 87.78.129.86 // 87.78.129.86
const clients = net.connect({host: 'localhost', port: 3000}, () => { const testClient = net.connect({host: 'localhost', port: 3000}, () => {
console.log('connected to server!'); console.log('connected to server!');
clients.write('Hello World Client!\r\n'); testClient.write('Hello World Client!\r\n');
//test(clients); //test(testClient);
}); });
clients.on('data', (data) => { testClient.on('data', (data) => {
console.log(data.toString()); console.log(data.toString());
//clients.end(); testClient.end();
}); });
/*clients.on('end', () => { /*testClient.on('end', () => {
console.log('disconnected from server'); console.log('disconnected from server');
}); });
//*/ //*/
......
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