我正在使用 SignalR/PersistentConnection,而不是集线器。
我想从服务器向客户端发送消息。我有客户端 ID 来发送它,但是如何从服务器向客户端发送消息呢?
就像,当服务器上发生某些事件时,我们希望向特定用户发送通知。
有任何想法吗?
The github https://github.com/SignalR/SignalR页面显示了如何使用持久连接来执行此操作。
public class MyConnection : PersistentConnection {
protected override Task OnReceivedAsync(string clientId, string data) {
// Broadcast data to all clients
return Connection.Broadcast(data);
}
}
全局.asax
using System;
using System.Web.Routing;
using SignalR.Routing;
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
// Register the route for chat
RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
}
}
然后在客户端:
$(function () {
var connection = $.connection('echo');
connection.received(function (data) {
$('#messages').append('<li>' + data + '</li>');
});
connection.start();
$("#broadcast").click(function () {
connection.send($('#msg').val());
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)