/// <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); }
protected override void OnDestroy() { base.OnDestroy(); if (roomServerConnection != null) { roomServerConnection.Disconnect(); } }
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); } } }
protected virtual void OnApplicationQuit() { if (masterConnection != null) { masterConnection.Disconnect(); } }
protected void OnReceived(byte[] data) { if (!_isHandSharked) { _cache.AddRange(data); try { var handShakeText = Encoding.UTF8.GetString(_cache.ToArray()); string key = string.Empty; Regex reg = new Regex(@"Sec\-WebSocket\-Accept:(.*?)\r\n"); Match m = reg.Match(handShakeText); if (string.IsNullOrEmpty(m.Value)) { throw new Exception("回复中不存在 Sec-WebSocket-Accept"); } key = Regex.Replace(m.Value, @"Sec\-WebSocket\-Accept:(.*?)\r\n", "$1").Trim(); _isHandSharked = true; } catch (Exception ex) { OnError.Invoke(_wsContext.UserToken.ID, ex); } } else { var coder = (WSCoder)_wsContext.Unpacker; coder.Unpack(data, (d) => { var wsProtocal = (WSProtocal)d; switch (wsProtocal.Type) { case (byte)WSProtocalType.Close: _client.Disconnect(); break; case (byte)WSProtocalType.Pong: var date = DateTime.Parse(Encoding.UTF8.GetString(wsProtocal.Content)); OnPong?.Invoke(date); break; case (byte)WSProtocalType.Binary: case (byte)WSProtocalType.Text: case (byte)WSProtocalType.Cont: OnMessage?.Invoke((WSProtocal)d); break; case (byte)WSProtocalType.Ping: ReplyPong(); break; default: var error = string.Format("收到未定义的Opcode={0}", d.Type); break; } }, null, null); } }
private static void ErrorMessageCallback <T>(Record <T> record) { Console.WriteLine($"{record.Message} Sent to {record.EndPoint} with Error. {record.Error}"); #if DEBUG if (record.Error.Length > 0) { _client.Disconnect(); Environment.Exit(0); } #endif }
public void Disconnect() { _udpClient.Disconnect(); }
/// <summary> /// 释放资源 /// </summary> public void Dispose() { _isDisposed = true; _client.Disconnect(); _client.Dispose(); }
public void ClientDisconnect() { _clientSocket.Disconnect(); }
public void GlobalCleanup() { _client.Disconnect(); _tcpService.Stop(); }
/// <summary> /// Disconnect /// </summary> public void Disconnect() { _clientSokcet.Disconnect(); }