Search code examples
asp.netasp.net-mvc-5

SignalR Chat asp.net between user and editor


How can I make it work for the current user and any editor user account? Even if I am logged on a normal user and on incognito on one of the editor accounts, it shows like I am speaking with myself because it enters on the name != Admin.

<script>
    $(function () {
        // Reference the auto-generated proxy for the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call back to display messages.
        chat.client.addNewMessageToPage = function (name, message) {
            if (name == "Admin") {
                // Add the message to the page.
                $('#discussion').append('<p style="color:green; text-align:left; width:500px"><strong><img = src="https://www.phplivesupport.com/pics/icons/avatars/public/avatar_7.png" title="Admin">'
                    + ' </strong> ' + htmlEncode(message) + '</p>');
            }
            else if (name != "Admin") {
                // Add the message to the page.
                $('#discussion').append('<p style="color:blue;text-align:right;"><strong><img = src="https://www.phplivesupport.com/pics/icons/avatars/public/avatar_71.png" title="Peter">'
                    + ' </strong> ' + htmlEncode(message) + '</p>');
            }
        };
        // Get the user name and store it to prepend to messages.
        var currentMember= '@Html.Raw(@ViewBag.Name)';
        alert(currentMember);
        $('#displayname').val(currentMember);
        //$('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
    // This optional function html-encodes messages for display in the page.
    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    }
</script>

Solution

  • That's how it worked.

     <script>
            $(function () {
                // Reference the auto-generated proxy for the hub.
                var chat = $.connection.chatHub;
                // Create a function that the hub can call back to display messages.
                chat.client.addNewMessageToPage = function (name, message) {
                    if (name == "true") {
                        // Add the message to the page.
                        $('#discussion').append('<p style="text-align:left; text-shadow: 2px 2px 3px red"><strong><img = src="/Images/admin_icon.png" title="Admin">'
                            + ' </strong> ' + htmlEncode(message) + '</p>');
                    }
                    else if (name != "true") {
                        // Add the message to the page.
                        $('#discussion').append('<p style="text-align:right;text-shadow: 2px 2px 3px teal;"><strong>'
                            + ' </strong> ' + htmlEncode(message) + '<img = src="/Images/client_icon.png" title="Client">' + '</p>');
                    }
                };
                // Get the user name and store it to prepend to messages.
                var currentMember= '@(User.IsInRole("Administrator") ? "true" : "false")';
                //alert(currentMember);
                $('#displayname').val(currentMember);
                //$('#displayname').val(prompt('Enter your name:', ''));
                // Set initial focus to message input box.
                $('#message').focus();
                // Start the connection.
                $.connection.hub.start().done(function () {
                    $('#sendmessage').click(function () {
                        // Call the Send method on the hub.
                        chat.server.send($('#displayname').val(), $('#message').val());
                        // Clear text box and reset focus for next comment.
                        $('#message').val('').focus();
                    });
                });
            });
            // This optional function html-encodes messages for display in the page.
            function htmlEncode(value) {
                var encodedValue = $('<div />').text(value).html();
                return encodedValue;
            }
        </script>