private void OnProtocolClose(object sender, WebSocketProtocolEventArgs args) { this.LastError = args.Exception; this.ReadyState = WebSocketState.Closed; if (this.OnClose != null) { this.DispatchEvent(delegate(object state) { try { this.OnClose(this, args); } catch { } }); } }
private void OnProtocolData(object sender, WebSocketProtocolEventArgs args) { if (this.OnData != null) { this.DispatchEvent(delegate(object state) { try { this.OnData( this, new WebSocketEventArgs { BinaryData = args.BinaryData, TextData = args.TextData, IsFragment = args.IsFragment, IsFinal = args.IsFinal }); } catch { } }); } }
/// <summary> /// callback from WebSocket after socket is closed /// clean up and error reporting /// </summary> /// <param name="sender">The sender object.</param> /// <param name="e">Event arguments.</param> private void OnSocketClose(object sender, WebSocketProtocolEventArgs e) { // if WebSocket initiated close by itself, SMProtocol is not aware yet // In this case this.opened will be true and SMProtocol is forced to close if (this.webSocket.LastError != null) { if (this.OnError != null) { // General error state, do not attempt to communicate this.OnError(this, new SMProtocolErrorEventArgs(this.webSocket.LastError)); } else { // fallback to notify user anyhow Console.WriteLine("ERROR: WebSocket error {0}", this.webSocket.LastError); } } else { try { if (e.BinaryData != null) { // We received WS close frame from server with extension data. // Attempt to unpack the data CloseFrameExt closeData = this.serializer.DeserializeCloseFrameExt(e.BinaryData); if (this.OnClose != null) { // Send unpacked extension data to event listener this.OnClose(this, new CloseFrameEventArgs(closeData)); } } else { // We received WS close frame without extension data. If may be // response to our frame. Or it may be server initiated by server. if (this.OnClose != null) { this.OnClose(this, null); } } // this object is now closed this.opened = false; } catch (Exception protocolError) { if (this.OnError != null) { this.OnError(this, new SMProtocolErrorEventArgs(protocolError)); } } } }