我正在使用此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){
res.sendFile(__dirname + '/index.html');
})
// Gets the css file
app.use(express.static(path.join(__dirname, 'public')));
// When connecting
socket.on("connection", function(client) {
client.on("join", function(name){
client.emit("update-people", {people: people});
sizePeople = _.size(people);
socket.sockets.emit("update-peopleCount", {people: people, count: sizePeople});
var ownerRoomID = inRoomID = null;
roomID = null;
conversationID = null; // This line probably has to go, since users should be able to create several conversations.
people[client.id] = {"name" : name, "owns" : ownerRoomID, "inroom" : inRoomID, "id" : client.id, "room" : roomID, "conversation" : conversationID};
var id = uuid.v4();
//sizePeople = _.size(people);
sizeRooms = _.size(rooms);
sizeConversations = _.size(conversations);
//socket.sockets.emit("update-peopleCount", {people: people, count: sizePeople});
//socket.sockets.emit("update-people", {people: people});
socket.sockets.emit("roomList", {rooms: rooms, count: sizeRooms});
socket.sockets.emit("update-conversations", {conversations: conversations, count: sizeConversations});
client.emit("updateToSelf", "You have connected to the server. Start conversation or create/join room to chat");
client.broadcast.emit('updateToOthers', name + " is online.");
clients.push(client); //populates the clients array with the client object
console.log("Someone joined the chat", people[client.id].id);
//client.broadcast.emit("sendingOwnName", people[client.id].id);
client.broadcast.emit("update-people-withSelf", people[client.id]);
});
答案 0 :(得分:0)
如果这些人分配了一些ID,那么你可以拥有
循环中的条件不会追加用户自己。
if(obj.id !== currentUser.id) { $'#people').append(... }
当然,您也可以使用用户名或其他信息。
答案 1 :(得分:0)
我找到了解决问题的方法。我用正确的代码更新了我的问题。 基本上需要的是在为新用户提供id,名称等之前更新用户。之后,该新用户将他/她的名字广播给其他用户。