yavsc/src/Yavsc/Startup/Startup.WebSockets.cs

41 lines
1.5 KiB
C#
Raw Normal View History

2019-02-08 10:22:52 +00:00
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
2016-11-14 12:17:24 +01:00
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
2019-02-08 10:22:52 +00:00
using Microsoft.AspNet.Http;
using Microsoft.AspNet.WebSockets.Server;
2016-11-14 12:17:24 +01:00
namespace Yavsc
{
public partial class Startup
{
public void ConfigureWebSocketsApp(IApplicationBuilder app,
SiteSettings siteSettings, IHostingEnvironment env)
{
2019-02-08 10:22:52 +00:00
var webSocketOptions = new WebSocketOptions()
{
KeepAliveInterval = TimeSpan.FromSeconds(120),
ReceiveBufferSize = 4 * 1024,
ReplaceFeature = true
};
app.UseWebSockets(webSocketOptions);
2019-01-30 11:14:32 +00:00
app.UseSignalR(Constants.SignalRPath);
2016-11-14 12:17:24 +01:00
}
2019-02-08 10:22:52 +00:00
private async Task Echo(HttpContext context, WebSocket webSocket)
{
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
2016-11-14 12:17:24 +01:00
}
}