ASP.NET C # SignalR Stream for the client

I have a couple of basic SignalR demos working locally (chat, etc.), and I understand that I need to click on the server and then transfer it to all connected clients.

My question is: how can I use SignalR to "stream" data for the client. For example, examples are discussed in NodeJS / Socket.IO using the streaming API Twitters.

Are there any streaming SignalR examples? How can this work?

+5
source share
2 answers

Take a look at the samples in the SignalR.Samples project. There is a streaming sample.

Connection to the server:

namespace SignalR.Samples.Streaming
{
    public class Streaming : PersistentConnection
    {
    }
}

The code that generates the stream

ThreadPool.QueueUserWorkItem(_ =>
{
    var connection = Connection.GetConnection<Streaming.Streaming>();
    while (true)
    {
        connection.Broadcast(DateTime.Now.ToString());
        Thread.Sleep(2000);
    }
});

Client code

<!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>
+10
source

SignalR, HTTP ? Stream - .NET?

, , - , .

+1

All Articles