未捕获的错误:信号器 jquery 错误

本文关键字:错误 jquery 信号器 | 更新日期: 2023-09-27 18:36:12

我在 C# 中实现 Signalr 时遇到错误 asp.net 例如:

Uncaught Error: SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started.

我正在Visual Studio 2010中实现信号器,我以前已经实现了相同的代码,但这工作正常。请帮助我解决此错误。我的代码是这样的:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="SignalRTest._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR-1.0.0-rc1.js" type="text/javascript"></script>
    <script src="signalr/hubs" type="text/javascript"></script> 
</head>
<body>
    <form id="form1" runat="server">
     <script type="text/javascript">
         $(function () {
             var IWannaChat = $.connection.myChatHub;
             IWannaChat.client.addMessage = function (message) {
                 $('#listMessages').append('<li>' + message + '</li>');
             };
             $("#SendMessage").click(function () {
                 IWannaChat.server.send($('#txtMessage').val());
             });
             $.connection.hub.start();
         });
    </script>


  <div>
        <input type="text" id="txtMessage" />
        <input type="button" id="SendMessage" value="broadcast" />
        <ul id="listMessages">
        </ul>
    </div>
</form>
</body>
</html>

我的代码是这样的:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Microsoft.AspNet.SignalR.Hubs
Imports Microsoft.AspNet.SignalR
Imports System.Net
Namespace SignalRChat
    <HubName("myChatHub")> _
    Public Class LetsChat
        Inherits Hub
        Public Sub send(message As String)
            message = "User : " + Dns.GetHostName() + " User :" + System.Security.Principal.WindowsIdentity.GetCurrent().Name + " Message : " + message

            Clients.Caller.addMessage(message)
        End Sub
    End Class
End Namespace

我的 global.asax 是这样的:

   Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
        RouteTable.Routes.MapHubs()
    End Sub

未捕获的错误:信号器 jquery 错误

我会写这个,以便在连接开始后设置按钮上的钩子:

 $(function () {
    var IWannaChat = $.connection.myChatHub;
    IWannaChat.client.addMessage = function (message) {
        $('#listMessages').append('<li>' + message + '</li>');
    };
    $.connection.hub.start().done(function() {
         $("#SendMessage").click(function () {
             IWannaChat.server.send($('#txtMessage').val());
         });
    });
}