diff --git a/Managers/Battle.cs b/Managers/Battle.cs index 93fc9bb..94a7fc0 100644 --- a/Managers/Battle.cs +++ b/Managers/Battle.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Google.Protobuf; +using UnityEngine; class Battle { @@ -12,10 +13,12 @@ class Battle public Battle(JoinInfo joinInfo) { - this.conn = new Net.ClientNet("ws://192.168.100.21:7601/websocket?server_id=2"); + var rootObj = new GameObject(); + rootObj.AddComponent(typeof(Net.ClientNet)); + this.conn = rootObj.GetComponent(); + this.conn.Init("ws://192.168.100.21:7601/websocket?server_id=2"); this.conn.OnOpen += (Net.ClientNet conn) => { - return; var joinMsg = new battle.cs.CMJoin(); joinMsg.AccountId = joinInfo.account_id; joinMsg.SessionId = joinInfo.session_id; @@ -24,14 +27,6 @@ class Battle this.conn.SendMsg(joinMsg); }; this.conn.Open(); - { - var joinMsg = new battle.cs.CMJoin(); - joinMsg.AccountId = joinInfo.account_id; - joinMsg.SessionId = joinInfo.session_id; - joinMsg.TeamUuid = joinInfo.team_uuid; - joinMsg.PayloadData = joinInfo.payload_data; - this.conn.SendMsg(joinMsg); - } } } diff --git a/Managers/BattleMgr.cs b/Managers/BattleMgr.cs index 518931f..f5e4d29 100644 --- a/Managers/BattleMgr.cs +++ b/Managers/BattleMgr.cs @@ -45,17 +45,4 @@ public class BattleMgr } } - - public static void StartNet(JoinInfo join_info) - { - /* - var m_webSocket = new WebSocket(new Uri("192.168.100.21")); - m_webSocket.OnOpen += BattleMgr.OnOpen; - m_webSocket.OnBinary += BattleMgr.OnBinaryReceived; - m_webSocket.OnError += BattleMgr.OnError; - m_webSocket.OnClosed += BattleMgr.OnClosed; - m_webSocket.Open();*/ - - } - } diff --git a/Net/ClientNet.cs b/Net/ClientNet.cs index da72e9c..a4039c4 100644 --- a/Net/ClientNet.cs +++ b/Net/ClientNet.cs @@ -14,8 +14,14 @@ namespace Net public delegate void OnNetError(ClientNet conn); public delegate void OnNetClosed(ClientNet conn); - public class ClientNet + public class ClientNet : MonoBehaviour { + enum EventType + { + Open, + Error, + Closed + } private string uri; private WebSocket ws; @@ -24,8 +30,10 @@ namespace Net public OnNetOpen OnOpen; public OnNetError OnError; public OnNetClosed OnClosed; + private readonly object lockObj = new object(); + private Queue> events = new Queue>(); - public ClientNet(string uri) + public void Init(string uri) { this.ws = new WebSocket(new Uri(uri)); this.ws.OnOpen += this.OnWsOpen; @@ -98,7 +106,7 @@ namespace Net private void OnWsOpen(WebSocket ws) { Debug.Log("ClientNet.OnOpen"); - this.OnOpen(this); + this.AddEvent(EventType.Open, null); } private void OnWsBinaryReceived(WebSocket ws, byte[] data) @@ -109,13 +117,68 @@ namespace Net private void OnWsError(WebSocket ws, string reason) { Debug.Log("ClientNet.OnError"); - this.OnError(this); + this.AddEvent(EventType.Error, null); } private void OnWsClosed(WebSocket ws, UInt16 code, string message) { Debug.Log("ClientNet.OnClosed"); - this.OnClosed(this); + this.AddEvent(EventType.Closed, null); + } + + private void AddEvent(EventType type, object[] args) + { + lock (this.lockObj) + { + this.events.Enqueue(new KeyValuePair(type, args)); + } + } + + void Update() + { + lock (this.lockObj) + { + while (this.events.Count > 0) + { + this.DispatchEvent(this.events.Dequeue()); + } + } + } + + private void DispatchEvent(KeyValuePair e) + { + switch (e.Key) + { + case EventType.Open: + { + if (this.OnOpen != null) + { + this.OnOpen(this); + } + } + break; + case EventType.Error: + { + if (this.OnError != null) + { + this.OnError(this); + } + } + break; + case EventType.Closed: + { + if (this.OnClosed != null) + { + this.OnClosed(this); + } + } + break; + } + } + + void Awake() + { + int i = 0; } }