Android/IOS 使用WebSocket

This commit is contained in:
fatiao 2026-01-27 10:42:02 +08:00
parent 065efca003
commit 881f1ca12d
5 changed files with 14 additions and 14 deletions

View File

@ -1,4 +1,4 @@
#define UNITY_INSTANTGAME
//#define UNITY_INSTANTGAME
using System;
using System.IO;

View File

@ -84,7 +84,7 @@ public class Connector : ISocketHandler
{
if (data == null) return false;
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
if(mSocketConn == null || mSocketConn.mWebSocket == null || mSocketConn.webState != UnityWebSocket.WebSocketState.Open)
{
return false;

View File

@ -104,7 +104,7 @@ public class NetworkMgr : Singleton<NetworkMgr>
public bool ConnectServer(string host, int port)
{
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
string ip = host;
#else
string ip = GetIp(host);
@ -267,7 +267,7 @@ public class NetworkMgr : Singleton<NetworkMgr>
gameSrv.HandleSending();
#if UNITY_WEBGL && UNITY_EDITOR
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID) && UNITY_EDITOR
//HandleLobbyMsgRecv();
#endif
}

View File

@ -29,7 +29,7 @@ public class SocketConn
enSocketState mSocketState;
ISocketHandler iHandler;
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
private WebSocket m_iSocket;
private string m_address;
private bool logMessage = false;
@ -99,7 +99,7 @@ public class SocketConn
iHandler.OnConnect();
}
}
#if !UNITY_WEBGL || UNITY_EDITOR
#if !(UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID) || UNITY_EDITOR
try
{
//检测网络是否出错
@ -146,7 +146,7 @@ public class SocketConn
String newServerIp = "";
AddressFamily newAddressFamily = AddressFamily.InterNetwork;
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
#else
if (!IPv6SupportMidleware.GetValidServerIpInfo(host, out newServerIp, out newAddressFamily))
{
@ -157,7 +157,7 @@ public class SocketConn
#endif
try
{
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
string address = "";
if (CommonUtil.IsValidIPv4(host))
{
@ -231,7 +231,7 @@ public class SocketConn
#endif
try
{
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
//m_address = string.Format("ws://{0}:{1}", mLastIP, mLastPort);
Debug.LogWarning("Begin reconnect to Socket Address :" + m_address);
m_iSocket = new WebSocket(m_address);
@ -292,7 +292,7 @@ public class SocketConn
public void Disconnect(bool ignoreEvent = false)
{
Debug.Log("Disconnect");
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
if (m_iSocket != null && m_iSocket.ReadyState != WebSocketState.Closed)
{
mPreSocketState = mSocketState;
@ -341,7 +341,7 @@ public class SocketConn
mSocketState = enSocketState.ConnectedTimeout;
m_fBeginConnectTime = -1f;
}
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
private void Socket_OnOpen(object sender, OpenEventArgs e)
{
Debug.Log(string.Format("webGl socket Connected: {0}\n", m_address));

View File

@ -1,4 +1,4 @@
#define UNITY_INSTANTGAME
//#define UNITY_INSTANTGAME
using System.Text;
using System;
@ -513,11 +513,11 @@ public class AssetsMgr : SingletonMono<AssetsMgr>
#endif
path = string.Format("{0}assetsmapping.bytes", FileSystem.LocalPackagePath);
#if UNITY_WEBGL
#if (UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
path = string.Format("{0}assetsmapping.bytes", FileSystem.LocalPackagePath);
#endif
}
#if !UNITY_WEBGL
#if !(UNITY_WEBGL || UNITY_IOS || UNITY_ANDROID)
if (!path.Contains("file://"))
{
path = "file://" + path;