locked
call chatub in <a> hyperference uisng signal R RRS feed

  • Question

  • User81789783 posted

    can we call signal R client method in <a href=#>text</a>

    here is client signal R methods

     function registerClientMethods(chat) {
                    chat.client.notify = function (message) {
                        alert("notify")
    
                        if (message && message.toLowerCase() == "added") {
    
                            updateNotificationCount(chat);
                        }
                    }
    }
           function updateNotificationCount(chat) {
               
                    value = $("#connlbl").text();
                   // alert("value: " + value)
                    $.ajax({
                        type: "POST",
                        url: "/LetsChat/GetOnlineusers?connid=" + value,
                  
                        contentType: "application/json; charset=utf-8",
                   
                        dataType: "json",
                        success: function (response) {
                            debugger;
                            $('#notiContent').empty();
                           
                            if (response.length == 0) {
                                $('#notiContent').append($('<li>Currently You Have No New users.</li>'));
                            }
    
                            for (var i = 0, len = response.length; i < len; i++) {
                 
                              
                                $('#notiContent').append('<strong>' + (i + 1) + "<a onclick='chat.client.getValue(\"" + response[i].ContextID + "\",\"" + response[i].Name + "\");' href='#'>" + response[i].Name + "</a><br />");
                             
    
                            }
                            $.each(response, function (index, value) {
                              
                              
                            });
                        },
                        error: errorFunc
                    });
                }

    it gives error over here

    <a onclick='chat.client.getValue(\"" + response[i].ContextID

    see below error

    ReferenceError: chat is not definedLetsChat:1:1

        onclick http://localhost:55590/LetsChat/LetsChat#:1

    Sunday, June 2, 2019 1:42 PM

All replies

  • User-474980206 posted

    chat, is a parameter variable passed via closure to the append function. but in the click event, it will be looking for a global variable name chat. once you fix this, you will have issues with the finding the variable passed "+ response[i].ContextID...".

    Sunday, June 2, 2019 8:58 PM
  • User81789783 posted

    well bruce ,pleas note down in updateNotificationCount and getvalue method its showing [object object] .that means its not null . get value call OpenPrivateChatWindow where it also send chat as last parameter as shown in below code

       function OpenPrivateChatWindow(connectionid, userName, chat) {
                var ctrId = 'private_' + connectionid;
                alert("ctrId "  +ctrId)
                alert($('#' + ctrId).length)
                if ($('#' + ctrId).length > 0) return;
    
                createPrivateChatWindow(connectionid, ctrId, userName,chat);
    
                //chatHub.server.getPrivateMessage(userEmail, email, loadMesgCount).done(function (msg) {
                //    for (i = 0; i < msg.length; i++) {
                //        $('#' + ctrId).find('#divMessage').append('<div class="message"><span class="userName">' + msg[i].userName + '</span>: ' + msg[i].message + '</div>');
                //        // set scrollbar
                //        scrollTop(ctrId);
                //    }
                //});
            }

    then above method call function createPrivateChatWindow(userId, ctrId, userName,chat) 

    when retrive value (when getvalue called) .it shows selected value text i.e username in label ,and when i type some thing in text box and click send button it then shows error as below

    TypeError: chat.server is undefined

    LetsChat:193:21
        createPrivateChatWindow http://localhost:55590/LetsChat/LetsChat#:193
        jQuery 2

    TypeError: chat.server is undefinedLetsChat:178:21

        createPrivateChatWindow http://localhost:55590/LetsChat/LetsChat#:178
        jQuery 2

    .

    that means when its all working fine except when i clicked on send button .

    Monday, June 3, 2019 5:33 AM
  • User61956409 posted

    Hi erum,

    <a onclick='chat.client.getValue(\"" + response[i].ContextID

    Normally, we use the client property of the generated proxy to define method that the server can call from a Hub, can you clarify more about your scenario/requirement that requires calling client side function from HTML client side with above code?

    I really recommend that you had better to read this document to know details about SignalR JavaScript clients setup.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#how-to-establish-a-connection

    With Regards,

    Fei Han

    Monday, June 3, 2019 5:40 AM
  • User81789783 posted

    well fei , sorry by mistake yesterday i changed like this but forgot to show u

    for (var i = 0, len = response.length; i < len; i++) {


    $('#notiContent').append('<strong>' + (i + 1) + "<a onclick='getValue(\"" + response[i].ContextID + "\",\"" + response[i].Name + "\",\""+ chat +"\");' href='#'>" + response[i].Name + "</a><br />");

    }

    also if u noted down ,i mentioned it never shows undefine/null in any method ,but i do not know what is the isssue then

    Monday, June 3, 2019 5:54 AM
  • User81789783 posted

    if i check same issue in chrome it says

    Uncaught TypeError: Cannot read property 'sendPrivateMessage' of undefined
    at HTMLInputElement.<anonymous> (LetsChat:180)
    at HTMLInputElement.dispatch (jquery-1.10.2.min.js:22)
    at HTMLInputElement.v.handle (jquery-1.10.2.min.js:22)

    in forefox it says  below (same issue)

    TypeError: chat.server is undefinedLetsChat:180:21

    Monday, June 3, 2019 6:50 AM
  • User61956409 posted

    Hi erum,

    In this thread, we discussed similar issue, and I shared a working sample to achieve the requirement of sending private messages, if you have further questions, please post back in that thread. Thanks for your understanding.

    https://forums.asp.net/t/2156217.aspx 

    With Regards,

    Fei Han

    Tuesday, June 4, 2019 2:20 AM