Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an unlimited stream endpoint in asp.net core

I'd like to create:

  • an ASP.NET Core 1.1 Web Application in VS2017 with one HttpGet Endpoint
  • The endpoint should stream random Values between 0 and 255
  • Every 20ms the value should change.
  • The Client should never stop reading the stream and as fast as possible get the new values.

So far this is my Code:

Client

    public async Task SubscribeToUpdates()
    {
        this.subscribe = false;
        try
        {
            var client = new HttpClient();
            var stream = await client.GetStreamAsync(Constants.SubscribeEndpoint);

            using (var rdr = new StreamReader(stream))
            {
                while (!rdr.EndOfStream && !subscribe)
                {
                    var result = rdr.ReadLine();
                    var json = JObject.Parse(result);
                    this.HandleUpdateResult(json); // todo
                }
            }
        }
        catch (Exception e)
        {
            // TO do: log exception
        }
    }

Server, not working

    [HttpGet]
    public Task PushStreamContent()
    {
        HttpContext.Response.ContentType = "text/event-stream";
        var sourceStream = randomStream();
        return sourceStream.CopyToAsync(HttpContext.Response.Body);
    }

    public static Stream randomStream()
    {
        Random rnd = new Random();
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(JsonConvert.SerializeObject(rnd.News(0,255));
        writer.Flush();
        stream.Position = 0;
        return stream;
    }

Question:

  • How to rewrite Server function to create the unlimited stream?
  • Other solutions are also welcome, as long as they keep in mind that they are .net core conform and enable the client to get as fast as possible the latest information.

Working Full .Net Version

I've managed to write the Code for .net Standard, but not for .net core. Reason for this is that PushStreamContent does not exist in .net core :/.

    [HttpGet]
    public HttpResponseMessage PushStreamContent()
    {
        var response = Request.CreateResponse(HttpStatusCode.Accepted);

        response.Content =
            new PushStreamContent((stream, content, context) =>
            {
                var plotter = new Plotter();

                while (true)
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        Random rnd = new Random()
                        writer.WriteLine(rnd.Next(0,255));
                        stream.Flush();
                        Thread.Sleep(20);
                    }
                }
            });

        return response;
    }
like image 321
The_Holy_One Avatar asked Jan 31 '26 07:01

The_Holy_One


1 Answers

Thanks to the previous Answer from "Mike McCaughan" and "Joel Harkes", i've rethinked the communcation process and switched from REST to Websockets.

You can find a good Example how to use WebSockets in .net Core (and Xamarin.Forms) here. You will have to use the Nuget Package "Websockets.PCL".

like image 83
The_Holy_One Avatar answered Feb 01 '26 21:02

The_Holy_One