-1

データを送信したいときに問題が発生するoページの読み込み中にハブからサブを開始する。短いので、このサンプルコードを入れてください

これは私のchat.vbです

 Imports System
 Imports System.Collections.Generic
 Imports System.Linq
 Imports System.Web
 Imports SignalR.Hubs

 Public Class Chat
    Inherits Hub
    Public Sub Send(message As String)
       ' Call the addMessage method on all clients
        Clients.addMessage(message)
    End Sub
 End Class

これが私のdefault.aspxです

 <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
 <script src="Scripts/jquery.signalR-0.5.3.js" type="text/javascript"></script>
 <script src="signalr/hubs" type="text/javascript"></script>
 <script language="javascript" type="text/javascript">
    $(function () {
        // Proxy created on the fly
        var chat = $.connection.chat;

        // Declare a function on the chat hub so the server can invoke it
        chat.addMessage = function (message) {
        $('#messages').append('<li>' + message + '</li>');
        };

        $("#broadcast").click(function () {
           // Call the chat method on the server
           chat.send($('#msg').val());
       }); 
       // Start the connection
       $.connection.hub.start();
    });
 </script>
 </head>
 <body>
   <form id="form1" runat="server">
   <div style="position: absolute; left: 0; top: 0; height: 100%; width: 100%">
     <input type="text" id="msg" />
     <input type="button" id="broadcast" value="broadcast" /> 
     <ul id="messages">
     </ul>
   </div> 
   </form>
 </body>

私はボタンを押したくありません、私はページをロードするときに彼がすべて自分でやりたいです

このように置くと

    <script language="javascript" type="text/javascript">
      $(document).ready(function () {
        // Proxy created on the fly
        var chat = $.connection.chat;

        // Declare a function on the chat hub so the server can invoke it
        chat.addMessage = function (message) {
        $('#messages').append('<li>' + message + '</li>');
        }; 

       chat.send('Hello World');

       // Start the connection
       $.connection.hub.start();
    });
 </script>

接続の問題が発生しました。「SignalR:データを送信する前に接続を開始する必要があります。.send()の前に.start()を呼び出してください」というエラーが発生します。

4

1 に答える 1

0

送信する前に start を呼び出す必要があります。

そう:

chat.send('Hello World');

// Start the connection
$.connection.hub.start();

次のようにする必要があります。

// Start the connection
$.connection.hub.start().done(function () {
    // Call the server side function AFTER the connection has been started
    chat.send('Hello World');
});

お役に立てれば!

于 2012-08-30T23:24:11.743 に答える