/// <summary> /// 连接websocketServer /// </summary> /// <returns></returns> public bool Connect(int timeOut = 10 * 1000) { _client.ConnectAsync((e) => { _client.SendAsync(WSUserToken.RequestHandShark(_url, _serverIP, _serverPort, _subProtocol, _origin)); }); var to = timeOut / 10; int i = 0; while (!_isHandSharked && i < to) { Thread.Sleep(10); i++; } if (_isHandSharked) { return(true); } _client.Disconnect(); return(false); }
void HeartAsync() { TaskHelper.Start(() => { try { while (true) { if (_client.Connected) { if (Actived.AddMilliseconds(HeartSpan) <= DateTimeHelper.Now) { var sm = new BaseSocketProtocal() { BodyLength = 0, Type = (byte)SocketProtocalType.Heart }; _client.SendAsync(sm.ToBytes()); } ThreadHelper.Sleep(HeartSpan); } else { ThreadHelper.Sleep(1000); } } } catch { } }); }
private void _batcher_OnBatched(IBatcher batcher, List <byte[]> data) { if (data != null && data.Any()) { var list = new List <byte>(); foreach (var item in data) { list.AddRange(item); } _clientSocket.SendAsync(list.ToArray()); list.Clear(); } }
public void SendAsyncMessage() { var line = Encoding.UTF8.GetBytes(new string('r', BufferSize) + Environment.NewLine); while (Totals > 0) { _client.SendAsync(line, 0).ConfigureAwait(false); Totals--; } }
private static void CreateClientSocket() { _serverConfig = _serviceProvider.GetRequiredService <IOptions <ServerConfig> >()?.Value; _client = _serviceProvider.GetRequiredService <IClientSocket>(); _timers.Start(); Task.Run(async() => await _client.ConnectAsync(_serverConfig.IpAddress, _serverConfig.Port) .ConfigureAwait(false)) .ContinueWith(tresult => { if (tresult.Status == TaskStatus.Faulted) { Console.WriteLine($"connect {_serverConfig.IpAddress}:{_serverConfig.Port} faulted"); _needReConnecting = true; } }); string line = null; while ((line = Console.ReadLine()) != "") { if (line == "r") { Console.WriteLine("Reconnecting..."); _client.Disconnect(); Task.Run(async() => await _client.ConnectAsync(_serverConfig.IpAddress, _serverConfig.Port)) .ContinueWith(tresult => { if (tresult.Status == TaskStatus.Faulted) { Console.WriteLine($"Connected Faulted."); } else { Console.WriteLine($"IsConnected = {_client.IsConnected}"); } }); } else if (line == "exit") { _client.Disconnect(); break; } else { Console.WriteLine($"{line} Sending.."); _client.SendAsync(line, 3, ErrorMessageCallback).ConfigureAwait(false); } } }
/// <summary> /// 基础发送数据 /// </summary> /// <param name="protocalType"></param> /// <param name="data"></param> void BaseSend(ProtocalType protocalType, byte[] data) { var p = new BaseSocketProtocal(); p.Type = (byte)protocalType; p.Content = data; if (p.Content != null) { p.BodyLength = p.Content.Length; } else { p.BodyLength = 0; } _udpClient.SendAsync(p.ToBytes()); }
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { while (!_clientSocket.Connected) { Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(true); if (cancellationToken.IsCancellationRequested) { throw new MqttCommunicationTimedOutException(); } } ChannelManager.Instance.Refresh(_clientSocket.Endpoint); return(_clientSocket.SendAsync(buffer, offset, count, cancellationToken)); }
public void SendAsync(ushort mainCmd, ushort subCmd, byte[] datas) { _client.SendAsync(Package.ToArray(17408, mainCmd, subCmd, datas)); }
/// <summary> /// 发送数据 /// </summary> /// <param name="msg"></param> internal void SendBase(RSocketMsg msg) { var data = _rUnpacker.Encode(msg); _client.SendAsync(data); }
/// <summary> /// 发送数据 /// </summary> /// <param name="msg"></param> internal void Send(RSocketMsg msg) { var data = ((RCoder)_RContext.Unpacker).Encode(msg); _client.SendAsync(data); }
private void _batcher_OnBatched(IBatcher sender, byte[] data) { _client.SendAsync(data); }
public void Request(byte[] data) { _clientSocket.SendAsync(data); }
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { ChannelManager.Current.Refresh(_clientSocket.Endpoint); return(_clientSocket.SendAsync(buffer, offset, count, cancellationToken)); }
public void Send(byte[] data) { _udpClient.SendAsync(data); }
public Task SendAsync <T>(T message, int retryMax, Action <Record <T> > errorMessageCallback = null) { return(_clientSocket.SendAsync <T>(message, retryMax, errorMessageCallback)); }
public void SendAsync(BaseSocketProtocal protocal) { _udpClient.SendAsync(protocal.ToBytes()); }
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return(_clientSocket.SendAsync(buffer, offset, count, cancellationToken)); }
/// <summary> /// SendAsync /// </summary> /// <param name="data"></param> public void SendAsync(byte[] data) { _clientSokcet.SendAsync(data); }
private void RequestHandShark() { _client.SendAsync(WSUserToken.RequestHandShark(_url, _serverIP, _serverPort, _subProtocol, _origin)); }
public void SendAsync(JT808Package jT808Package) { _clientSokcet.SendAsync(new JT808Serializer().Serialize(jT808Package)); }
private void RequestHandShark() { _client.SendAsync(WSUserToken.RequestHandShark(_serverIP, _serverPort)); }