huangxiaoqiang
2025-05-19 051aa9a0e7f58af6665fb4526e42cf8060fbaa05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Buffers;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
 
public class WebSocketClientService : IWebSocketClientService, IDisposable
{
    private readonly ILogger<WebSocketClientService> _logger;
    private readonly string _serverUrl;
    private readonly CancellationTokenSource _cts = new();
    private readonly SemaphoreSlim _connectionLock = new(1, 1);
 
    private ClientWebSocket _webSocket = new();
    private Timer _heartbeatTimer;
    private DateTime _lastPongReceived = DateTime.MinValue;
    private const int HeartbeatInterval = 30000; // 30秒
    private const int PongTimeout = 60000; // 60秒
 
    public event EventHandler<string> MessageReceived;
    public bool IsConnected => _webSocket?.State == WebSocketState.Open;
 
    public WebSocketClientService(ILogger<WebSocketClientService> logger)
    {
        _logger = logger;
        _serverUrl ="ws://localhost:5050";
    }
 
    public async Task ConnectAsync()
    {
        await _connectionLock.WaitAsync();
        try
        {
            if (IsConnected)
            {
                _logger.LogWarning("WebSocket 已连接");
                return;
            }
 
            // 清理旧连接
            //await SafeDisconnectAsync();
 
            _webSocket = new ClientWebSocket();
            _lastPongReceived = DateTime.UtcNow;
 
            try
            {
                await _webSocket.ConnectAsync(new Uri(_serverUrl), _cts.Token);
                _logger.LogInformation("成功连接到 {ServerUrl}", _serverUrl);
 
                // 启动心跳和接收消息
                StartHeartbeat();
                _ = ReceiveMessagesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "连接失败");
                await SafeDisconnectAsync();
                throw;
            }
        }
        finally
        {
            _connectionLock.Release();
        }
    }
 
    private void StartHeartbeat()
    {
        _heartbeatTimer?.Dispose();
        _heartbeatTimer = new Timer(
            async _ => await SendHeartbeatAsync(),
            null,
            HeartbeatInterval,
            HeartbeatInterval);
    }
 
    private async Task SendHeartbeatAsync()
    {
        if (!IsConnected) return;
 
        try
        {
            // 检查PONG响应超时
            if ((DateTime.UtcNow - _lastPongReceived).TotalMilliseconds > PongTimeout)
            {
                _logger.LogWarning("PONG响应超时,重新连接...");
                await ReconnectAsync();
                return;
            }
 
            // 发送PING
            await SendAsync("PONG");
            _logger.LogDebug("发送心跳检测");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "发送心跳失败");
            await ReconnectAsync();
        }
    }
 
    private async Task ReceiveMessagesAsync()
    {
        var buffer = ArrayPool<byte>.Shared.Rent(4096);
        try
        {
            while (IsConnected && !_cts.Token.IsCancellationRequested)
            {
                try
                {
                    var result = await _webSocket.ReceiveAsync(
                        new ArraySegment<byte>(buffer),
                        _cts.Token);
                    var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
                    if (result.MessageType == WebSocketMessageType.Close)
                    {
                        await DisconnectAsync();
                        break;
                    }
 
                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        
                        MessageReceived?.Invoke(this, message);
                        _logger.LogDebug("收到消息: {Message}", message);
                    }
                    // 可添加对二进制消息的处理
                    else if (result.MessageType == WebSocketMessageType.Binary)
                    {
                        _logger.LogWarning("收到二进制消息,忽略");
                    }
 
                    // 如果是最后一片消息且是心跳响应
                    if (result.EndOfMessage && message == "PONG")
                    {
                        _lastPongReceived = DateTime.UtcNow;
                        _logger.LogDebug("收到心跳响应");
                    }
                }
                catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
                {
                    _logger.LogWarning("连接提前关闭");
                    await DisconnectAsync();
                    break;
                }
                catch (OperationCanceledException)
                {
                    _logger.LogDebug("接收消息操作已取消");
                    break;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "接收消息错误");
                    await ReconnectAsync();
                    break;
                }
            }
        }
        finally
        {
            ArrayPool<byte>.Shared.Return(buffer);
        }
    }
 
    public async Task SendAsync(string message)
    {
        if (_webSocket == null || _webSocket.State != WebSocketState.Open)
        {
            _logger.LogWarning("WebSocket 未连接,尝试重新连接");
            await ReconnectAsync();
            if (_webSocket == null || _webSocket.State != WebSocketState.Open)
            {
                _logger.LogError("无法重新连接到 WebSocket");
                return;
            }
        }
 
        var buffer = Encoding.UTF8.GetBytes(message);
        var segment = new ArraySegment<byte>(buffer);
 
        try
        {
            await _webSocket.SendAsync(segment, WebSocketMessageType.Text, true, _cts.Token);
            _logger.LogDebug("消息发送成功: {Message}", message);
        }
        catch (WebSocketException ex)
        {
            _logger.LogError(ex, "发送消息时 WebSocket 异常");
            await DisconnectAsync();
        }
        catch (OperationCanceledException)
        {
            _logger.LogDebug("发送消息操作已取消");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "发送消息时发生未知错误");
            await ReconnectAsync();
        }
    }
 
    public async Task DisconnectAsync()
    {
        if (_webSocket != null)
        {
            try
            {
                if (_webSocket.State == WebSocketState.Open)
                {
                    await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Connection closed normally", CancellationToken.None);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "关闭WebSocket连接时出错");
            }
            finally
            {
                _webSocket.Dispose();
                _webSocket = null;
            }
        }
    }
 
    private async Task SafeDisconnectAsync()
    {
        try
        {
            _heartbeatTimer?.Dispose();
            _heartbeatTimer = null;
 
            if (_webSocket?.State == WebSocketState.Open)
            {
                await _webSocket.CloseAsync(
                    WebSocketCloseStatus.NormalClosure,
                    "正常关闭",
                    CancellationToken.None);
            }
        }
        catch (Exception ex)
        {
            _logger.LogWarning(ex, "关闭连接时出错");
        }
        finally
        {
            _webSocket?.Dispose();
            _webSocket = new ClientWebSocket();
            _logger.LogInformation("连接已关闭");
        }
    }
 
    private async Task ReconnectAsync()
    {
        await DisconnectAsync();
 
        int retryCount = 0;
        const int maxRetries = 5;
        const int delayMilliseconds = 1000;
 
        while (retryCount < maxRetries && !_cts.Token.IsCancellationRequested)
        {
            try
            {
                await ConnectAsync(); // 假设 ConnectAsync 是你的连接方法
                _logger.LogInformation("重新连接成功");
                return;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "重新连接失败");
                retryCount++;
                if (retryCount < maxRetries)
                {
                    await Task.Delay(delayMilliseconds, _cts.Token);
                }
            }
        }
 
        _logger.LogError("达到最大重试次数,无法重新连接");
    }
 
 
    public void Dispose()
    {
        _cts.Cancel();
        _heartbeatTimer?.Dispose();
        _webSocket?.Dispose();
        _connectionLock.Dispose();
        _cts.Dispose();
        GC.SuppressFinalize(this);
    }
}