diff --git a/Example/Program.cs b/Example/Program.cs index 7d2e19be..4ea070d1 100644 --- a/Example/Program.cs +++ b/Example/Program.cs @@ -35,7 +35,7 @@ namespace Example nf.Notify ( new NotificationMessage { Summary = "WebSocket Message", - Body = e.Data, + Body = e.Type != Opcode.Ping ? e.Data : "Received a Ping.", Icon = "notification-message-im" }); @@ -61,6 +61,9 @@ namespace Example // To change the wait time for the response to the Ping or Close. ws.WaitTime = TimeSpan.FromSeconds (10); + + // To emit a WebSocket.OnMessage event when receives a Ping. + ws.EmitOnPing = true; #endif // To enable the Per-message Compression extension. //ws.Compression = CompressionMethod.Deflate; diff --git a/websocket-sharp/MessageEventArgs.cs b/websocket-sharp/MessageEventArgs.cs index 9e9bb6a1..0ae9d48b 100644 --- a/websocket-sharp/MessageEventArgs.cs +++ b/websocket-sharp/MessageEventArgs.cs @@ -37,11 +37,12 @@ namespace WebSocketSharp /// /// /// A event occurs when the receives - /// a text or binary message. + /// a text or binary message, or a Ping if the property is + /// set to true. /// /// - /// If you would like to get the message data, you should access - /// the or property. + /// If you would like to get the message data, you should access the or + /// property. /// /// public class MessageEventArgs : EventArgs @@ -113,7 +114,7 @@ namespace WebSocketSharp /// Gets the type of the message. /// /// - /// or . + /// , , or . /// public Opcode Type { get { diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 8bf86529..237b06b9 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -76,6 +76,7 @@ namespace WebSocketSharp private WebSocketContext _context; private CookieCollection _cookies; private NetworkCredential _credentials; + private bool _emitOnPing; private bool _enableRedirection; private string _extensions; private AutoResetEvent _exitReceiving; @@ -308,6 +309,24 @@ namespace WebSocketSharp } } + /// + /// Gets or sets a value indicating whether the emits + /// a event when receives a Ping. + /// + /// + /// true if the emits a event + /// when receives a Ping; otherwise, false. The default value is false. + /// + public bool EmitOnPing { + get { + return _emitOnPing; + } + + set { + _emitOnPing = value; + } + } + /// /// Gets or sets a value indicating whether the redirects to /// the new URL located in the handshake response. @@ -1001,6 +1020,9 @@ namespace WebSocketSharp if (send (new WebSocketFrame (Opcode.Pong, frame.PayloadData, _client).ToByteArray ())) _logger.Trace ("Returned a Pong."); + if (_emitOnPing) + enqueueToMessageEventQueue (new MessageEventArgs (frame)); + return true; } @@ -1399,7 +1421,7 @@ namespace WebSocketSharp if (processReceivedFrame (frame) && _readyState != WebSocketState.Closed) { receive (); - if (frame.IsControl || !frame.IsFinal) + if ((frame.IsControl && !(frame.IsPing && _emitOnPing)) || !frame.IsFinal) return; lock (_forEvent) {