This commit is contained in:
azw 2024-05-19 19:12:41 +08:00
parent 60cb29e75a
commit bd23228940
3 changed files with 73 additions and 28 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Google.Protobuf; using Google.Protobuf;
using UnityEngine;
class Battle class Battle
{ {
@ -12,10 +13,12 @@ class Battle
public Battle(JoinInfo joinInfo) 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<Net.ClientNet>();
this.conn.Init("ws://192.168.100.21:7601/websocket?server_id=2");
this.conn.OnOpen += (Net.ClientNet conn) => this.conn.OnOpen += (Net.ClientNet conn) =>
{ {
return;
var joinMsg = new battle.cs.CMJoin(); var joinMsg = new battle.cs.CMJoin();
joinMsg.AccountId = joinInfo.account_id; joinMsg.AccountId = joinInfo.account_id;
joinMsg.SessionId = joinInfo.session_id; joinMsg.SessionId = joinInfo.session_id;
@ -24,14 +27,6 @@ class Battle
this.conn.SendMsg(joinMsg); this.conn.SendMsg(joinMsg);
}; };
this.conn.Open(); 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);
}
} }
} }

View File

@ -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();*/
}
} }

View File

@ -14,8 +14,14 @@ namespace Net
public delegate void OnNetError(ClientNet conn); public delegate void OnNetError(ClientNet conn);
public delegate void OnNetClosed(ClientNet conn); public delegate void OnNetClosed(ClientNet conn);
public class ClientNet public class ClientNet : MonoBehaviour
{ {
enum EventType
{
Open,
Error,
Closed
}
private string uri; private string uri;
private WebSocket ws; private WebSocket ws;
@ -24,8 +30,10 @@ namespace Net
public OnNetOpen OnOpen; public OnNetOpen OnOpen;
public OnNetError OnError; public OnNetError OnError;
public OnNetClosed OnClosed; public OnNetClosed OnClosed;
private readonly object lockObj = new object();
private Queue<KeyValuePair<EventType, object[]>> events = new Queue<KeyValuePair<EventType, object[]>>();
public ClientNet(string uri) public void Init(string uri)
{ {
this.ws = new WebSocket(new Uri(uri)); this.ws = new WebSocket(new Uri(uri));
this.ws.OnOpen += this.OnWsOpen; this.ws.OnOpen += this.OnWsOpen;
@ -98,7 +106,7 @@ namespace Net
private void OnWsOpen(WebSocket ws) private void OnWsOpen(WebSocket ws)
{ {
Debug.Log("ClientNet.OnOpen"); Debug.Log("ClientNet.OnOpen");
this.OnOpen(this); this.AddEvent(EventType.Open, null);
} }
private void OnWsBinaryReceived(WebSocket ws, byte[] data) private void OnWsBinaryReceived(WebSocket ws, byte[] data)
@ -109,13 +117,68 @@ namespace Net
private void OnWsError(WebSocket ws, string reason) private void OnWsError(WebSocket ws, string reason)
{ {
Debug.Log("ClientNet.OnError"); Debug.Log("ClientNet.OnError");
this.OnError(this); this.AddEvent(EventType.Error, null);
} }
private void OnWsClosed(WebSocket ws, UInt16 code, string message) private void OnWsClosed(WebSocket ws, UInt16 code, string message)
{ {
Debug.Log("ClientNet.OnClosed"); 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<EventType, object[]>(type, args));
}
}
void Update()
{
lock (this.lockObj)
{
while (this.events.Count > 0)
{
this.DispatchEvent(this.events.Dequeue());
}
}
}
private void DispatchEvent(KeyValuePair<EventType, object[]> 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;
} }
} }