简体   繁体   English

使用websockets在聊天中链接名称

[英]link names in chat using websockets

I am trying to solve this problem where I am using chat tutorial http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket . 我正在尝试使用聊天教程http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket来解决此问题。 I am attempting to create anchors that that will go to each users profile if clicked on. 我正在尝试创建锚点,如果单击该锚点,它将转到每个用户个人资料。 The problem is that during testing with multiple users the links only direct to the very first user in the room. 问题在于,在与多个用户进行测试期间,链接仅直接指向会议室中的第一个用户。 I have spent a fair amount of time trying to figure this out. 我花了很多时间试图解决这个问题。 How do I get separate clickable links? 如何获得单独的可点击链接? I am using CodeIgniter with links processed by a controller. 我将CodeIgniter与控制器处理的链接一起使用。 I cannot fathom how I would perform any loop or while statements in this structure. 我无法理解如何在此结构中执行任何循环或while语句。

Javascript with HTML HTML HTML

websocket.onmessage = function(ev) {
    var msg = JSON.parse(ev.data); //PHP sends Json data
    var type = msg.type; //message type
    var umsg = msg.message; //message text
    var uname = msg.name; //user name
    var ucolor = msg.color; //color

    if(type == 'usermsg') 
    {
        $('#message_box').append("<div><span class=\'user_name\'       style=\'color:#'+ucolor+'\"><button type='button' class='greenButton' onclick='proceed()' id='mem_name' value='"+uname+"'>"+uname+"</button></span> : <span class=\"user_message\">"+umsg+" </span></div>" );

    }`

JS Function JS功能

function proceed (button) {
    var mem_name = $('#mem_name').val();
    var form = document.createElement('form');
    form.setAttribute('method', 'post');
    form.setAttribute('action', 'member_chat');
    form.style.display = 'hidden';
    form.setAttribute("target", "formresult");

    var hiddenField = document.createElement("input");      
    hiddenField.setAttribute('name', 'mem_chat');
    hiddenField.setAttribute('value', mem_name)
    form.appendChild(hiddenField);
    document.body.appendChild(form)
    form.submit();
}

At a high level, your issue is that all your inserted buttons have the same id, mem_name . mem_name ,您的问题是所有插入的按钮都具有相同的ID,即mem_name IDs must be unique, so $("#mem_name") stops after finding the first mafch. ID必须是唯一的,因此$("#mem_name")在找到第一个mafch之后停止。

The quick fix here is to pass this in your inline click listener: 此处的快速解决方案是在您的嵌入式点击监听器中传递this

onclick="proceed(this)"

And use $(button) (ie, the parameter to proceed ) instead of $("#mem_name") . 并使用$(button) (即,要proceed的参数)代替$("#mem_name")

A more elegant solution would be to eliminate inline listeners at all and use jQuery's .on delegation. 一个更优雅的解决方案是完全消除内联侦听器,并使用jQuery的.on委托。 Change the id=mem_name to class=mem_name , eliminate the onclick listener, and add this code to run once only (ie, not inside a message event listener): id=mem_name更改为class=mem_name ,消除onclick侦听器,并添加以下代码使其运行一次 (即,不在消息事件侦听器内部运行):

$("#message_box").on("click", ".mem_name", proceed);

Then change the $("#mem_name") to $(this) . 然后将$("#mem_name")更改$(this) This will cause proceed to run any time an element with a mem_name class is clicked inside of #message_box . 这会造成proceed运行一个元素任何时候mem_name点击类的内部#message_box

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM