From 00ee9c35abb5317dc57e4f5419b1d185d6198c00 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 12 Dec 2022 17:43:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=88=98=E6=96=97=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client.cs | 5 +++++ Service.cs | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/Client.cs b/Client.cs index 55e6df9..f377022 100644 --- a/Client.cs +++ b/Client.cs @@ -13,6 +13,11 @@ namespace BattleServer { socket = _socket; socket.OnBinary = doBinary; + socket.OnClose = close; + } + + private void close (){ + Service.Instance.Shutdown(this); } /// diff --git a/Service.cs b/Service.cs index 733e7d9..be12cf1 100644 --- a/Service.cs +++ b/Service.cs @@ -10,6 +10,7 @@ namespace BattleServer /// class Service { + public static Service Instance; private WebSocketServer server; private List clients; @@ -18,6 +19,7 @@ namespace BattleServer var server = new WebSocketServer("ws://127.0.0.1:9897"); //创建webscoket服务端实例 server.Start(onAccept); clients = new List(); + Instance = this; } private void onAccept(IWebSocketConnection webSocket) @@ -26,5 +28,10 @@ namespace BattleServer Console.WriteLine("新的连接对象进入{0}", client.ToString()); clients.Add(client); } + + public void Shutdown(Client client){ + Console.WriteLine("连接对象断开{0}", client.ToString()); + this.clients.Remove(client); + } } } \ No newline at end of file