title: How to use websockets in asp.net core
body: <p>im trying to develop a game where i store a scoreboard in a text file which is stored on server (currently on localhost). I am using http get and post calls in order to communicate with the server and get and send the data that i want. Now i want to implement websockets in order to send a notification from the server to the c# client. The notification will just display on the console a message for the user, for example in mu case i want to display a message to the user each time a user is added to the scoreboard, each time the UpdateScoreBoard method is called. Based on tutorials i found online i have managed to build the following code, can anyone make it more clear for me how the i will build the websocket for the server and how i will initialize the websocket on the client? Thank you</p>
  <p>Startup.cs (Server)</p>
  <pre><code>        public void Configure(IApplicationBuilder app, IHostEnvironment env)
          {
            //deleted code
  
              var webSocketOptions = new WebSocketOptions()
              {
                  KeepAliveInterval = TimeSpan.FromSeconds(120),
                  ReceiveBufferSize = 4 * 1024
              };
  
  
              app.UseWebSockets(webSocketOptions);
          
              app.Use(async (context, next) =&gt;
              {
                  if (context.Request.Path == &quot;/ws&quot;)
                  {
                      if (context.WebSockets.IsWebSocketRequest)
                      {
                          WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                          await Echo(context, webSocket);
                      }
                      else
                      {
                          context.Response.StatusCode = 400;
                      }
                  }
                  else
                  {
                      await next();
                  }
  
              });
          }
  
          private async Task Echo(HttpContext context, WebSocket webSocket)
          {
              var buffer = new byte[1024 * 4];
              WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment&lt;byte&gt;(buffer), CancellationToken.None);
              while (!result.CloseStatus.HasValue)
              {
                  await webSocket.SendAsync(new ArraySegment&lt;byte&gt;(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
  
                  result = await webSocket.ReceiveAsync(new ArraySegment&lt;byte&gt;(buffer), CancellationToken.None);
              }
              await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
          }
  </code></pre>
  <p>HttpClass.cs (Client) - where i call the http post request</p>
  <pre><code>public async override Task&lt;List&lt;Scoreboard&gt;&gt; UpdateScoreBoards(string username, int attempts, int seconds, DateTime date)
              {
                  HttpResponseMessage response = null;
                  //Creating a new instance of object Scoreboard
                  //deleted code
  
                  var url = &quot;http://localhost:5000/api/Scoreboard&quot;;
  
                  var socket_url = new Uri(&quot;ws://localhost:5000&quot;); 
                  var exitEvent = new ManualResetEvent(false);
                  using (var client = new WebsocketClient(socket_url))
                  {
                      client.ReconnectTimeout = TimeSpan.FromSeconds(30);
                      client.ReconnectionHappened.Subscribe(info =&gt;
                          Log.Information($&quot;Reconnection happened, type: {info.Type}&quot;));
  
                      client.MessageReceived.Subscribe(msg =&gt; Log.Information($&quot;Message received: {msg}&quot;));
                      await client.Start();
  
                      await Task.Run(() =&gt; client.Send(&quot;test&quot;));
  
                      exitEvent.WaitOne();
                  }
  
  // deleted code
              }
  </code></pre>