私はこの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 });
ユーザーが自分の名前を見ることができないようにするために「ブロードキャスト」を使用する必要はありませんか?あなたのコードはどこに追加されるべきですか? – JonasSH
クライアント側。 $( "#people")$( "#people")を置き換えてください。(..... – resu9561
私は試してみました:socket.on( "update-people"、function(data){ $( "#people" { return; } else { $( '#people(client.id).name == obj.name) ').append( "