using System; using System.Runtime.CompilerServices; namespace Fantasy.Helper { /// /// WebSocket帮助类 /// public static partial class WebSocketHelper { /// /// 根据字符串获取WebSocket的连接地址 /// /// 目标服务器地址格式为:127.0.0.1:2000 /// 目标服务器是否为加密连接也就是https /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string GetWebSocketAddress(string address, bool isHttps) { var addressSplit = address.Split(':'); if (addressSplit.Length != 2) { throw new FormatException("Invalid format"); } var ipString = addressSplit[0]; var portString = addressSplit[1]; if (!int.TryParse(portString, out var port) || port < 0 || port > 65535) { throw new FormatException("Invalid port number"); } return isHttps ? $"wss://{ipString}:{portString}" : $"ws://{ipString}:{portString}"; } } }