FleckWebSocket.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Model.Net;
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using IWebSocketConnection = Fleck.IWebSocketConnection;
  7. namespace MediaBrowser.Server.Implementations.WebSocket
  8. {
  9. public class FleckWebSocket : IWebSocket
  10. {
  11. private readonly IWebSocketConnection _connection;
  12. public FleckWebSocket(IWebSocketConnection connection)
  13. {
  14. _connection = connection;
  15. _connection.OnMessage = OnReceiveData;
  16. }
  17. public WebSocketState State
  18. {
  19. get { return _connection.IsAvailable ? WebSocketState.Open : WebSocketState.Closed; }
  20. }
  21. private void OnReceiveData(string data)
  22. {
  23. if (OnReceive != null)
  24. {
  25. OnReceive(data);
  26. }
  27. }
  28. public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken)
  29. {
  30. return Task.Run(() => _connection.Send(bytes));
  31. }
  32. public void Dispose()
  33. {
  34. _connection.Close();
  35. }
  36. public Action<byte[]> OnReceiveBytes { get; set; }
  37. public Action<string> OnReceive { get; set; }
  38. }
  39. }