示例#1
0
        /// <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);
        }
示例#2
0
 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 { }
     });
 }
示例#3
0
        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();
            }
        }
示例#4
0
        public void SendAsyncMessage()
        {
            var line = Encoding.UTF8.GetBytes(new string('r', BufferSize) + Environment.NewLine);

            while (Totals > 0)
            {
                _client.SendAsync(line, 0).ConfigureAwait(false);
                Totals--;
            }
        }
示例#5
0
        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);
                }
            }
        }
示例#6
0
        /// <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());
        }
示例#7
0
        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));
 }
示例#9
0
文件: RClient.cs 项目: yswenli/SAEA
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="msg"></param>
        internal void SendBase(RSocketMsg msg)
        {
            var data = _rUnpacker.Encode(msg);

            _client.SendAsync(data);
        }
示例#10
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="msg"></param>
        internal void Send(RSocketMsg msg)
        {
            var data = ((RCoder)_RContext.Unpacker).Encode(msg);

            _client.SendAsync(data);
        }
示例#11
0
 private void _batcher_OnBatched(IBatcher sender, byte[] data)
 {
     _client.SendAsync(data);
 }
示例#12
0
 public void Request(byte[] data)
 {
     _clientSocket.SendAsync(data);
 }
示例#13
0
 public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 {
     ChannelManager.Current.Refresh(_clientSocket.Endpoint);
     return(_clientSocket.SendAsync(buffer, offset, count, cancellationToken));
 }
示例#14
0
 public void Send(byte[] data)
 {
     _udpClient.SendAsync(data);
 }
示例#15
0
 public Task SendAsync <T>(T message, int retryMax, Action <Record <T> > errorMessageCallback = null)
 {
     return(_clientSocket.SendAsync <T>(message, retryMax, errorMessageCallback));
 }
示例#16
0
 public void SendAsync(BaseSocketProtocal protocal)
 {
     _udpClient.SendAsync(protocal.ToBytes());
 }
示例#17
0
 public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 {
     return(_clientSocket.SendAsync(buffer, offset, count, cancellationToken));
 }
示例#18
0
文件: TCPClient.cs 项目: yswenli/SAEA
 /// <summary>
 /// SendAsync
 /// </summary>
 /// <param name="data"></param>
 public void SendAsync(byte[] data)
 {
     _clientSokcet.SendAsync(data);
 }
示例#19
0
 private void RequestHandShark()
 {
     _client.SendAsync(WSUserToken.RequestHandShark(_url, _serverIP, _serverPort, _subProtocol, _origin));
 }
示例#20
0
 public void SendAsync(JT808Package jT808Package)
 {
     _clientSokcet.SendAsync(new JT808Serializer().Serialize(jT808Package));
 }
示例#21
0
 private void RequestHandShark()
 {
     _client.SendAsync(WSUserToken.RequestHandShark(_serverIP, _serverPort));
 }