我需要实时向客户发送字符串,但我的代码只适用于睡眠。如果我减少睡眠参数,则会错过一些呼叫。我的中心类是空的,因为我不需要客户端到服务器的调用。这是我的aspx文件:客户端(aspx):
var myhub = $.connection.chatHub;
myhub.client.broadcastMessage = function (message) {
$('#container').append(message);
};
$.connection.hub.start();
服务器(c#):
protected void Button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(novaThread);
t.Start();
}
static void novaThread() {
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
for (int i = 0; i < 20; i++)
{
hubContext.Clients.All.broadcastMessage("Server datetime: " + DateTime.Now.ToString());
Thread.Sleep(1000);
}
}