简体   繁体   中英

SignalR 2 Not working

So I get no reaction what so ever from my chatapp created in asp mvc 5. I dont get any javascript errors according to chrome Console.

Got this line in my Startup in the Configuration method.

app.MapSignalR();

ChatHub class

public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
    }

Chat view, The first line here below, works fine, ive seen it in the source. The last js line doesnt even get executed cause the textbox doesnt get cleared.

<input type="hidden" id="displayname" value="@User.Identity.GetUserName()" />
 <ul id="realChat" class="chat">

        <!-- MESSAGES GO HERE -->

    </ul>

    <!-- MESSAGE -->
    <input id="btn-input" type="text" class="form-control input-sm" placeholder="Type your message here..." />
    <span class="input-group-btn">

        <!-- SEND BUTTON -->
        <button class="btn btn-warning btn-sm" id="btn-chat">Send</button>
    </span>

    <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <script src="~/signalr/hubs"></script>

    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = text(name).html();
                var encodedMsg = text(message).html();
                // Add the message to the page.
                $('#realChat').append(" <li class='clearfix'><div class='chat-body clearfix'> <strong style='color: red' class='primary-font'>"+encodedName+"</strong>:<span>"+encodedMsg+"Ls</span></div></li>");
            };
            // Set initial focus to message input box.
            $('#btn-input').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#btn-chat').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#btn-input').val());
                    // Clear text box and reset focus for next comment.
                    $('#btn-input').val('').focus();
                });
            });
        });
    </script>

Edit: Something's wrong in my view, i created a new html page and pasted all html from here http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr and it worked. Still dont know what the problem is in my View

This will probably be the worst answer ever, but I got it to work. I dont know how, I just rewrote everything and it works now

new chat view

<input type="hidden" id="displayname" value="@User.Identity.GetUserName()" />
<div class="container">
    <div class="row">
        <div class="col-md-5">
            <div class="panel panel-primary">
                <div class="panel-body">
                    <ul id="discussion" class="chat">


                    </ul>
                </div>
                <div class="panel-footer">
                    <div class="input-group">
                        <input id="message" type="text" class="form-control input-sm" placeholder="Type your message here..." />
                        <span class="input-group-btn">
                            <button class="btn btn-warning btn-sm" id="sendmessage">
                                Send
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>

<script type="text/javascript">
$(function() {
    // Declare a proxy to reference the hub.
    var chat = $.connection.chatHub;
    // Create a function that the hub can call to broadcast messages.
    chat.client.broadcastMessage = function(name, message) {
        // Html encode display name and message.
        var encodedName = $('<div />').text(name).html();
        var encodedMsg = $('<div />').text(message).html();
        // Add the message to the page.
        $('#discussion').append("<li class='clearfix'><div class='chat-body clearfix'><span class='time'>[05:08:28]</span><strong style='color: red' class='primary-font'> "+encodedName+"</strong>:"
            + '</strong><span>' + encodedMsg + '</span></li>');
    };
    // Get the user name and store it to prepend to messages.
    // 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();
        });
    });
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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