1
This commit is contained in:
parent
60cb29e75a
commit
bd23228940
@ -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<Net.ClientNet>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<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.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<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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user