我有几个本地工作的 SignalR 基本演示(聊天等),我了解推送到服务器然后将其广播到所有连接的客户端。
我的问题是如何利用 SignalR 将数据“流”到客户端。例如,在 NodeJS/Socket.IO 中看到使用 Twitter 流 API 的示例。
是否有任何 SignalR 流式传输示例?这将如何运作?
最佳答案
查看 SignalR.Samples 项目中的示例。那里有一个流媒体示例。
服务器端连接:
namespace SignalR.Samples.Streaming
{
public class Streaming : PersistentConnection
{
}
}
生成流的代码
ThreadPool.QueueUserWorkItem(_ =>
{
var connection = Connection.GetConnection<Streaming.Streaming>();
while (true)
{
connection.Broadcast(DateTime.Now.ToString());
Thread.Sleep(2000);
}
});
客户端代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<script src="../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../Scripts/jquery.signalR.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var connection = $.connection('Streaming.ashx');
connection.received(function (data) {
$('#messages').append('<li>' + data + '</li>');
});
connection.start();
});
</script>
</head>
<body>
<ul id="messages">
</ul>
</body>
</html>
关于c# - ASP.NET C# SignalR 流到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153282/