SignalR + 未捕获类型错误:对象 #<Object> 没有方法"发送"



我以前使用的是较旧的 signalR 版本.js一切都很好,除了它间歇性地导致我的页面挂起,因此我想用从 SignalR github 站点下载的较新版本进行测试。

我尝试遵循 SignalR 的客户端示例,但在检查 chrome 中的元素时出现此错误,

未捕获的类型错误:对象 # 没有方法"发送"。有人遇到过此错误吗?

  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script> 
  <script src="/Scripts/jquery.signalR.js" type="text/javascript"> </script>    
  <script src="signalr/hubs" type="text/javascript"> </script>
        var hub = $.connection.testHub;
        hub.showMessage = function() {
                    $("#inboxcount").show();
                };
        $.connection.hub.start()
            .done(function() {
                hub.subscribe($('#<%= hdnUserId.ClientID %>').val());
            })
            .fail(function() {
                alert("Could not Connect!");
            });

测试中心.cs:

using System.Threading;
{
    /// <summary>
    /// Test Hub used to demonstrate the key concepts of SignalR
    /// </summary>
    [SignalR.Hubs.HubName("testHub")]
    public class TestHub : SignalR.Hubs.Hub
    {
        /// <summary>
        /// Broadcast the message to all clients
        /// </summary>
        /// <param name="message">message to be broadcasted</param>
        public void Broadcast(string message)
        {
            this.Clients.showMessage(message);
        }
        /// <summary>
        /// Return a string with the formate, Hello [current user name]
        /// </summary>
        /// <returns></returns>
        public string SayHello()
        {
            //Context property can be used to retreive HTTP attributes like User
            return "Hello " + Context.User.Identity.Name;
        }
        /// <summary>
        /// Simulates a long running process that updates its progress
        /// </summary>
        public void LongRunningMethod()
        {
            Thread.Sleep(1000);
            this.Caller.showMessage("25% Completed");
            Thread.Sleep(1000);
            this.Caller.showMessage("50% Completed");
            Thread.Sleep(1000);
            this.Caller.showMessage("75% Completed");
            Thread.Sleep(1000);
            this.Caller.showMessage("Done");
        }
        /// <summary>
        /// Subscribe to a given message category
        /// </summary>
        /// <param name="category">the category to subscribe</param>
        public void Subscribe(string category)
        {
            //Add current connection to a connection group with the name 'category'
            this.AddToGroup(category);
        }
        /// <summary>
        /// Publish a message to the given mmessage category
        /// </summary>
        /// <param name="category">the category to send the message</param>
        /// <param name="message">the message to be sent</param>
        public void Publish(string category, string message)
        {
            //Broadcast the message to all connections registered under the group 'category'
            this.Clients[category].showMessage(message);
        }
    }

你可能应该使用

 <script src="@Url.Content("~/signalr/hubs")" type="text/javascript"> </script>

而不是

 <script src="signalr/hubs" type="text/javascript"> </script>

如果这没有帮助,则应检查项目中是否没有任何旧的 SignalR 库:因此,请删除每个 SignalR dll,并添加:

  • Microsoft.AspNet.SignalR.Core
  • Microsoft.AspNet.SignalR.Hosting.AspNet
  • Microsoft.AspNet.SignalR.Hosting.Common

这对我来说起到了作用:)

在中心调用中添加服务器或客户端枢纽。服务器订阅

相关内容

最新更新