首先需要用到 Best HTTP (Pro) 插件 链接:https://download.csdn.net/download/qq_43912395/19791052
直接上代码,里面已经包含了注释
using BestHTTP.WebSocket;
using UnityEngine;
using System;
using BestHTTP.WebSocket.Frames;
using LitJson;
public class WebSocket2 : MonoBehaviour {
private static WebSocket2 instance;
public static WebSocket webSocket;
public static string tempMsg = "";
public static WebSocket2 GetInstance()
{
if (instance == null) {
Debug.LogError("instance为空");
instance = new GameObject("WebSocket2").AddComponent<WebSocket2>();
instance.Init();
DontDestroyOnLoad(instance.gameObject);
}
return instance;
}
private void Awake()
{
instance = this;
DontDestroyOnLoad(instance.gameObject);
}
public static bool IsExist {
get
{
return (instance != null);
}
}
bool IsInit = false;
private void Start()
{
Init();
}
private void Init()
{
if (!IsInit) {
IsInit = true;
Debug.Log(" ssssss " + GVars.payIp + ":" + GVars.payPort);
webSocket = new WebSocket(new Uri("ws://" + GVars.payIp + &