【问题标题】:SignalR Error: signalR(...).starting(...).sending is not a functionSignalR 错误:signalR(...).starting(...).sending 不是函数
【发布时间】:2016-02-26 17:44:51
【问题描述】:

我正在从链接 Asp.Net Signal MVC4 旁边学习实现 SignalR 的基础知识。就像教程建议的那样,我已经实现了一个演示应用程序,但我无法解决以下错误。

未捕获的类型错误:signalR(...).starting(...).sending 不是 功能

这是我迄今为止尝试过的。

先决条件:

  1. Microsoft.AspNet.SignalR
  2. WebAPIDoodle.SignalR

集线器

namespace SignalRChat.Hubs
{        
    public class ChatHub : Hub
    {
        public void Send( string name, string message )
        {
            Clients.All.addNewMessageToPage( name, message );
        }
    }
}

Owin 启动

[assembly: OwinStartup( typeof( SignalRChat.Startup ) )]

namespace SignalRChat
{
    public class Startup
    {
        public void Configuration( IAppBuilder app )
        {
            app.MapSignalR();
        }
    }
}

控制器

public ActionResult Chat()
{
    return View();
}

查看(聊天)

@{
    ViewBag.Title = "Chat";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Chat</h2>
<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="discussion"></ul>
</div>
@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    @*<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>*@
    <script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.0.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script type="text/javascript">
        $(function () {

            var connection = $.connection('/echo');
            console.log(connection);
            // 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) {
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + htmlEncode(name)
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#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>
}

我无法解决以下错误。有什么我在这里遗漏的,因为我已经安装了所有必需的文件并使用了最新的 Jquery。我尝试使用较低版本的 signalR,但同样的问题仍然存在。

这里是带有错误列表的开发者控制台快照。

错误来源

【问题讨论】:

    标签: javascript c# asp.net-mvc-4 signalr


    【解决方案1】:

    我终于解决了我在问题中提到的异常。实际上我不必要地安装了 WebAPIDoodle.SignalR dll。我已经转储了这个项目,并在新项目设置完成后立即开始一个新项目,我去了包控制台管理器,并一一更新了以下 dll。

    1. Microsoft.AspNet.SignalR
    2. Microsoft.Owin
    3. Microsoft.Owin.Security
    4. Microsoft.Owin.Host.SystemWeb

    Rest 过程与教程中建议的创建 owin 启动类和集线器类相同。就像教程中提到的那样,应用程序运行得很好。我注意到的是两个项目信号器自动生成的 javascript 都不同我猜这是因为以前我不必要地安装了 WebAPIDoodle.SignalR dll,这给了我不同的 javascript。

    【讨论】:

      【解决方案2】:

      你应该使用'send'而不是'sending',因为在文件'charthub.cs'中你已经声明'public void send (...)'javascriptmyhub.cs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-18
        • 2013-11-26
        • 1970-01-01
        相关资源
        最近更新 更多