我想修改一個簡單的聊天應用程序用於學習的目的。我做的唯一改變是將按鈕改爲服務器端控件。我遇到的問題是,我第一次播放消息時,它可以正常工作,並且調用了Clients.addNotification(msg)。然而,第二次,雖然JavaScript工作,在最後一步,JavaScript撤消所有的變化和Clients.addNotification(..)永遠不會被調用:/它只是第一次工作!我要重建我的項目看到它再次合作(刷新頁面將無法正常工作)簡單的聊天應用程序信號r
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.addNotification(message);
}
}
我的aspx頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client1.aspx.cs" Inherits="WebApplication1.WorkingWithHubs.Client1" %>
<!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.js" type="text/javascript"></script>
<script src="../Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script 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.addNotification = function (message) {
$('#messages').append('<li>' + message + '</li>');
var labelValue = $('#total').val();
$('#total').val(parseInt(labelValue, 10) + 1);
};
$("#btn").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">
<asp:ScriptManager ID="scriptManager" runat="server" />
<div>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<input type="text" id="msg" runat="server" />
<asp:Button ID="btn" runat="server" Text="BroadCast" OnClick="btn_click" ClientIDMode="static" />
<input type="text" id="total" value="0" />
<ul id="messages">
</ul>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
我後面的代碼:
public partial class Client1 : System.Web.UI.Page
{
public List<String> list = new List<String>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_click(object sender, EventArgs e)
{
list.Add(msg.Value);
}
}
一旦這個示例工作我將把代碼轉移到我正在處理的另一個應用程序,以便通知可以立即推送給所有客戶端。
我是一個非常初學者,我會很感激你的幫助!非常感謝你們!
編輯:
檢查出來的時候網絡選項卡(在我的鉻),爲第一次點擊,我得到一個「發送」(signalR)和Client1.aspx和一切工作正常,第二點擊開始,我只得到Client1.aspx,並沒有任何發送:對於一個快速和骯髒的解決方案/
沒問題@test :) – redsquare 2012-04-02 09:57:04