Parcourir la source

兼容一下ip的形式,方便连本地服务开发.

hexiaojie il y a 1 mois
Parent
commit
fb4812db3b

+ 1 - 1
GameClient/Assets/Game/HotUpdate/GameConfig.cs

@@ -31,7 +31,7 @@ namespace GFGGame
             LoginAddress = result.loginApiUrl;
             //LoginAddress = "43.139.184.240:10003";
             //LoginAddress = "129.204.4.238:11005";//测试地址
-            //LoginAddress = "192.168.1.2:10005";//测试地址       
+            //LoginAddress = "192.168.1.191:10005";//测试地址      
             showGM = int.Parse(result.showGM);
             if(!string.IsNullOrEmpty(result.openTime))
             {

+ 35 - 8
GameClient/Assets/Game/HotUpdate/NetworkTCP/WChannel_WebGL.cs

@@ -24,20 +24,47 @@ namespace ET
             this.ChannelType = ChannelType.Connect;
             this.Id = id;
 
-            string wsStr = $"ws://{ipEndPoint}";
-
-            Log.Debug($"wc address:{address}");
-            if (LauncherConfig.isHttps)
+            string wsStr;
+            bool isHttps = LauncherConfig.isHttps;
+            
+            // 检查address是否是URL格式
+            if (!string.IsNullOrEmpty(address) && (address.StartsWith("http://") || address.StartsWith("https://")))
+            {
+                // 如果是URL格式,转换为WebSocket协议
+                if (address.StartsWith("https://"))
+                {
+                    wsStr = address.Replace("https://", "wss://");
+                    isHttps = true;
+                }
+                else
+                {
+                    wsStr = address.Replace("http://", "ws://");
+                }
+            }
+            else if (ipEndPoint != null)
+            {
+                // 如果不是URL格式但有IPEndPoint,使用IP+端口方式
+                wsStr = $"ws://{ipEndPoint}";
+            }
+            else if (!string.IsNullOrEmpty(address))
             {
-                Uri uri = new Uri(address);
-                string hostAndPath = uri.Host + uri.AbsolutePath;
-                wsStr = $"wss://{hostAndPath}";
+                // 如果没有IPEndPoint但有address,直接使用address作为WebSocket地址
+                // 支持IP:端口格式
+                wsStr = address.StartsWith("ws://") || address.StartsWith("wss://") ? address : $"ws://{address}";
             }
+            else
+            {
+                // 无法构造WebSocket地址
+                Log.Error($"WChannel: Invalid address and ipEndPoint are both null");
+                throw new ArgumentException("Invalid address and ipEndPoint are both null");
+            }
+
+            Log.Debug($"wc address:{address}");
             Log.Debug($"wc wsStr:{wsStr}");
 
             WebSocket ws = new WebSocket(new Uri(wsStr));
 
-            if (LauncherConfig.isHttps)
+            if (isHttps || ipEndPoint == null)
             {
                 this.RemoteAddress = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
             }