Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting list of clients using WebSockets
    primarykey
    data
    text
    <p>I'm building a multiuser sketchpad and having some issues getting and keeping an updated list of all the currently connected users. I'm trying to find the best way to notify all existing clients of a new client, and passing the existing clients to the newly connected one. The reason I'm trying to do this is so when one client draws a line, all the other clients find that client in the list, and add the line coords to the respective array. </p> <p>Server.js</p> <pre><code>var sys = require("sys"); var ws = require('websocket-server'); var server = ws.createServer({debug: true}); server.addListener("connection", function(conn) { // When a message is received, broadcast it too all the clients // that are currently connected. conn.send('NEW_SERVER_MESSAGE:Welcome!'); conn.send('USER_CONNECTED:' + conn.id); conn.addListener("message", function( msg ){ switch(msg) { case 'GET_ALL_ACTIVE_USERS': server.manager.forEach(function(con) { conn.broadcast('USER_CONNECTED:' + con.id); }); break; case 'UPDATE_CURSOR': conn.broadcast( msg + '_' + conn.id ); break; default: conn.broadcast( msg ); break; } }); conn.addListener('close', function( msg ) { conn.broadcast('USER_DISCONNECTED:' + conn.id); }); }); server.addListener("error", function(){ console.log(Array.prototype.join.call(arguments, ", ")); }); server.addListener("listening", function(){ console.log("Listening for connections..."); }); server.addListener("disconnected", function(conn){ console.log("&lt;"+conn.id+"&gt; disconnected."); }); server.listen(8002);&lt;/code&gt; </code></pre> <p>My JS network code:</p> <pre><code>sketchPadNet = function (b,s) { var self = this; this.host = "ws://localhost:8002/server.js"; this.id = null; this.users = []; this.heightOffset = 53; this.scene = s; this.connected = b; var User = function(id) { this.id = id; this.nickname = id; this.lines = []; this.position = ""; }; this.init = function () { try { socket = new WebSocket(self.host); socket.onopen = function (msg) { socket.send('GET_ALL_ACTIVE_USERS'); }; socket.onmessage = function (msg) { var m = msg.data.split(':'); switch(m[0]) { case 'USER_CONNECTED': console.log('USER_CONNECTED'); var u = new User(m[1]); u.lines = []; u.nickname = u.id; self.users.push(u); console.log(self.users.length); console.log(self.users); break; case 'USER_DISCONNECTED': console.log('USER_DISCONNECTED'); for(var i = 0; i &lt; self.users.length; ++i) { console.log(self.users[i]); if(m[1] == self.users[i].id) { self.users.splice(i, 1); } } break; case 'DRAW_LINE': var tmpMsg = m[1].split('_'); var id = tmpMsg[8]; var userIndex = ''; for(var i = 0; i &lt; self.users.length; ++i) { if(self.users[i].id == id) userIndex = i; } var p1 = (self.users[userIndex].lines.length &lt; 1) ? new BLUR.Vertex3(tmpMsg[0], tmpMsg[1], tmpMsg[2]) : self.users[userIndex].lines[self.users[userIndex].lines.length - 1].point2; var p2 = new BLUR.Vertex3(tmpMsg[3], tmpMsg[4], tmpMsg[5]); var line = new BLUR.Line3D(p1, p2, tmpMsg[7]); line.material = tmpMsg[6]; // add the newly created line to the scene. self.scene.addObject(line); self.users[userIndex].lines.push(line); break; case 'UPDATE_CURSOR': for(var i = 0; i &lt; self.scene.objects.length; ++i) { if(self.scene.objects[i].type == 'BLUR.Particle') self.scene.removeObject(scene.objects[i]); } var coords = m[1].split('_'); var id = coords[2]; var userIndex = 0; for(var i = 0; i &lt; self.users.length; ++i) { if(self.users[i].id == id) userIndex = i; } self.users[userIndex].position = new BLUR.Vertex3(coords[0], coords[1], 1); var p = new BLUR.Particle( self.users[userIndex].position, 4 ); p.material = new BLUR.RGBColour(176,23,31,0.3); self.scene.addObject(p); break; case 'RECEIVE_ID': self.id = m[1]; self.connected.nicknameObj.text = self.id; console.log(self.id); break; break; } }; socket.onclose = function (msg) { // feck! self.connected.addServerError('Server disconnected, try refreshing.'); }; } catch (ex) { console.log('EXCEPTION: ' + ex); } }; </code></pre> <p>At the moment, when a new client connected it sends out a message and is added to an array, the the reverse when a client disconnects. For some reason though this isn't staying update? For example when a client draws a line, some of the others report this error: </p> <pre><code>Uncaught TypeError: Cannot read property 'lines' of undefined </code></pre> <p>Sorry if i'm being incredibly stupid here, but any help is appreciated!! </p> <p>Cheers :) </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.
 

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