You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Web/WebSockets/WebSocketWrapper.cs

34 lines
910 B

using System;
using System.Net.WebSockets;
using System.Threading;
using Microsoft.AspNetCore.Http;
namespace Infrastructure.WebSockets
{
public class WebSocketWrapper
{
public WebSocketWrapper()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public HttpContext Context { get; set; }
public WebSocket WebSocket { get; set; }
public event EventHandler<string> Received;
public void Send(ArraySegment<byte> data, WebSocketMessageType messageType, Func<WebSocketWrapper, bool> condition)
{
if (condition != null && condition.Invoke(this))
{
this.WebSocket.SendAsync(data, messageType, true, CancellationToken.None);
}
}
public void OnReceived(string message)
{
this.Received?.Invoke(this, message);
}
}
}