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 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 { } }