123456789101112131415161718192021222324252627282930313233 |
- using System;
- using System.Net.WebSockets;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- namespace BubbleSocketCore.Handlers
- {
- class EchoHandler : IHandler
- {
- public void AddSocket(WebSocket webSocket)
- {
- }
- public bool IsListening(HttpRequest request)
- {
- return request.Path == "/echo";
- }
- public async Task Listen(HttpContext context, WebSocket webSocket, CancellationToken cancellationToken)
- {
- var buffer = new byte[1024 * 4];
- WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
- while (!result.CloseStatus.HasValue)
- {
- await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, cancellationToken);
- result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
- }
- await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, cancellationToken);
- }
- }
- }
|