|
@@ -1,78 +0,0 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Net;
|
|
|
-using System.Threading.Tasks;
|
|
|
-using WingInterfaceLibrary.Request.Notification;
|
|
|
-using WingNotificationModule.Adapter;
|
|
|
-using WingNotificationModule.Channel.WebSocket;
|
|
|
-
|
|
|
-namespace WingNotificationModule.Channel
|
|
|
-{
|
|
|
- public class WebSocketClientChannel : IChannel
|
|
|
- {
|
|
|
- private WebSocketClient _webSocketClient;
|
|
|
- private string _reqToken = Guid.NewGuid().ToString("N");
|
|
|
-
|
|
|
- public WebSocketClientChannel(string token)
|
|
|
- {
|
|
|
- _reqToken = token;
|
|
|
- }
|
|
|
-
|
|
|
- public async Task<bool> ConnectAsync(IPEndPoint serverEndpoint)
|
|
|
- {
|
|
|
- _webSocketClient = new WebSocketClient
|
|
|
- (
|
|
|
- $"ws://{serverEndpoint.Address.ToString()}:{serverEndpoint.Port}?Token={_reqToken}"
|
|
|
- );
|
|
|
- await _webSocketClient.ConnectAsync();
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- public async Task<bool> DisconnectAsync()
|
|
|
- {
|
|
|
- _webSocketClient?.Disconnect();
|
|
|
- return await Task.FromResult(true);
|
|
|
- }
|
|
|
-
|
|
|
- public void Dispose()
|
|
|
- {
|
|
|
- _webSocketClient?.Dispose();
|
|
|
- }
|
|
|
-
|
|
|
- public async Task<List<NotifyMessage>> GetMessageAsync()
|
|
|
- {
|
|
|
- if(_webSocketClient!=null)
|
|
|
- {
|
|
|
- var receiveData = await _webSocketClient.ReceiveAsync();
|
|
|
- var buffer = receiveData.Buffer;
|
|
|
- var adapter = new MessageAdapter(buffer);
|
|
|
- return await Task.FromResult(adapter.GetNotifyMessages());
|
|
|
- }
|
|
|
- return await Task.FromResult(new List<NotifyMessage>());
|
|
|
- }
|
|
|
-
|
|
|
- public async Task<bool> SendAsync(NotifyMessage message, string clientId = null)
|
|
|
- {
|
|
|
- var adapter = new BufferAdapter(message);
|
|
|
- var buffer = adapter.GetMessageBuffer();
|
|
|
- if(_webSocketClient!=null)
|
|
|
- {
|
|
|
- await _webSocketClient.SendAsync(buffer, System.Net.WebSockets.WebSocketMessageType.Binary);
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- class SimpleApplyToken : IApplyToken
|
|
|
- {
|
|
|
- public string GetTokenByDeviceId(string clientDeviceId)
|
|
|
- {
|
|
|
- throw new NotImplementedException();
|
|
|
- }
|
|
|
-
|
|
|
- public bool ValidateToken(string token)
|
|
|
- {
|
|
|
- return !string.IsNullOrWhiteSpace(token);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|