0

I've been struggling with this for the past few days, I've been looking for a way to send data to every connected client to my UDP node.js server.

var dgram = require("dgram");
var port = 3000;

var server = dgram.createSocket("udp4");


var sockets = [];
var players = [];
playerID = 0; 
count = 0;
server.on("message",function(msg, rinfo)
{
    console.log("data: " + String(msg));
    data = String(msg);
    if (data.includes("Client:Connected")){
        count = 0; 
        playerID+=1;

        rinfo.ID = playerID;
        players.push(rinfo.ID);
        sockets.push(rinfo.port, rinfo.address);   
        console.table(sockets);
                                                           
        console.log(rinfo.address + " has connected with ID " + playerID);
        

        players.forEach(() => {
            for (var i = 0; i < sockets.length; i += 1) {
                server.send("Player:Connected" + rinfo.ID, sockets[i], sockets[i]);
            }
        });
           
              
    }
});

server.bind(port);

This is the current code, as you can see I attempted to use a for loop with arrays to store each individual client but that doesn't work, any help and suggestions would be greatly appreciated!

I attempted to use a for loop with arrays to store each individual client but that doesn't work

0 Answers0