private async Task OnSocketConnected() { try { var connectionstring = GetConnectionstring(); var handshake = new Rfc6455Handshake(connectionstring, this.Client.Origin, this.Client); var b = Encoding.UTF8.GetBytes(handshake.ToString()); await this._stream.WriteAsync(b, 0, b.Length); Connected = true; //Start receive thread FrameHandler = CreateFrameHandler(); var data = new List <byte>(1024); var buffer = new byte[1024]; ReadHandshake(data, buffer); } catch (Exception ex) { //Connection failed if (this.OnError != null) { this.OnError.Invoke(this, ex); } await this.Client.FireOnDisconnected(); } }
private Task <bool> DoHandshake(Rfc6455Handshake handshake) { return(new Task <bool>(() => { Socket.Send(Encoding.UTF8.GetBytes(handshake.ToString()), () => { StartReceiving(); } , exception => { }); return SpinWait.SpinUntil(() => IsHandshakeDone, ConnectionTimeout); })); }
private async Task DoHandshake(Rfc6455Handshake handshake) { var buffer = new byte[2048]; try { await Socket.SendAsync(Encoding.UTF8.GetBytes(handshake.ToString())).ConfigureAwait(false); var gotHandshakeResponse = false; do { var count = await Socket.ReceiveAsync(buffer).ConfigureAwait(false); var str = Encoding.UTF8.GetString(buffer, 0, count); if (str.EndsWith("\r\n\r\n")) { gotHandshakeResponse = true; } } while (!gotHandshakeResponse); } catch (Exception) { FireOnClose(); } Receive(); IsHandshakeDone = true; if (!this.IsPrimitive) { await BindUnboundBindings().ConfigureAwait(false); } if (this.OnSocketOpen != null) { this.OnSocketOpen.Invoke(this, null); } }
private void OnSocketConnected(object sender, EventArgs eventArgs) { try { _writer = new DataWriter(_clientSocket.OutputStream); var connectionstring = GetConnectionstring(); var handshake = new Rfc6455Handshake(connectionstring, this.Client.Origin, this.Client); _writer.WriteString(handshake.ToString()); _writer.StoreAsync(); //read handshake _reader = new DataReader(_clientSocket.InputStream); _reader.InputStreamOptions = InputStreamOptions.Partial; var data = _reader.LoadAsync(1024); data.Completed = (info, status) => { switch (status) { case AsyncStatus.Completed: //read complete message uint byteCount = _reader.UnconsumedBufferLength; byte[] bytes = new byte[byteCount]; _reader.ReadBytes(bytes); var r = Encoding.UTF8.GetString(bytes, 0, bytes.Length); //Debug.WriteLine(r); Connected = true; //Start receive thread FrameHandler = CreateFrameHandler(); Task.Factory.StartNew(Read); if (this.OnConnected != null) { this.OnConnected.Invoke(this, null); } return; case AsyncStatus.Error: if (this.OnError != null) { this.OnError.Invoke(this, new Exception("Error when doing handshake")); } this.Disconnect(); break; case AsyncStatus.Canceled: this.Disconnect(); break; } }; } catch { } }
private async Task OnSocketConnected() { try { _writer = new DataWriter(_clientSocket.OutputStream); var connectionstring = GetConnectionstring(); var handshake = new Rfc6455Handshake(connectionstring, this.Client.Origin, this.Client); _writer.WriteString(handshake.ToString()); await _writer.StoreAsync(); //read handshake _reader = new DataReader(_clientSocket.InputStream); _reader.InputStreamOptions = InputStreamOptions.Partial; var data = _reader.LoadAsync(1024); data.Completed = async(info, status) => { switch (status) { case AsyncStatus.Completed: //read complete message uint byteCount = _reader.UnconsumedBufferLength; byte[] bytes = new byte[byteCount]; _reader.ReadBytes(bytes); var r = Encoding.UTF8.GetString(bytes, 0, bytes.Length); //Debug.WriteLine(r); Connected = true; //Start receive thread FrameHandler = CreateFrameHandler(); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed ((Communication)this)._readBuffer = new byte[1024 * 320]; Task.Factory.StartNew(Read); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed if (this.OnConnected != null) { this.OnConnected.Invoke(this, null); } return; case AsyncStatus.Error: if (this.OnError != null) { this.OnError.Invoke(this, new Exception("Error when doing handshake")); } await this.Disconnect(); break; case AsyncStatus.Canceled: await this.Disconnect(); break; } }; } catch { } }