Skip to content

Commit 930032e

Browse files
committed
[修改]1. 修改连接远程服务器的地址的参数为URI
1 parent 0a7d3fa commit 930032e

File tree

5 files changed

+19
-30
lines changed

5 files changed

+19
-30
lines changed

Runtime/Network/Interface/INetworkChannel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,9 @@ public interface INetworkChannel
177177
/// <summary>
178178
/// 连接到远程主机。
179179
/// </summary>
180-
/// <param name="ipAddress">远程主机的 IP 地址。</param>
181-
/// <param name="port">远程主机的端口号。</param>
180+
/// <param name="address">远程主机的地址。</param>
182181
/// <param name="userData">用户自定义数据。</param>
183-
/// <param name="isSsl">是否是加密</param>
184-
void Connect(IPAddress ipAddress, int port, object userData = null, bool isSsl = false);
182+
void Connect(Uri address, object userData = null);
185183

186184
/// <summary>
187185
/// 设置忽略指定的消息包的发送和接收ID列表

Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Net;
1212
using System.Net.Sockets;
13+
using System.Security.Policy;
1314
using System.Threading.Tasks;
1415
using GameFrameX.Runtime;
1516

@@ -544,20 +545,19 @@ public void SetRPCEndHandler(EventHandler<MessageObject> handler)
544545
/// <summary>
545546
/// 连接到远程主机。
546547
/// </summary>
547-
/// <param name="ipAddress">远程主机的 IP 地址。</param>
548-
/// <param name="port">远程主机的端口号。</param>
548+
/// <param name="address">远程主机的地址。</param>
549549
/// <param name="userData">用户自定义数据。</param>
550-
/// <param name="isSsl">是否加密</param>
551550
[UnityEngine.Scripting.Preserve]
552-
public virtual void Connect(IPAddress ipAddress, int port, object userData = null, bool isSsl = false)
551+
public virtual void Connect(Uri address, object userData = null)
553552
{
554553
if (PSocket != null)
555554
{
556555
Close();
557556
PSocket = null;
558557
}
559558

560-
switch (ipAddress.AddressFamily)
559+
EndPoint endPoint = new IPEndPoint(IPAddress.Parse(address.Host), address.Port);
560+
switch (endPoint.AddressFamily)
561561
{
562562
case System.Net.Sockets.AddressFamily.InterNetwork:
563563
PAddressFamily = AddressFamily.IPv4;
@@ -568,7 +568,7 @@ public virtual void Connect(IPAddress ipAddress, int port, object userData = nul
568568
break;
569569

570570
default:
571-
string errorMessage = Utility.Text.Format("Not supported address family '{0}'.", ipAddress.AddressFamily);
571+
string errorMessage = Utility.Text.Format("Not supported address family '{0}'.", endPoint.AddressFamily);
572572
if (NetworkChannelError != null)
573573
{
574574
NetworkChannelError(this, NetworkErrorCode.AddressFamilyError, SocketError.Success, errorMessage);

Runtime/Network/Network/SystemSocket/NetworkManager.SystemTcpNetworkChannel.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,18 @@ public SystemTcpNetworkChannel(string name, INetworkChannelHelper networkChannel
3737
/// <summary>
3838
/// 连接到远程主机。
3939
/// </summary>
40-
/// <param name="ipAddress">远程主机的 IP 地址。</param>
41-
/// <param name="port">远程主机的端口号。</param>
40+
/// <param name="address">远程主机的地址。</param>
4241
/// <param name="userData">用户自定义数据。</param>
43-
/// <param name="isSsl">是否是加密</param>
44-
public override void Connect(IPAddress ipAddress, int port, object userData = null, bool isSsl = false)
42+
public override void Connect(Uri address, object userData = null)
4543
{
4644
if (PIsConnecting)
4745
{
4846
return;
4947
}
5048

51-
m_ConnectedEndPoint = new IPEndPoint(ipAddress, port);
52-
base.Connect(ipAddress, port, userData, isSsl);
53-
PSystemNetSocket = new SystemNetSocket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
49+
m_ConnectedEndPoint = new IPEndPoint(IPAddress.Parse(address.Host), address.Port);
50+
base.Connect(address, userData);
51+
PSystemNetSocket = new SystemNetSocket(m_ConnectedEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
5452
PSocket = PSystemNetSocket;
5553
if (PSocket == null)
5654
{

Runtime/Network/Network/WebSocket/NetworkManager.WebSocketNetSocket.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ private sealed class WebSocketNetSocket : INetworkSocket
1414
{
1515
private readonly IWebSocket _client;
1616

17-
/// <summary>
18-
/// 是否是加密协议
19-
/// </summary>
20-
private readonly bool _isSSL = false;
21-
2217
/// <summary>
2318
/// 是否正在连接
2419
/// </summary>
@@ -28,10 +23,9 @@ private sealed class WebSocketNetSocket : INetworkSocket
2823
private Action<byte[]> _action;
2924
private Action<string> _onCloseAction;
3025

31-
public WebSocketNetSocket(IPAddress ipAddress, int port, bool isSSL, Action<byte[]> action, Action<string> onCloseAction)
26+
public WebSocketNetSocket(string url, Action<byte[]> action, Action<string> onCloseAction)
3227
{
33-
_isSSL = isSSL;
34-
_client = new UnityWebSocket.WebSocket("ws://" + ipAddress + ":" + port + "/" + (_isSSL ? "wss" : "ws"));
28+
_client = new UnityWebSocket.WebSocket(url);
3529
_action = action;
3630
_onCloseAction = onCloseAction;
3731
_client.OnOpen += OnOpen;

Runtime/Network/Network/WebSocket/NetworkManager.WebSocketNetworkChannel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ public WebSocketNetworkChannel(string name, INetworkChannelHelper networkChannel
3838
/// <summary>
3939
/// 连接到远程主机。
4040
/// </summary>
41-
/// <param name="ipAddress">远程主机的 IP 地址。</param>
42-
/// <param name="port">远程主机的端口号。</param>
41+
/// <param name="address">远程主机的地址。</param>
4342
/// <param name="userData">用户自定义数据。</param>
4443
/// <param name="isSsl">是否是加密</param>
45-
public override void Connect(IPAddress ipAddress, int port, object userData = null, bool isSsl = false)
44+
public override void Connect(Uri address, object userData = null)
4645
{
4746
if (PIsConnecting)
4847
{
4948
return;
5049
}
5150

52-
base.Connect(ipAddress, port, userData, isSsl);
53-
PSocket = new WebSocketNetSocket(ipAddress, port, isSsl, ReceiveCallback, CloseCallback);
51+
base.Connect(address, userData);
52+
PSocket = new WebSocketNetSocket(address.ToString(), ReceiveCallback, CloseCallback);
5453
if (PSocket == null)
5554
{
5655
const string errorMessage = "Initialize network channel failure.";

0 commit comments

Comments
 (0)