javascript - How can i create the collection of following data structure and send using socket.io? -
I use node.js and socket.io.
This my data structure is in mysql table: ABC3, Name: 'ABC3', TUUSERID: 1 ID: 2, Name: 'ABC 2', Touuser ID: 1 ID: 3, Name: 'ABC3' ToUserId: 1 ID: 4, Name: 'ABC4', TUUSERID: 2 ID: 5, Name: 'ABC5', Tuuseir ID: 2 ID: 6, Name: 'ABC 6', TUUSUCID ID: 2 ID : 7, Name: With ABC 7 UserID1 and Rows (ID4 - ID6), userID2 and user with a user ID as a collection (i.e. ID-3) Want to send as a collection, TUUSERID: 3
in line with User ID 3 (ID7).
How do I create a collection and send a collection to the user?
I read the data as follows:
getUsers: function (callback) {client.query (select 'user from', select function (mistake, result, field) {If (err) {fail err;} var userslist = {}; if (Results.length & gt; 0) {for (var i = 0; i & lt; results.length; i ++) {var row = Create a collision in the results [i]; // object loglist ific user ------ v //io.sockets.socket(USER_ID).emit('sendusers', userslist)}} and {userslist = {};} Callback (userslist);}); }
You have to store the association between the socket id and the user id.
On your client, you have to emit the current user's USER_ID.
Something like this
socket = io.connect (); Socket.on ('connect', function (data) {socket.emit ('init', user_id);}); and you have to set user_id on your server. You can do something like
io.sockets.on ('connection', function (socket) {Socket.on ('init', function (data) {socket.set ('user_id', data, function () {console.log ('user_id', data);});});}); Then you can retrieve all those sockets and loops in your controller.
// var sockets = io.sockets.clients () to get all connections; / / Socket socket.get ('user_id', to get user_id related to function (mistake, user_id), // // whatever you want, // pre: if (user_id == ...) { Socket.emit ('channel', 'value');}}); Hope that helps!
Comments
Post a Comment