From 30726c22cb03e1f7b67d8c090e6d7e9bda406fe3 Mon Sep 17 00:00:00 2001 From: Elivo Date: Mon, 21 Apr 2025 10:35:54 +0800 Subject: [PATCH] =?UTF-8?q?WebSocket=EF=BC=8C=E6=9E=84=E9=80=A0=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=8F=82=E6=95=B0=E7=9A=84=20IP=20=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E4=BB=8E=20string=20=E6=94=B9=E4=B8=BA=20IPAddress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer.Web/WebSocket/ChatServer.cs | 12 +++-- Apewer.Web/WebSocket/GenericServer.cs | 66 +++++++++++++++++---------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/Apewer.Web/WebSocket/ChatServer.cs b/Apewer.Web/WebSocket/ChatServer.cs index db99b1d..4ba3425 100644 --- a/Apewer.Web/WebSocket/ChatServer.cs +++ b/Apewer.Web/WebSocket/ChatServer.cs @@ -4,6 +4,7 @@ using Apewer; using Apewer.Network; using System; using System.Collections.Generic; +using System.Net; namespace Apewer.WebSocket { @@ -127,18 +128,19 @@ namespace Apewer.WebSocket _websocket = new GenericServer(); lock (_websocket) { - var ws = _websocket.Start(_websocketport, _websocketaddress); - if (ws) + try { + _websocket.Start(_websocketport, IPAddress.Parse(_websocketaddress)); + _websocket.OnMessage += WebSocketReceived; _websocket.OnOpen += WebsocketOnOpen; _websocket.OnClose += WebsocketOnClose; RaiseConsole("WebSocket 服务已启动。"); } - else + catch (Exception ex) { _websocket = null; - RaiseError("WebSocket 服务启动失败。"); + RaiseError($"WebSocket 服务启动失败:{ex.Message()}"); return; } @@ -426,7 +428,7 @@ namespace Apewer.WebSocket { var input = Console.ReadLine(); if (input == "exit") break; - ws.Send(ws.Address, ":", ws.Port.ToString(), "\n", input); + ws.Send(ws.Address.ToString(), ":", ws.Port.ToString(), "\n", input); } ws.Close(); diff --git a/Apewer.Web/WebSocket/GenericServer.cs b/Apewer.Web/WebSocket/GenericServer.cs index 608fba7..cba756c 100644 --- a/Apewer.Web/WebSocket/GenericServer.cs +++ b/Apewer.Web/WebSocket/GenericServer.cs @@ -3,6 +3,7 @@ using Apewer; using System; using System.Collections.Generic; +using System.Net; namespace Apewer.WebSocket { @@ -11,15 +12,12 @@ namespace Apewer.WebSocket public sealed class GenericServer : IDisposable { - const string DefaultAddress = "0.0.0.0"; - const ushort DefaultPort = 8000; - private Dictionary _connections = new Dictionary(); private WebSocketServer _server = null; private bool _running = false; - private string _address = null; - private int _port = DefaultPort; + private IPAddress _address = null; + private int _port = 0; /// public event SocketEvent OnOpen; @@ -54,7 +52,7 @@ namespace Apewer.WebSocket } /// 监听的地址。 - public string Address + public IPAddress Address { get { return _address; } } @@ -101,28 +99,46 @@ namespace Apewer.WebSocket _running = false; } + /// 启动监听,如果监听正在运行则失败。在所有 IPv4 网络接口上自动选择可用的端口号。 + /// 已监听的端口号。 + /// + /// + public void Start() => Start(0, null); + /// 启动监听,如果监听正在运行则失败。 - public bool Start(int port = DefaultPort, string address = DefaultAddress) + /// 要监听的终结点。指定为 NULL 时将在所有 IPv4 网络接口上自动选择可用的端口号。 + /// 已监听的端口号。 + /// + /// + /// + public void Start(IPEndPoint endPoint) { - if (address == null) return false; - if (_running) return false; - - _address = address ?? ""; - _port = NumberUtility.Restrict( port, 0, ushort.MaxValue); + if (endPoint == null) Start(0, null); + else Start(endPoint.Port, endPoint.Address); + } - try - { - var location = "ws://" + address + ":" + port.ToString(); - _server = new WebSocketServer(location); - _server.Start(Initialize); - _running = true; - return true; - } - catch (Exception exception) - { - RaiseExcepted(exception); - return false; - } + /// 启动监听,如果监听正在运行则失败。 + /// 要监听的端口号。指定为 0 时将自动选择可用的端口号。 + /// 要监听的网络接口。指定为 NULL 时将在所有 IPv4 网络接口监听,等同于 。 + /// 已监听的端口号。 + /// + /// + /// + public void Start(int port, IPAddress address = null) + { + if (address == null) address = IPAddress.Any; + if (port < 0) throw new ArgumentOutOfRangeException(nameof(port)); + if (port > 65535) throw new ArgumentOutOfRangeException(nameof(port)); + if (_running) throw new InvalidOperationException("示例已经在运行中,无法再次启动。"); + + _address = address; + _port = port; + + var location = "ws://" + _address.ToString() + ":" + port.ToString(); + _server = new WebSocketServer(location); + _server.Start(Initialize); + _port = _server.Port; + _running = true; } /// 对所有连接发送文本。