当其他用户点击他们的名字时,如何将用户“拉”进房间?

时间:2017-05-02 07:00:18

标签: javascript jquery node.js sockets socket.io

我正在构建一个套接字聊天,现在我正在尝试添加一个功能,使用户能够从列表中单击另一个在线用户,从而启动对话(房间),这对其他用户是私有/不可见的。 它应该与Facebook的聊天功能基本相同,您可以点击朋友并开始发短信。

但是,我很难理解它是如何工作的,因为“点击”用户不会自愿加入对话。

下面你可以看到我的代码。我希望有人可以指出我所犯的任何错误或我遗漏的错误。

更新,“点击”用户端正在接收消息,但消息不会反过来。

第1步,客户端:用户点击在线用户的名称

//Starting conversation
$("#people").on('click', '.list-group-item', function () {
    var peopleName = $(this).children("span").text();
    var peopleID = $(this).children("span").attr("class");
    //this har INTET id! Hvordan tilføjer jeg det til update-people, når user logger ind?
    socket.emit("serverCreateConversation", peopleName, peopleID);
    $("#msg").prop("readonly", false);
    $("#msg").attr("placeholder", "Your message");
    $("#send").attr("disabled", false);
    $("#chat").empty();
});

第2步,服务器端:创建对话,并将“点击”用户拉入对话。

// Creating conversation
	client.on("serverCreateConversation", function(conversationReceiver, id) {
		console.log("About to create conversation with:", conversationReceiver, id);
		var key = uuid.v4();
		conversationStarter = people[client.id].name;
		var conversation = new Conversation(conversationReceiver, key, conversationStarter);
		conversations[key] = conversation;
		sizeConversations = _.size(conversations);
		client.emit("update-conversations", {conversations: conversations, count: sizeConversations});
		client.to(id).emit("update-conversationsForReceiver", {conversations: conversations, count: sizeConversations});
		client.conversation = conversation.key;
		client.join(client.conversation);
		//conversation.addPerson(client.id);
		conversation.addPerson(id);
		people[client.id].conversation = key;
		//chatHistoryInConversation[client.conversation] = [];
		console.log("Created conversation", conversation.people);
		client.to(id).emit("pullReceiverIntoConversation", conversation); //Pull conversationReceiver into conversation
		client.emit("addConversationForSelf", conversationReceiver);
		client.to(id).emit("addConversationForOthers", conversationStarter);
	});

第3步,客户端:'点击'用户'再次从服务器和联系人服务器接收对话。

//Pulling conversationReceiver into conversation
socket.on("pullReceiverIntoConversation", function (conversation) {
    socket.emit("assigningConversationKeyToReceiver", conversation);
});

第4步,服务器端:会话密钥被分配给“点击”用户。

//Assigning conversation key to receiver
	client.on("assigningConversationKeyToReceiver", function(conversation){
		people[client.id].conversation = conversation.key;
		client.join(client.conversation);
		console.log("Pulled", people);
	});

0 个答案:

没有答案