| | 1 | | using System; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Net; |
| | 5 | | using System.Net.NetworkInformation; |
| | 6 | | using DCL; |
| | 7 | | using UnityEditor; |
| | 8 | | using UnityEngine; |
| | 9 | | using WebSocketSharp; |
| | 10 | | using WebSocketSharp.Server; |
| | 11 | |
|
| | 12 | | public class DCLWebSocketService : WebSocketBehavior |
| | 13 | | { |
| | 14 | | public static bool VERBOSE = false; |
| | 15 | |
|
| | 16 | | private void SendMessageToWeb(string type, string message) |
| | 17 | | { |
| | 18 | | #if (UNITY_EDITOR || UNITY_STANDALONE) |
| 0 | 19 | | var x = new Message() |
| | 20 | | { |
| | 21 | | type = type, |
| | 22 | | payload = message |
| | 23 | | }; |
| 0 | 24 | | Send(Newtonsoft.Json.JsonConvert.SerializeObject(x)); |
| 0 | 25 | | if (VERBOSE) |
| | 26 | | { |
| 0 | 27 | | Debug.Log("SendMessageToWeb: " + type); |
| | 28 | | } |
| | 29 | | #endif |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public class Message |
| | 33 | | { |
| | 34 | | public string type; |
| | 35 | | public string payload; |
| | 36 | |
|
| 0 | 37 | | public override string ToString() { return string.Format("type = {0}... payload = {1}...", type, payload); } |
| | 38 | | } |
| | 39 | |
|
| | 40 | | protected override void OnMessage(MessageEventArgs e) |
| | 41 | | { |
| 0 | 42 | | base.OnMessage(e); |
| | 43 | |
|
| 0 | 44 | | lock (WebSocketCommunication.queuedMessages) |
| | 45 | | { |
| | 46 | | Message finalMessage; |
| 0 | 47 | | finalMessage = JsonUtility.FromJson<Message>(e.Data); |
| | 48 | |
|
| 0 | 49 | | WebSocketCommunication.queuedMessages.Enqueue(finalMessage); |
| 0 | 50 | | WebSocketCommunication.queuedMessagesDirty = true; |
| 0 | 51 | | } |
| 0 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | protected override void OnError(ErrorEventArgs e) { base.OnError(e); } |
| | 55 | |
|
| | 56 | | protected override void OnClose(CloseEventArgs e) |
| | 57 | | { |
| 0 | 58 | | base.OnClose(e); |
| 0 | 59 | | WebInterface.OnMessageFromEngine -= SendMessageToWeb; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | protected override void OnOpen() |
| | 63 | | { |
| 0 | 64 | | base.OnOpen(); |
| 0 | 65 | | WebInterface.OnMessageFromEngine += SendMessageToWeb; |
| | 66 | |
|
| 0 | 67 | | DataStore.i.wsCommunication.communicationEstablished.Set(true); |
| | 68 | |
|
| 0 | 69 | | var callFromMainThread = new Action(WebInterface.SendSystemInfoReport); // `WebInterface.SendSystemInfoReport` c |
| 0 | 70 | | callFromMainThread.Invoke(); |
| 0 | 71 | | } |
| | 72 | | } |