| | 1 | | using System.IO; |
| | 2 | | using System.Text; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Interface; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | | using UnityEngine; |
| | 7 | | using WebSocketSharp; |
| | 8 | | using WebSocketSharp.Server; |
| | 9 | | using System; |
| | 10 | | using ErrorEventArgs = WebSocketSharp.ErrorEventArgs; |
| | 11 | |
|
| | 12 | | public class DCLWebSocketService : WebSocketBehavior |
| | 13 | | { |
| | 14 | | public static bool VERBOSE = false; |
| | 15 | |
|
| | 16 | | public event Action OnCloseEvent; |
| | 17 | |
|
| | 18 | | public event Action<string> OnErrorEvent; |
| | 19 | |
|
| | 20 | | public event Action<byte[]> OnMessageEvent; |
| | 21 | |
|
| | 22 | | public event Action OnConnectEvent; |
| | 23 | |
|
| | 24 | | private bool wantsToQuit; |
| | 25 | |
|
| 0 | 26 | | public DCLWebSocketService() |
| | 27 | | { |
| 0 | 28 | | Application.wantsToQuit += OnApplicationWantsToQuit; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | private bool OnApplicationWantsToQuit() |
| | 32 | | { |
| 0 | 33 | | wantsToQuit = true; |
| 0 | 34 | | return true; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | private void SendMessageToWeb(string type, string message) |
| | 38 | | { |
| | 39 | | #if (UNITY_EDITOR || UNITY_STANDALONE) |
| 0 | 40 | | var x = new Message() |
| | 41 | | { |
| | 42 | | type = type, |
| | 43 | | payload = message |
| | 44 | | }; |
| | 45 | |
|
| 0 | 46 | | if (ConnectionState == WebSocketState.Open) |
| | 47 | | { |
| 0 | 48 | | var serializeObject = JsonConvert.SerializeObject(x); |
| | 49 | |
|
| 0 | 50 | | Send(serializeObject); |
| | 51 | |
|
| 0 | 52 | | if (VERBOSE) |
| | 53 | | { |
| 0 | 54 | | Debug.Log("SendMessageToWeb: " + type); |
| | 55 | | } |
| | 56 | | } |
| | 57 | | #endif |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public void SendBinary(byte[] data) |
| | 61 | | { |
| 0 | 62 | | Send(data); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public class Message |
| | 66 | | { |
| | 67 | | public string type; |
| | 68 | | public string payload; |
| | 69 | |
|
| 0 | 70 | | public override string ToString() { return string.Format("type = {0}... payload = {1}...", type, payload); } |
| | 71 | | } |
| | 72 | |
|
| | 73 | | protected override void OnMessage(MessageEventArgs e) |
| | 74 | | { |
| 0 | 75 | | base.OnMessage(e); |
| | 76 | |
|
| 0 | 77 | | if (e.IsBinary) |
| | 78 | | { |
| 0 | 79 | | OnMessageEvent?.Invoke(e.RawData); |
| 0 | 80 | | return; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | lock (WebSocketCommunication.queuedMessages) |
| | 84 | | { |
| 0 | 85 | | if (wantsToQuit) |
| 0 | 86 | | return; |
| | 87 | |
|
| 0 | 88 | | Message finalMessage = JsonUtility.FromJson<Message>(e.Data); |
| | 89 | |
|
| 0 | 90 | | WebSocketCommunication.queuedMessages.Enqueue(finalMessage); |
| 0 | 91 | | WebSocketCommunication.queuedMessagesDirty = true; |
| 0 | 92 | | } |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | protected override void OnError(ErrorEventArgs e) |
| | 96 | | { |
| 0 | 97 | | Debug.LogError(e.Message); |
| 0 | 98 | | base.OnError(e); |
| 0 | 99 | | OnErrorEvent?.Invoke(e.Message); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | protected override void OnClose(CloseEventArgs e) |
| | 103 | | { |
| 0 | 104 | | base.OnClose(e); |
| 0 | 105 | | WebInterface.OnMessageFromEngine -= SendMessageToWeb; |
| 0 | 106 | | DataStore.i.wsCommunication.communicationEstablished.Set(false); |
| 0 | 107 | | OnCloseEvent?.Invoke(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | protected override void OnOpen() |
| | 111 | | { |
| 0 | 112 | | Debug.Log("WebSocket Communication Established"); |
| 0 | 113 | | base.OnOpen(); |
| | 114 | |
|
| 0 | 115 | | WebInterface.OnMessageFromEngine += SendMessageToWeb; |
| 0 | 116 | | DataStore.i.wsCommunication.communicationEstablished.Set(true); |
| 0 | 117 | | OnConnectEvent?.Invoke(); |
| 0 | 118 | | } |
| | 119 | | } |