349 lines
12 KiB
C#
349 lines
12 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using Newtonsoft.Json;
|
||
using System.Net.Sockets;
|
||
using UnityWebSocket;
|
||
using System.Text;
|
||
using Google.Protobuf;
|
||
using System.Net.WebSockets;
|
||
using System;
|
||
using System.Threading.Tasks;
|
||
public class test : MonoBehaviour
|
||
{
|
||
public string token;
|
||
public WEBScriptListener wEBScriptListener;
|
||
public class auth_login
|
||
{
|
||
public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e";
|
||
public string grantType = "password";
|
||
public string userType = "company_user";
|
||
public string username = "13699802230";
|
||
public string password = "YYL2230!";
|
||
}
|
||
public async void loging()
|
||
{
|
||
auth_login auth_Login = new auth_login();
|
||
string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(auth_Login));
|
||
|
||
// 解析服务器返回的数据
|
||
server serverData = JsonConvert.DeserializeObject<server>(response);
|
||
token = serverData.data.access_token;
|
||
//Debug.Log(setverData.msg);
|
||
|
||
}
|
||
//登录
|
||
void lodingWebSocket()
|
||
{
|
||
LoginRequest data = new LoginRequest();
|
||
data.UserId = "1846032755921199105";
|
||
data.Account = "13152628999";
|
||
|
||
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "hall";
|
||
msg.ServiceName = "Login";
|
||
msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
LoginRequest login = ProtoBufffer.DeSerialize<LoginRequest>(deinfo.Data.ToByteArray());
|
||
Debug.Log("登录");
|
||
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
}
|
||
//加入房间
|
||
void JoinRoom()
|
||
{
|
||
JoinRoomRequest data = new JoinRoomRequest();
|
||
data.RoomId = "168888";
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "hall";
|
||
msg.ServiceName = "JoinRoom";
|
||
msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
JoinRoomRequest login = ProtoBufffer.DeSerialize<JoinRoomRequest>(deinfo.Data.ToByteArray());
|
||
Debug.Log("加入房间");
|
||
//BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
}
|
||
|
||
//void BroadcastMessage()
|
||
//{
|
||
// WSMessage msg = new WSMessage();
|
||
// msg.Module = "hall";
|
||
// msg.ServiceName = "BroadcastMessage";
|
||
// //msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
// byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
// WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
// JoinRoomRequest login = ProtoBufffer.DeSerialize<JoinRoomRequest>(deinfo.Data.ToByteArray());
|
||
// Debug.Log("加入房间");
|
||
// //BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
|
||
// wEBScriptListener.SendMessageByte(sendData);
|
||
//}
|
||
|
||
//心跳
|
||
async void Heartbeat()
|
||
{
|
||
//WSMessage msg = new WSMessage();
|
||
//msg.Data = ByteString.CopyFrom(new byte[] { 1 });
|
||
//byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
//Debug.Log("");
|
||
while (true)
|
||
{
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "ping";
|
||
//msg.Data = ByteString.CopyFrom(new byte[] { 1 });
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
// 每秒执行的任务
|
||
Debug.Log("每秒执行一次");
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
|
||
// 等待一秒
|
||
await Task.Delay(1000);
|
||
}
|
||
}
|
||
//帧存储操作输入
|
||
//void UpFps()
|
||
//{
|
||
// InputData data = new InputData();
|
||
// data.RoomId = "168888";
|
||
// data.Id = "778899";
|
||
// data.SId = "7";
|
||
// data.X = 7f;
|
||
// data.Y = 5f;
|
||
// data.Z = 4f;
|
||
// data.RoomSeatId = 5;
|
||
// WSMessage msg = new WSMessage();
|
||
// msg.Module = "fps";
|
||
// msg.ServiceName = "UpFps";
|
||
// msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
// byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
// WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
// InputData login = ProtoBufffer.DeSerialize<InputData>(deinfo.Data.ToByteArray());
|
||
// Debug.Log("帧存储操作输入");
|
||
// //BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
// wEBScriptListener.SendMessageByte(sendData);
|
||
//}
|
||
|
||
//拾取道具
|
||
void PickUpProp()
|
||
{
|
||
PropRequest data = new PropRequest();
|
||
data.PropId = 1001;
|
||
data.RoomId = "168888";
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "prop";
|
||
msg.ServiceName = "PickUpProp";
|
||
msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
PropRequest login = ProtoBufffer.DeSerialize<PropRequest>(deinfo.Data.ToByteArray());
|
||
Debug.Log("拾取道具");
|
||
//BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
}
|
||
|
||
//使用道具
|
||
void StartUseProp()
|
||
{
|
||
PropRequest data = new PropRequest();
|
||
data.RoomId = "168888";
|
||
data.PropId = 1001;
|
||
data.Action = 1;
|
||
data.PropServerId = "MTg1OTc5MDMwOTIyODc4OTc2MA==";
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "prop";
|
||
msg.ServiceName = "StartUseProp";
|
||
msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
PropRequest login = ProtoBufffer.DeSerialize<PropRequest>(deinfo.Data.ToByteArray());
|
||
Debug.Log("使用道具");
|
||
//BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
}
|
||
//停止使用道具
|
||
void StopUseProp()
|
||
{
|
||
PropRequest data = new PropRequest();
|
||
data.RoomId = "168888";
|
||
data.PropId = 1001;
|
||
data.PropServerId = "MTg1OTc5MDMwOTIyODc4OTc2MA==";
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "prop";
|
||
msg.ServiceName = "StopUseProp";
|
||
msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
PropRequest login = ProtoBufffer.DeSerialize<PropRequest>(deinfo.Data.ToByteArray());
|
||
Debug.Log("停止使用道具");
|
||
//BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
}
|
||
|
||
//创建NPC ========================暂时不需要
|
||
//void CreateNpc()
|
||
//{
|
||
// PropRequest data = new PropRequest();
|
||
// data.RoomId = "168888";
|
||
// WSMessage msg = new WSMessage();
|
||
// msg.Module = "move";
|
||
// msg.ServiceName = "CreateNpc";
|
||
// msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
// byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
// WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
// PropRequest login = ProtoBufffer.DeSerialize<PropRequest>(deinfo.Data.ToByteArray());
|
||
// Debug.Log("创建NPC");
|
||
// //BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
// wEBScriptListener.SendMessageByte(sendData);
|
||
//}
|
||
|
||
//推送npc和玩家的信息 ===============只需要收消息
|
||
//void Location()
|
||
//{
|
||
// PropRequest data = new PropRequest();
|
||
// data.RoomId = "168888";
|
||
// WSMessage msg = new WSMessage();
|
||
// msg.Module = "prop";
|
||
// msg.ServiceName = "CreateNpc";
|
||
// msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
// byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
// WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
// PropRequest login = ProtoBufffer.DeSerialize<PropRequest>(deinfo.Data.ToByteArray());
|
||
// Debug.Log("推送npc和玩家的信息");
|
||
// //BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
// wEBScriptListener.SendMessageByte(sendData);
|
||
//}
|
||
|
||
|
||
|
||
//开始计时
|
||
void RoomStartTime()
|
||
{
|
||
JoinRoomRequest data = new JoinRoomRequest();
|
||
data.RoomId = "168888";
|
||
WSMessage msg = new WSMessage();
|
||
msg.Module = "hall";
|
||
msg.ServiceName = "RoomStartTime";
|
||
msg.Data = ByteString.CopyFrom(ProtoBufffer.Serialize(data));
|
||
byte[] sendData = ProtoBufffer.Serialize(msg);
|
||
WSMessage deinfo = ProtoBufffer.DeSerialize<WSMessage>(sendData);
|
||
JoinRoomRequest login = ProtoBufffer.DeSerialize<JoinRoomRequest>(deinfo.Data.ToByteArray());
|
||
Debug.Log("推送npc和玩家的信息");
|
||
//BroadcastFrameMsg.FramesFieldNumber
|
||
|
||
wEBScriptListener.SendMessageByte(sendData);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
//loging();
|
||
wEBScriptListener.ConcatWEBSocket();
|
||
wEBScriptListener.OnCallback += callback;
|
||
if (wEBScriptListener != null)
|
||
{
|
||
lodingWebSocket();
|
||
JoinRoom();
|
||
//Heartbeat();
|
||
//BroadcastMessage();
|
||
//UpFps();
|
||
PickUpProp();
|
||
StartUseProp();
|
||
StopUseProp();
|
||
//RoomStartTime();
|
||
|
||
}
|
||
}
|
||
|
||
void callback(byte[] data)
|
||
{
|
||
WSResponse deinfo = ProtoBufffer.DeSerialize<WSResponse>(data);
|
||
Debug.Log("返回数据类型:"+deinfo.MessageType);
|
||
byte[] bytes = deinfo.Data.ToByteArray();
|
||
switch (deinfo.MessageType)
|
||
{
|
||
//监听
|
||
//登录
|
||
case "hall.Login":
|
||
LoginResponse user = ProtoBufffer.DeSerialize<LoginResponse>(bytes);
|
||
Debug.Log(user.Message);
|
||
break;
|
||
|
||
//加入房间
|
||
case "hall.JoinRoom":
|
||
JoinRoomResponse joinRoomResponse = ProtoBufffer.DeSerialize<JoinRoomResponse>(bytes);
|
||
byte[] joinByte = joinRoomResponse.Data.ToByteArray();
|
||
PlayerJoinResponse playerJoinResponse = ProtoBufffer.DeSerialize<PlayerJoinResponse>(joinByte);
|
||
Debug.Log("玩家:"+playerJoinResponse.UserName+"加入房间:"+playerJoinResponse.RoomId.ToString());
|
||
break;
|
||
|
||
//广播
|
||
case "hall.BroadcastMessage":
|
||
UserJoinResponse userJoinResponse = ProtoBufffer.DeSerialize<UserJoinResponse>(bytes);
|
||
Debug.Log("玩家昵称:" + userJoinResponse.NickName + ",房间:" + userJoinResponse.RoomId.ToString() + ",用户角色"+ userJoinResponse.RoleName);
|
||
Debug.Log(userJoinResponse.IncidentPosition);
|
||
break;
|
||
|
||
//
|
||
case "fps.UpFps":
|
||
InputData inputData = ProtoBufffer.DeSerialize<InputData>(bytes);
|
||
Debug.Log("Id:" + inputData.Id + ",sId :" + inputData.SId + ",X:"+ inputData.X.ToString() + ",Y:" + inputData.Y.ToString() + ",roomSeatId :" + inputData.RoomSeatId + ",z:" + inputData.Z.ToString() + ",roomId:" + inputData.RoomId);
|
||
break;
|
||
|
||
case "prop.PickUpProp":
|
||
PropRequest propRequest = ProtoBufffer.DeSerialize<PropRequest>(bytes);
|
||
Debug.Log("PropId:" + propRequest.PropId);
|
||
break;
|
||
|
||
case "prop.StartUseProp":
|
||
PropResponse useProp = ProtoBufffer.DeSerialize<PropResponse>(bytes);
|
||
|
||
Debug.Log("PropId:" + useProp);
|
||
break;
|
||
|
||
case "prop.StopUseProp":
|
||
PropResponse stopuseProp = ProtoBufffer.DeSerialize<PropResponse>(bytes);
|
||
|
||
Debug.Log("PropId:" + stopuseProp);
|
||
break;
|
||
|
||
//case "prop.CreateNpc":
|
||
// MoveResponse moveResponse = ProtoBufffer.DeSerialize<MoveResponse>(bytes);
|
||
// Debug.Log("UserId:" + moveResponse.UserId + ",RoomId:" + moveResponse.RoomId + ",x:" + moveResponse.MoveData);
|
||
// break;
|
||
|
||
|
||
|
||
//case "hall.RoomStartTime":
|
||
// JoinRoomRequest joinRoomRequest = ProtoBufffer.DeSerialize<JoinRoomRequest>(bytes);
|
||
// Debug.Log("PropId:" + joinRoomRequest.RoomId);
|
||
// break;
|
||
//case " hall.MessageToUser":
|
||
// //JoinRoomRequest joinRoomRequest = ProtoBufffer.DeSerialize<JoinRoomRequest>(bytes);
|
||
// //Debug.Log("PropId:" + joinRoomRequest.RoomId);
|
||
// break;
|
||
|
||
}
|
||
}
|
||
|
||
}
|