private Task<bool> DoHandshake(Rfc6455Handshake handshake) { return new Task<bool>(() => { Socket.Send(Encoding.UTF8.GetBytes(handshake.ToString()), () => { StartReceiving(); } , exception => { }); return SpinWait.SpinUntil(() => IsHandshakeDone, 3000); }); }
private Task<bool> DoHandshake(Rfc6455Handshake handshake) { return new Task<bool>(() => { var cts = new CancellationTokenSource(); var token = cts.Token; var working = true; var buffer = new byte[1024]; Socket.Send(Encoding.UTF8.GetBytes(handshake.ToString()), () => Socket.Receive(buffer, r => { Receive(); IsHandshakeDone = true; foreach (var ctrl in this.Controllers.GetAll()) { ctrl.BindUnboundSubscriptions(); } working = false; }, err => FireOnDisconnected()), err => FireOnDisconnected()); while (working) { if (token.IsCancellationRequested) return false; Thread.Sleep(1); } return true; }); }