| | 1 | | using System; |
| | 2 | | using rpc_csharp.transport; |
| | 3 | |
|
| | 4 | | namespace RPC.Transports |
| | 5 | | { |
| | 6 | | public class WebSocketTransport : ITransport |
| | 7 | | { |
| | 8 | | public event Action OnCloseEvent; |
| | 9 | |
|
| | 10 | | public event Action<string> OnErrorEvent; |
| | 11 | |
|
| | 12 | | public event Action<byte[]> OnMessageEvent; |
| | 13 | |
|
| | 14 | | public event Action OnConnectEvent; |
| | 15 | |
|
| | 16 | | private DCLWebSocketService wsService; |
| | 17 | |
|
| 425 | 18 | | public WebSocketTransport() |
| | 19 | | { |
| | 20 | | // TODO: refactor websocket service to avoid the need of this reference |
| 425 | 21 | | if (WebSocketCommunication.service != null) |
| | 22 | | { |
| 0 | 23 | | OnWebSocketServiceAdded(WebSocketCommunication.service); |
| | 24 | | } |
| | 25 | | else |
| | 26 | | { |
| 425 | 27 | | WebSocketCommunication.OnWebSocketServiceAdded += OnWebSocketServiceAdded; |
| | 28 | | } |
| 425 | 29 | | } |
| | 30 | |
|
| | 31 | | public void Dispose() |
| | 32 | | { |
| 425 | 33 | | WebSocketCommunication.OnWebSocketServiceAdded -= OnWebSocketServiceAdded; |
| | 34 | |
|
| 425 | 35 | | if (wsService != null) |
| | 36 | | { |
| 0 | 37 | | wsService.OnConnectEvent -= OnConnect; |
| 0 | 38 | | wsService.OnMessageEvent -= OnMessage; |
| 0 | 39 | | wsService.OnErrorEvent -= OnError; |
| 0 | 40 | | wsService.OnCloseEvent -= OnClose; |
| | 41 | | } |
| 425 | 42 | | OnCloseEvent = null; |
| 425 | 43 | | OnErrorEvent = null; |
| 425 | 44 | | OnMessageEvent = null; |
| 425 | 45 | | OnConnectEvent = null; |
| 425 | 46 | | } |
| | 47 | |
|
| | 48 | | public void SendMessage(byte[] data) |
| | 49 | | { |
| 0 | 50 | | wsService?.SendBinary(data); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Close() |
| | 54 | | { |
| 0 | 55 | | WebSocketCommunication.OnWebSocketServiceAdded -= OnWebSocketServiceAdded; |
| | 56 | |
|
| 0 | 57 | | if (wsService != null) |
| | 58 | | { |
| 0 | 59 | | wsService.OnConnectEvent -= OnConnect; |
| 0 | 60 | | wsService.OnMessageEvent -= OnMessage; |
| 0 | 61 | | wsService.OnErrorEvent -= OnError; |
| 0 | 62 | | wsService.OnCloseEvent -= OnClose; |
| | 63 | | } |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnWebSocketServiceAdded(DCLWebSocketService service) |
| | 67 | | { |
| 0 | 68 | | WebSocketCommunication.OnWebSocketServiceAdded -= OnWebSocketServiceAdded; |
| 0 | 69 | | wsService = service; |
| | 70 | |
|
| 0 | 71 | | wsService.OnConnectEvent += OnConnect; |
| 0 | 72 | | wsService.OnMessageEvent += OnMessage; |
| 0 | 73 | | wsService.OnErrorEvent += OnError; |
| 0 | 74 | | wsService.OnCloseEvent += OnClose; |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | private void OnClose() |
| | 78 | | { |
| 0 | 79 | | OnCloseEvent?.Invoke(); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | private void OnError(string error) |
| | 83 | | { |
| 0 | 84 | | OnErrorEvent?.Invoke(error); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | private void OnMessage(byte[] message) |
| | 88 | | { |
| 0 | 89 | | OnMessageEvent?.Invoke(message); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void OnConnect() |
| | 93 | | { |
| 0 | 94 | | OnConnectEvent?.Invoke(); |
| 0 | 95 | | } |
| | 96 | | } |
| | 97 | | } |