ict.shenzhi/Assets/Scripts/UI/MainScenesUI/Chat/ServerHandlerUI.cs

69 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WebSocketSharp;
using WebSocketSharp.Server;
public class ServerHandlerUI : WebSocketBehavior
{
protected override void OnClose(CloseEventArgs e)
{
Debug.Log("断开连接" + e.Reason);
ChatRequest.instance.handler = null;
}
protected override void OnError(ErrorEventArgs e)
{
Debug.LogError("OnError: }" + e.Message);
}
protected override void OnOpen()
{
Debug.Log("连接成功");
ChatRequest.instance.handler = this;
}
protected override void OnMessage(MessageEventArgs e)
{
if (e.IsText)
{
Debug.Log(e.Data);
JObject jb = JObject.Parse(e.Data);
int id = jb["eventid"].ToObject<int>();
string type = jb["type"].ToString();
if (type == "checkSdk")
{
string version = jb["data"].ToString();
ResultState isok;
string msg = "'";
if (version == ChatRequest.instance.version)
{
isok = ResultState.SUCCESS;
}
else
{
isok = ResultState.FAILED_ERROR;
msg = "版本匹配,unity版本为" + version;
}
ChatRequest.instance.SendMsgToSDK(id, isok, msg);
}
else
{
//进入主线程
ChatRequest.instance.reciveData.Enqueue(jb);
}
}
}
public void SendMsg(string msg)
{
if (State == WebSocketState.Open)
{
Send(msg);
}
}
}