2017-04-24 36 views
0

我正在使用此guide建立一个简单的聊天。如何让用户在套接字聊天中的用户列表中看不到自己的名字

当某人登录时,他们的姓名被追加到在线用户列表中。但是,每个用户不应该在列表中看到他/她自己的名字,而只能看到其他用户的名字。任何建议,我应该在我的代码中添加/更改以解决此问题?

客户端:

socket.on("update-people", function(data, id){ 
 
      $("#people").empty(); 
 
      $.each(data.people, function(a, obj, id) { 
 
       $('#people').append("<li class=\"people-item\"><a href=\"#\" class=\"list-group-item\"><span class="+obj.id+">" + obj.name + "</span></a></li>"); 
 
      }); 
 
     }); 
 

 
     socket.on("update-people-withSelf", function(people){ 
 
      $('#people').append("<li class=\"people-item\"><a href=\"#\" class=\"list-group-item\"><span class="+people.id+">" + people.name + "</span></a></li>"); 
 
     });

服务器端:

// Setting up the server 
 
var express = require('express'); 
 
var app = express(); 
 
var path = require('path'); 
 
var server = require('http').createServer(app); 
 
var socket = require("socket.io").listen(server); 
 
var Room = require('./room.js'); 
 
var Conversation = require('./conversation.js'); 
 
var _ = require('underscore')._; 
 
var uuid = require ('uuid'); 
 
server.listen(process.env.PORT || 3000); 
 
console.log('Server is running...'); 
 

 
socket.set("log level", 1); 
 
var people = {}; 
 
var rooms = {}; 
 
var conversations = {}; 
 
var clients = []; 
 
var chatHistory = {}; 
 

 
Array.prototype.contains = function(k, callback) { 
 
    var self = this; 
 
    return (function check(i) { 
 
     if (i >= self.length) { 
 
      return callback(false); 
 
     } 
 
     if (self[i] === k) { 
 
      return callback(true); 
 
     } 
 
     return process.nextTick(check.bind(null, i+1)); 
 
    }(0)); 
 
}; 
 

 

 

 
// Gets the html file 
 
app.get('/', function(req, res){ 
 
\t res.sendFile(__dirname + '/index.html'); 
 
}) 
 

 
// Gets the css file 
 
app.use(express.static(path.join(__dirname, 'public'))); 
 

 
// When connecting 
 
socket.on("connection", function(client) { 
 
\t client.on("join", function(name){ 
 
\t \t client.emit("update-people", {people: people}); 
 
\t \t sizePeople = _.size(people); 
 
\t \t socket.sockets.emit("update-peopleCount", {people: people, count: sizePeople}); 
 
\t \t var ownerRoomID = inRoomID = null; 
 
\t \t roomID = null; 
 
\t \t conversationID = null; // This line probably has to go, since users should be able to create several conversations. 
 
\t \t people[client.id] = {"name" : name, "owns" : ownerRoomID, "inroom" : inRoomID, "id" : client.id, "room" : roomID, "conversation" : conversationID}; 
 
\t \t var id = uuid.v4(); 
 
\t \t //sizePeople = _.size(people); 
 
\t \t sizeRooms = _.size(rooms); 
 
\t \t sizeConversations = _.size(conversations); 
 
\t \t //socket.sockets.emit("update-peopleCount", {people: people, count: sizePeople}); 
 
\t \t //socket.sockets.emit("update-people", {people: people}); 
 
\t \t socket.sockets.emit("roomList", {rooms: rooms, count: sizeRooms}); 
 
\t \t socket.sockets.emit("update-conversations", {conversations: conversations, count: sizeConversations}); 
 
\t \t client.emit("updateToSelf", "You have connected to the server. Start conversation or create/join room to chat"); 
 
\t \t client.broadcast.emit('updateToOthers', name + " is online."); 
 
\t \t clients.push(client); //populates the clients array with the client object 
 
\t \t console.log("Someone joined the chat", people[client.id].id); 
 
\t \t //client.broadcast.emit("sendingOwnName", people[client.id].id); 
 
\t \t client.broadcast.emit("update-people-withSelf", people[client.id]); 
 
\t });

回答

0

我找到了解决我的问题的方法。我用正确的代码更新了我的问题。 基本上所需要的是用户更新之前,新用户被给予一个ID,名称等。之后,该新用户将他/她的名字广播给其他用户。

0

如果这些人有一定的ID分配给他们的ñ你可以在你的循环中有 条件,不要追加用户他/她自己。 if(obj.id !== currentUser.id) { $'#people').append(... } 当然你也可以使用用户名或其他信息。

+0

我不需要使用'广播'来确保用户看不到自己的名字吗?你的代码应该放在哪里? – JonasSH

+0

客户端。替换$('#people')。append(..... – resu9561

+0

我试过:socket.on(“update-people”,function(data){(“#people”)。empty() ; $ .each(data.people,function(a,obj,id)if(people [client.id] .name == obj.name){ return; } else { $('#people ').append(“

  • " + obj.name + "
  • ”); } }); });但不起作用 – JonasSH

    相关问题