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


i'd create:

  • an asp.net core 1.1 web application in vs2017 1 httpget endpoint
  • the endpoint should stream random values between 0 , 255
  • every 20ms value should change.
  • the client should never stop reading stream , fast possible new values.

so far 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)         {             // 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 rewrite server function create unlimited stream?
  • other solutions welcome, long keep in mind .net core conform , enable client fast possible latest information.

working full .net version

i've managed write code .net standard, not .net core. reason pushstreamcontent 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;     } 

thanks previous answer "mike mccaughan" , "joel harkes", i've rethinked communcation process , switched rest websockets.

you can find example how use websockets in .net core (and xamarin.forms) here. have use nuget package "websockets.pcl".


Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

Command prompt result in label. Python 2.7 -