public async Task SendCommandAsync(ICommand command) { if (ClientWebSocket.State != WebSocketState.Open) { var exception = new DotNettyException("未与服务器连接,不能发送命令"); OnException?.Invoke(exception); return; } string commandJson = command.ToJson(); byte[] byteArray = _clientConfig.EncodingType.GetBytes(commandJson); var buffer = new ArraySegment <byte>(byteArray); await ClientWebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, _cancellationToken); }
public async Task ReconnectionAsync() { if (_clientConfig.ReconnectionNumber > 0 && _reconnectionNumber >= _clientConfig.ReconnectionNumber) { var exception = new DotNettyException($"已连接失败{_reconnectionNumber}次,请联系管理员。"); OnException?.Invoke(exception); await StopAsync(); throw exception; } if (_clientConfig.ReconnectionInterval < 1000) { throw new DotNettyException("至少等待1000ms"); } if (_isManualClose) { return; } OnSubMessage?.Invoke($"[{_reconnectionNumber + 1}]WebSocketClient将在{_clientConfig.ReconnectionInterval / 1000f:N2}秒后重新连接....", "重要"); await Task.Delay(_clientConfig.ReconnectionInterval, _cancellationToken); _reconnectionNumber++; await StartAsync(); }