经过网上多方实践发现这个代码是比较好的,源码找不到了这是修改了一部分的多余的你可以适当删减
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BestHTTP;
using BestHTTP.WebSocket;
using System;
using BestHTTP.Examples;
using UnityEngine.UI;
using System.Text;
using LitJson;
public class WSMgr : MonoBehaviour
{
public GameObject cube;
public static Student[] Students;
//public string url = "ws://localhost:8080/web1/websocket";
private string url = "ws://192.168.2.249:8099";
public InputField msg;
public Text console;
ReqData data = new ReqData();
string jsonstring;
bool isplayer=false;
private WebSocket webSocket;
private void Start()
{
init();
}
private void init()
{
webSocket = new WebSocket(new Uri(url));
webSocket.OnOpen += OnOpen;
webSocket.OnMessage += OnMessageReceived;
webSocket.OnError += OnError;
webSocket.OnClosed += OnClosed;
}
private void antiInit()
{
webSocket.OnOpen = null;
webSocket.OnMessage = null;
webSocket.OnError = null;
webSocket.OnClosed = null;
webSocket = null;
}
private void setConsoleMsg(string msg)
{
console.text = "Message: " + msg;
}
public void Connect()
{
webSocket.Open();
}
private byte[] getBytes(string message)
{
byte[] buffer = Encoding.Default.GetBytes(message);
return buffer;
}
public void Send()
{
//实例化
data.PlayerName = transform.name; //赋值
data.PlayerTR_x = transform.position.x;
data.PlayerTR_y = transform.position.y;
data.PlayerTR_z = transform.position.z;
data.PlayerRT_x = transform.localEulerAngles.x;
data.PlayerRT_y = transform.localEulerAngles.y;
data.PlayerRT_z = transform.localEulerAngles.z;
//将数据转换为json字符串
jsonstring = JsonUtility.ToJson(data);
//msg.text =transform.name+ Convert.ToString(transform.position);
webSocket.Send(jsonstring);
}
public void Send(string str)
{
webSocket.Send(str);
}
public void Close()
{
webSocket.Close();
}
#region WebSocket Event Handlers
/// <summary>
/// 当Web套接字打开时调用,我们准备发送和接收数据
/// </summary>
void OnOpen(WebSocket ws)
{
Instantiate(Resources.Load("Cube"));
Debug.Log("connected");
setConsoleMsg("Connected");
}
/// <summary>
/// 当我们从服务器收到文本消息时调用
/// </summary>
void OnMessageReceived(WebSocket ws, string message)
{
Debug.Log(message);
//Debug.Log(jsonstring);
JsonData jd = JsonMapper.ToObject(message);
string PlayerName = (string)jd["PlayerName"];
double PlayerTR_x = (double)jd["PlayerTR_x"];
double PlayerTR_y = (double)jd["PlayerTR_y"];
double PlayerTR_z = (double)jd["PlayerTR_z"];
float PlayerTR__x = (float)PlayerTR_x;
float PlayerTR__y = (float)PlayerTR_y;
float PlayerTR__z = (float)PlayerTR_z;
double PlayerRT_x = (double)jd["PlayerRT_x"];
double PlayerRT_y = (double)jd["PlayerRT_y"];
double PlayerRT_z = (double)jd["PlayerRT_z"];
float PlayerRT__x = (float)PlayerRT_x;
float PlayerRT__y = (float)PlayerRT_y;
float PlayerRT__z = (float)PlayerRT_z;
if (PlayerName != null&&!isplayer)
{
Instantiate(Resources.Load("Cube 1"));
isplayer = true;
}
GameObject go = GameObject.Find("Cube 1(Clone)");
Debug.Log(go.name);
go.transform.position = new Vector3(PlayerTR__x, PlayerTR__y, PlayerTR__z);
go.transform.localEulerAngles = new Vector3(PlayerRT__x, PlayerRT__y, PlayerRT__z);
//cube.transform.position = new Vector3(PlayerTR__x, PlayerTR__y, PlayerTR__z);
//cube.transform.localEulerAngles = new Vector3(PlayerRT__x, PlayerRT__y, PlayerRT__z);
Debug.Log(PlayerTR__x + "tx-----------");
//Debug.Log(PlayerTR__y + "ty-----------");
//Debug.Log(PlayerTR__z + "tz-----------");
//Debug.Log(PlayerRT__x + "rx-----------");
//Debug.Log(PlayerRT__y + "ry-----------");
//Debug.Log(PlayerRT__z + "rz-----------");
//JsonData jd = JsonMapper.ToObject(message);
//Debug.Log("status = " + (string)jd["status"]);
//Students = JsonTool.AddressToModelArray<Student>(message);
//foreach (Student s in Students)
//{
// Debug.Log(s.ToString());
//}
setConsoleMsg(message);
}
/// <summary>
/// 当Web套接字关闭时调用
/// </summary>
void OnClosed(WebSocket ws, UInt16 code, string message)
{
Debug.Log(message);
setConsoleMsg(message);
antiInit();
init();
}
private void OnDestroy()
{
if (webSocket != null && webSocket.IsOpen)
{
webSocket.Close();
antiInit();
}
}
/// <summary>
/// 在客户端发生错误时调用
/// </summary>
void OnError(WebSocket ws, Exception ex)
{
string errorMsg = string.Empty;
#if !UNITY_WEBGL || UNITY_EDITOR
if (ws.InternalRequest.Response != null)
errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
#endif
Debug.Log(errorMsg);
setConsoleMsg(errorMsg);
antiInit();
init();
}
#endregion
}