| | 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 void SendMessageToWeb(string type, string message) |
| | 25 | | { |
| | 26 | | #if (UNITY_EDITOR || UNITY_STANDALONE) |
| 0 | 27 | | var x = new Message() |
| | 28 | | { |
| | 29 | | type = type, |
| | 30 | | payload = message |
| | 31 | | }; |
| | 32 | |
|
| 0 | 33 | | if (ConnectionState == WebSocketState.Open) |
| | 34 | | { |
| 0 | 35 | | var serializeObject = JsonConvert.SerializeObject(x); |
| | 36 | |
|
| 0 | 37 | | Send(serializeObject); |
| | 38 | |
|
| 0 | 39 | | if (VERBOSE) |
| | 40 | | { |
| 0 | 41 | | Debug.Log("SendMessageToWeb: " + type); |
| | 42 | | } |
| | 43 | | } |
| | 44 | | #endif |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public void SendBinary(byte[] data) |
| | 48 | | { |
| 0 | 49 | | Send(data); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public class Message |
| | 53 | | { |
| | 54 | | public string type; |
| | 55 | | public string payload; |
| | 56 | |
|
| 0 | 57 | | public override string ToString() { return string.Format("type = {0}... payload = {1}...", type, payload); } |
| | 58 | | } |
| | 59 | |
|
| | 60 | | protected override void OnMessage(MessageEventArgs e) |
| | 61 | | { |
| 0 | 62 | | base.OnMessage(e); |
| | 63 | |
|
| 0 | 64 | | if (e.IsBinary) |
| | 65 | | { |
| 0 | 66 | | OnMessageEvent?.Invoke(e.RawData); |
| 0 | 67 | | return; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | lock (WebSocketCommunication.queuedMessages) |
| | 71 | | { |
| 0 | 72 | | Message finalMessage = JsonUtility.FromJson<Message>(e.Data); |
| | 73 | |
|
| 0 | 74 | | WebSocketCommunication.queuedMessages.Enqueue(finalMessage); |
| 0 | 75 | | WebSocketCommunication.queuedMessagesDirty = true; |
| 0 | 76 | | } |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | protected override void OnError(ErrorEventArgs e) |
| | 80 | | { |
| 0 | 81 | | Debug.LogError(e.Message); |
| 0 | 82 | | base.OnError(e); |
| 0 | 83 | | OnErrorEvent?.Invoke(e.Message); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | protected override void OnClose(CloseEventArgs e) |
| | 87 | | { |
| 0 | 88 | | base.OnClose(e); |
| 0 | 89 | | WebInterface.OnMessageFromEngine -= SendMessageToWeb; |
| 0 | 90 | | DataStore.i.wsCommunication.communicationEstablished.Set(false); |
| 0 | 91 | | OnCloseEvent?.Invoke(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | protected override void OnOpen() |
| | 95 | | { |
| 0 | 96 | | Debug.Log("WebSocket Communication Established"); |
| 0 | 97 | | base.OnOpen(); |
| | 98 | |
|
| 0 | 99 | | WebInterface.OnMessageFromEngine += SendMessageToWeb; |
| 0 | 100 | | DataStore.i.wsCommunication.communicationEstablished.Set(true); |
| 0 | 101 | | OnConnectEvent?.Invoke(); |
| 0 | 102 | | } |
| | 103 | | } |