2012-08-30 73 views
-1

當我想要發送數據時即時獲得一個箴言o在頁面加載時從集線器啓動一個子集。患病把這個樣本代碼,因爲是短SignalR發送數據頁面加載

這是我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> 

我不想按下按鈕,我想他DOIT全部由自己當加載頁面

當我把它像這樣

<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:連接必須在數據可以發送之前啓動。之前。發送調用。開始()()「;

回答

0

必須開始叫你做派前

這樣:

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'); 
}); 

希望這幫助!

+0

我做了IT,我必須刪除該功能並且不用它就像這樣 ,但我必須從任何函數之外調用它,因爲它們內部不起作用。謝謝 –