419 lines
13 KiB
C#
419 lines
13 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
using UnityWebSocket;
|
||
using System;
|
||
//using System.IO;
|
||
|
||
|
||
public class WebSocketDemo : MonoBehaviour
|
||
{
|
||
public static WebSocketDemo instance;
|
||
|
||
public string MainSceneName = "NTESDemoScene";
|
||
/// <summary>
|
||
/// 主服务器
|
||
/// </summary>
|
||
public static WebSocket socket_main;
|
||
/// <summary>
|
||
/// 移动服务
|
||
/// </summary>
|
||
public static WebSocket socket_trans;
|
||
|
||
public ushort mySyncid;
|
||
//内网测试版
|
||
//string address = "ws://192.168.1.124:19000";
|
||
//string address1 = "ws://192.168.1.124:19001";
|
||
//string address2 = "ws://192.168.1.124:19002";
|
||
//string address3 = "ws://192.168.1.124:19003";
|
||
//string address4 = "ws://192.168.1.124:19004";
|
||
//string address5 = "ws://192.168.1.124:19005";
|
||
//string address6 = "ws://192.168.1.124:19006";
|
||
//string address7 = "ws://192.168.1.124:19007";
|
||
//string address8 = "ws://192.168.1.124:19008";
|
||
|
||
//网易服务器版
|
||
//string address = "ws://36.155.130.22:9000";
|
||
//string address1 = "ws://36.155.130.22:9001";
|
||
//string address2 = "ws://36.155.130.22:9002";
|
||
//string address3 = "ws://36.155.130.22:9003";
|
||
//string address4 = "ws://36.155.130.22:9004";
|
||
//string address5 = "ws://36.155.130.22:9005";
|
||
//string address6 = "ws://36.155.130.22:9006";
|
||
//string address7 = "ws://36.155.130.22:9007";
|
||
//string address8 = "ws://36.155.130.22:9008";
|
||
|
||
//umayle服务器,小程序版
|
||
string address = "wss://wss.umayle.com:19000";
|
||
string address1 = "wss://wss.umayle.com:19001";
|
||
string address2 = "wss://wss.umayle.com:19002";
|
||
string address3 = "wss://wss.umayle.com:19003";
|
||
string address4 = "wss://wss.umayle.com:19004";
|
||
string address5 = "wss://wss.umayle.com:19005";
|
||
string address6 = "wss://wss.umayle.com:19006";
|
||
string address7 = "wss://wss.umayle.com:19007";
|
||
string address8 = "wss://wss.umayle.com:19008";
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
private void Start()
|
||
{
|
||
//string ip=File.ReadAllText(Application.streamingAssetsPath + "/WebsocketSetting.txt");
|
||
|
||
// string address = "ws://"+ip.Split('=')[1];
|
||
//string address = "ws://36.155.130.22:30000";
|
||
|
||
|
||
// 创建实例
|
||
socket_main = new WebSocket(address);
|
||
// 注册回调
|
||
socket_main.OnOpen += OnOpen;
|
||
socket_main.OnClose += OnClose;
|
||
socket_main.OnMessage += OnMessage;
|
||
socket_main.OnError += OnError;
|
||
UnityEngine.SceneManagement.SceneManager.LoadScene(MainSceneName);
|
||
|
||
InvokeRepeating("CheckConnect", 5, 5);
|
||
}
|
||
|
||
public void InitTrans(byte which)
|
||
{
|
||
string address = "";
|
||
switch (which)
|
||
{
|
||
case 1:
|
||
address = address1;
|
||
break;
|
||
case 2:
|
||
address = address2;
|
||
break;
|
||
case 3:
|
||
address = address3;
|
||
break;
|
||
case 4:
|
||
address = address4;
|
||
break;
|
||
case 5:
|
||
address = address5;
|
||
break;
|
||
case 6:
|
||
address = address6;
|
||
break;
|
||
case 7:
|
||
address = address7;
|
||
break;
|
||
case 8:
|
||
address = address8;
|
||
break;
|
||
default:
|
||
Debug.LogError("服务器编号错误");
|
||
return;
|
||
}
|
||
|
||
socket_trans = new WebSocket(address);
|
||
socket_trans.OnOpen += OnOpen;
|
||
socket_trans.OnClose += OnClose;
|
||
socket_trans.OnMessage += OnMessage;
|
||
socket_trans.OnError += OnError;
|
||
//Debug.Log("连接服务:"+address);
|
||
}
|
||
private void OnOpen(object sender, OpenEventArgs e)
|
||
{
|
||
string url=((UnityWebSocket.WebSocket)sender).Address ;
|
||
Debug.Log("连接成功:" + url);
|
||
if(url!=address)
|
||
{
|
||
//trans
|
||
byte[] data = new byte[6];
|
||
Array.Copy(BitConverter.GetBytes(104), 0, data, 0, 4);
|
||
Array.Copy(BitConverter.GetBytes(mySyncid), 0, data, 4, 2);
|
||
SendByTrans(data);
|
||
}
|
||
|
||
if (GameManager.Instance)
|
||
{
|
||
GameManager.Instance.Tip.TipHide();
|
||
}
|
||
}
|
||
|
||
private void OnClose(object sender, CloseEventArgs e)
|
||
{
|
||
Debug.Log(string.Format("关闭: StatusCode: {0}, Reason: {1},连接:{2}", e.StatusCode, e.Reason, ((UnityWebSocket.WebSocket)sender).Address));
|
||
}
|
||
|
||
private void OnError(object sender, UnityWebSocket.ErrorEventArgs e)
|
||
{
|
||
Debug.LogError(string.Format("错误: {0}", e.Message));
|
||
}
|
||
|
||
private void CheckConnect()
|
||
{
|
||
if (socket_main != null && socket_main.ReadyState == WebSocketState.Closed)
|
||
{
|
||
Debug.Log("重连main");
|
||
if (GameManager.Instance)
|
||
{
|
||
GameManager.Instance.Tip.TipConform();
|
||
return;
|
||
}
|
||
else
|
||
socket_main.ConnectAsync();
|
||
}
|
||
|
||
if (socket_trans != null && socket_trans.ReadyState == WebSocketState.Closed)
|
||
{
|
||
Debug.Log("重连trans");
|
||
if (GameManager.Instance)
|
||
{
|
||
GameManager.Instance.Tip.TipConform();
|
||
return;
|
||
}
|
||
else
|
||
socket_trans.ConnectAsync();
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
if (socket_main != null && socket_main.ReadyState != WebSocketState.Closed)
|
||
{
|
||
socket_main.CloseAsync();
|
||
}
|
||
|
||
if(socket_trans!=null && socket_trans.ReadyState!= WebSocketState.Closed)
|
||
{
|
||
socket_trans.CloseAsync();
|
||
}
|
||
}
|
||
public void ConnectMain()
|
||
{
|
||
// 连接
|
||
socket_main.ConnectAsync();
|
||
}
|
||
public void ConnectTrans()
|
||
{
|
||
// 连接
|
||
socket_trans.ConnectAsync();
|
||
}
|
||
|
||
public void SendByMain(byte[] data)
|
||
{
|
||
socket_main.SendAsync(data);
|
||
}
|
||
|
||
public void SendByTrans(byte[] data)
|
||
{
|
||
socket_trans.SendAsync(data);
|
||
}
|
||
|
||
bool isSend = false;
|
||
private void OnMessage(object sender, MessageEventArgs e)
|
||
{
|
||
if (e.IsBinary)
|
||
{
|
||
//Debug.Log(string.Format("Receive Bytes ({1}): {0}", e.RawData, e.RawData.Length));
|
||
//收到消息
|
||
int type = BitConverter.ToInt32(e.RawData, 0);
|
||
byte[] data = e.RawData;
|
||
if (type == 99)
|
||
{
|
||
//收到id (initScene)
|
||
ushort tmpid = BitConverter.ToUInt16(data, 4);
|
||
Debug.Log("我的syncid为" + tmpid);
|
||
byte[] myGrid = new byte[] { data[7], data[8] };
|
||
mySyncid = tmpid;
|
||
if (MyPlayer.Me == null)
|
||
{
|
||
//第一次连接
|
||
if (!MyPlayer.players.ContainsKey(tmpid))
|
||
{
|
||
mySyncid = tmpid;
|
||
GameObject player = GameManager.Instance.GeneratePlayers(true,myGrid);
|
||
MyPlayer.Me = player.AddComponent<MyPlayer>();
|
||
MyPlayer.Me.Init(tmpid, true,myGrid);
|
||
InitTrans(data[6]);
|
||
ConnectTrans();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//重连
|
||
MyPlayer.Me.Init(tmpid, true, null);
|
||
}
|
||
GetData();
|
||
}
|
||
else if (type == 100)
|
||
{
|
||
//房间数据 (GameSence)
|
||
int count = BitConverter.ToInt32(data, 4);
|
||
int num = 8;
|
||
Debug.Log("获取数据,房间共有" + count + "人");
|
||
for (int index = 0; index < count; index++)
|
||
{
|
||
ushort id = BitConverter.ToUInt16(data, num);
|
||
num += 2;
|
||
if (!MyPlayer.players.ContainsKey(id) && mySyncid != id)
|
||
{
|
||
Debug.Log("id为:" + id+",编号为:"+ data[num]+","+data[num + 1]);
|
||
byte[] myGrid = new byte[] { data[num], data[num+1] };
|
||
GameObject obj = GameManager.Instance.GeneratePlayers(false, myGrid);
|
||
MyPlayer player= obj.AddComponent<MyPlayer>();
|
||
player.Init(id, false, myGrid);
|
||
}
|
||
num += 2;
|
||
}
|
||
//允许发送
|
||
if (MyPlayer.Me != null)
|
||
{
|
||
MyPlayer.Me.ReadySend = true;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("me为空");
|
||
}
|
||
}
|
||
else if (type == 101)
|
||
{
|
||
//有人上线(不发给自己)
|
||
ushort tmpid = BitConverter.ToUInt16(data, 4);
|
||
if (!MyPlayer.players.ContainsKey(tmpid) && mySyncid != tmpid)
|
||
{
|
||
byte[] myGrid = new byte[] { data[data.Length - 2], data[data.Length - 1] };
|
||
GameObject obj = GameManager.Instance.GeneratePlayers(false, myGrid);
|
||
MyPlayer player = obj.AddComponent<MyPlayer>();
|
||
player.Init(tmpid, false, myGrid);
|
||
|
||
Debug.Log("有人上线:" + tmpid);
|
||
}
|
||
|
||
if (!isSend)
|
||
{
|
||
isSend = true;
|
||
Invoke("SendToOther", 5f);
|
||
}
|
||
}
|
||
else if (type == 102)
|
||
{
|
||
//有人下线
|
||
ushort tmpid = BitConverter.ToUInt16(data, 4);
|
||
if (MyPlayer.players.ContainsKey(tmpid) && mySyncid != tmpid)
|
||
{
|
||
//删除
|
||
MyPlayer player = MyPlayer.players[tmpid];
|
||
MyPlayer.players.Remove(tmpid);
|
||
GameManager.Instance.RecoveryPlayers(player);
|
||
}
|
||
}
|
||
else if(type==103)
|
||
{
|
||
//各子改变
|
||
ushort id = BitConverter.ToUInt16(data, 4);
|
||
MyPlayer.Me.OnCellChange(id, new byte[] { data[6], data[7] });
|
||
}
|
||
else if(type== 105)
|
||
{
|
||
//ping
|
||
SendPing();
|
||
}
|
||
else if (type == 1001)
|
||
{
|
||
//玩家移动
|
||
if (MyPlayer.Me.ReadySend)
|
||
{
|
||
int count = BitConverter.ToInt32(data, 4);
|
||
int index = 8;
|
||
Debug.Log(count + "个人动");
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
//id
|
||
ushort tmpsyncid = BitConverter.ToUInt16(data, index);
|
||
index += 2;
|
||
//判断位
|
||
byte zhou = data[index];
|
||
index++;
|
||
|
||
//位置
|
||
if (zhou<<7 != 0)
|
||
{
|
||
float posx = BitConverter.ToSingle(data, index);
|
||
index += 4;
|
||
MyPlayer.players[tmpsyncid].SetPx(posx);
|
||
}
|
||
var b = zhou >> 1;
|
||
if (b<<7 != 0)
|
||
{
|
||
float posy = BitConverter.ToSingle(data, index);
|
||
index += 4;
|
||
MyPlayer.players[tmpsyncid].SetPy(posy);
|
||
}
|
||
|
||
var c = zhou >> 2;
|
||
if (c<<7 != 0)
|
||
{
|
||
float posz = BitConverter.ToSingle(data, index);
|
||
index += 4;
|
||
MyPlayer.players[tmpsyncid].SetPz(posz);
|
||
}
|
||
|
||
var d = zhou >> 3;
|
||
if (d<<7 != 0)
|
||
{
|
||
float roty = BitConverter.ToSingle(data, index);
|
||
index += 4;
|
||
MyPlayer.players[tmpsyncid].SetRy(roty);
|
||
}
|
||
MyPlayer.players[tmpsyncid].ChangeOnce();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else if (e.IsText)
|
||
{
|
||
Debug.Log(string.Format("Receive: {0}", e.Data));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送给别人自己的位置
|
||
/// </summary>
|
||
private void SendToOther()
|
||
{
|
||
//发送自己的位置
|
||
isSend = false;
|
||
MyPlayer.Me.SendSyncAnyWay();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 只发type
|
||
/// </summary>
|
||
/// <param name="type"></param>
|
||
private void SendOnlyType(int type)
|
||
{
|
||
SendByMain(BitConverter.GetBytes(type));
|
||
}
|
||
/// <summary>
|
||
/// 获取房间数据
|
||
/// </summary>
|
||
public void GetData()
|
||
{
|
||
byte[] data = new byte[6];
|
||
Array.Copy(BitConverter.GetBytes(100), 0, data, 0, 4);
|
||
Array.Copy(BitConverter.GetBytes(mySyncid), 0, data, 4, 2);
|
||
SendByMain(data);
|
||
}
|
||
/// <summary>
|
||
/// 发送心跳
|
||
/// </summary>
|
||
private void SendPing()
|
||
{
|
||
byte[] senddata = new byte[6];
|
||
Array.Copy(BitConverter.GetBytes(105), 0, senddata, 0, 4);
|
||
Array.Copy(BitConverter.GetBytes(mySyncid), 0, senddata, 4, 2);
|
||
SendByMain(senddata);
|
||
//Debug.Log("发送心跳");
|
||
}
|
||
} |