90 lines
2.4 KiB
C#
90 lines
2.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using WebSocketSharp;
|
||
using WebSocketSharp.Server;
|
||
using Newtonsoft.Json.Linq;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
|
||
public class ServerHandler : WebSocketBehavior
|
||
{
|
||
bool enabled_=false;
|
||
protected override void OnClose(CloseEventArgs e)
|
||
{
|
||
Debug.Log("断开连接" + e.Reason);
|
||
MyServer.instance.handler = null;
|
||
MyServer.instance.console_log_message.Enqueue("断开连接");
|
||
}
|
||
|
||
protected override void OnError(ErrorEventArgs e)
|
||
{
|
||
Debug.LogError("OnError: }" + e.Message);
|
||
MyServer.instance.console_error_message.Enqueue(e.Message);
|
||
}
|
||
|
||
protected override void OnOpen()
|
||
{
|
||
Debug.Log("连接成功");
|
||
MyServer.instance.handler = this;
|
||
MyServer.instance.console_log_message.Enqueue("连接成功");
|
||
}
|
||
|
||
protected override void OnMessage(MessageEventArgs e)
|
||
{
|
||
if (e.IsText)
|
||
{
|
||
Debug.Log(e.Data);
|
||
JObject jb = JObject.Parse(e.Data);
|
||
int id = jb["event_id"].ToObject<int>();
|
||
string type = jb["type"].ToString();
|
||
if (type == "checkSdk")
|
||
{
|
||
string version = jb["data"].ToString();
|
||
ResultStatus isok;
|
||
string msg = "'";
|
||
if (version == MyServer.version)
|
||
{
|
||
isok = ResultStatus.SUCCESS;
|
||
}
|
||
else
|
||
{
|
||
isok = ResultStatus.FAILED_ERROR;
|
||
msg = "版本匹配,unity版本为:" + version;
|
||
}
|
||
|
||
MyServer.instance.SendMsgToSDK(id, isok, msg);
|
||
}
|
||
else
|
||
{
|
||
if (id == 1)
|
||
{
|
||
enabled_ = false;
|
||
string language = jb["language"].ToObject<string>();
|
||
if (language == SetJson.instance.ReadLanguage())
|
||
{
|
||
enabled_ = true;
|
||
}
|
||
}
|
||
if (enabled_)
|
||
{
|
||
//进入主线程
|
||
MyServer.instance.reciveData.Enqueue(jb);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("语言版本不正确");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public void SendMsg(string msg)
|
||
{
|
||
if (State == WebSocketState.Open)
|
||
{
|
||
Send(msg);
|
||
}
|
||
}
|
||
}
|