Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket.io refresh list of connected clients
    primarykey
    data
    text
    <p>At the moment I have a websocket server that generates a uuid for every connection made to it and passes that onto the client, and on the client side I update a simple list with all the connections. I'd like to see the original connaction's list refreshed, let me illustrate what I mean:</p> <ol> <li>open a tab - the first client connects and I see my uuid on the list</li> <li>I open a new tab, connect to the websocket, I see the previously connected ID and the new ID</li> <li>Now, when I go back to tab 1, I don't see both connections, only the first one that was there. How can I refresh this list, in a way such that if a websocket connection made, this list of uuid's is refreshed for all the connected clients.</li> </ol> <p>My code at the moment is the following:</p> <p>server:</p> <pre><code>var http = require("http"), io = require("socket.io"), uuid = require ("node-uuid"); var connectedPlayers = new Array(); var server = http.createServer(function(req, res) { // Send HTML headers and message res.writeHead(200,{ "Content-Type": "text/html" }); res.end("&lt;h1&gt;Server is ready &amp; listening&lt;/h1&gt;"); }); server.listen(1222, "1.1.1.1"); var socket = io.listen(server); socket.set('log level', 0); socket.on("connection", function(client) { client.on("connection", function(){ console.log("Client connected."); }); client.on("disconnect",function(client){ //connected--; console.log("Client disconnected."); }); client.userid = uuid(); console.log(client.userid); connectedPlayers.push(client.userid); client.emit("onconnected", {playersId: connectedPlayers}); }); </code></pre> <p>client:</p> <pre><code>var socket = io.connect("1.1.1.1:1222"); console.log(socket); socket.on("connect",function() { console.log("Client has connected to the server"); }); socket.on("disconnect",function() { console.log("The client has disconnected from the server"); }); socket.on("error", function(message) { console.log("It is not possible to connect to the server: " + message); }); socket.on("onconnected", function(data) { for (var i = 0; i &lt; data.playersId.length; i++) $("#players").append("&lt;li&gt;" + data.playersId[i] + "&lt;/li&gt;"); }); </code></pre> <p>(and of course: <code>&lt;ul id="players"&gt;&lt;/ul&gt;</code>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload