信号R2不工作

本文关键字:工作 R2 信号 | 更新日期: 2023-09-27 18:28:54

所以我的聊天应用程序在asp-mvc5中创建时没有任何反应。根据chrome控制台,我没有得到任何javascript错误。

在我的"配置"方法中的"启动"中得到了这一行。

app.MapSignalR();

ChatHub类

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

聊天视图,下面的第一行,运行良好,我在源代码中看到了它。最后一行js甚至没有被执行,因为文本框没有被清除。

<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>

编辑:我的视图有问题,我创建了一个新的html页面,并从这里粘贴了所有htmlhttp://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr它奏效了。仍然不知道我的View 中的问题是什么

信号R2不工作

这可能是有史以来最糟糕的答案,但我做到了。我不知道怎么写的,我只是重写了所有的东西,现在它起作用了

新聊天视图

<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>