diff --git a/WebSocket4Net.MonoDroid/WebSocket4Net.MonoDroid.csproj b/WebSocket4Net.MonoDroid/WebSocket4Net.MonoDroid.csproj index ff220681..642de905 100644 --- a/WebSocket4Net.MonoDroid/WebSocket4Net.MonoDroid.csproj +++ b/WebSocket4Net.MonoDroid/WebSocket4Net.MonoDroid.csproj @@ -110,6 +110,9 @@ Code + + PongReceivedEventArgs.cs + Code diff --git a/WebSocket4Net.MonoTouch/WebSocket4Net.MonoTouch.csproj b/WebSocket4Net.MonoTouch/WebSocket4Net.MonoTouch.csproj index 382c0c46..023e8e23 100644 --- a/WebSocket4Net.MonoTouch/WebSocket4Net.MonoTouch.csproj +++ b/WebSocket4Net.MonoTouch/WebSocket4Net.MonoTouch.csproj @@ -102,6 +102,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net.Silverlight/WebSocket4Net.SL40.csproj b/WebSocket4Net.Silverlight/WebSocket4Net.SL40.csproj index aaf4de4f..0eb55b85 100644 --- a/WebSocket4Net.Silverlight/WebSocket4Net.SL40.csproj +++ b/WebSocket4Net.Silverlight/WebSocket4Net.SL40.csproj @@ -121,6 +121,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net.Silverlight/WebSocket4Net.SL50.csproj b/WebSocket4Net.Silverlight/WebSocket4Net.SL50.csproj index 15a3ff09..7e41e80f 100644 --- a/WebSocket4Net.Silverlight/WebSocket4Net.SL50.csproj +++ b/WebSocket4Net.Silverlight/WebSocket4Net.SL50.csproj @@ -113,6 +113,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net.UWP/WebSocket4Net.UWP.csproj b/WebSocket4Net.UWP/WebSocket4Net.UWP.csproj index 7b9eac8e..38326ff9 100644 --- a/WebSocket4Net.UWP/WebSocket4Net.UWP.csproj +++ b/WebSocket4Net.UWP/WebSocket4Net.UWP.csproj @@ -156,6 +156,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net.WP71/WebSocket4Net.WP71.csproj b/WebSocket4Net.WP71/WebSocket4Net.WP71.csproj index 6f3f279a..9c27f424 100644 --- a/WebSocket4Net.WP71/WebSocket4Net.WP71.csproj +++ b/WebSocket4Net.WP71/WebSocket4Net.WP71.csproj @@ -117,6 +117,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net.WP80/WebSocket4Net.WP80.csproj b/WebSocket4Net.WP80/WebSocket4Net.WP80.csproj index c0d811f1..abdfae0c 100644 --- a/WebSocket4Net.WP80/WebSocket4Net.WP80.csproj +++ b/WebSocket4Net.WP80/WebSocket4Net.WP80.csproj @@ -149,6 +149,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net.iOS/WebSocket4Net.iOS.csproj b/WebSocket4Net.iOS/WebSocket4Net.iOS.csproj index 9440dc8f..4e830d4b 100644 --- a/WebSocket4Net.iOS/WebSocket4Net.iOS.csproj +++ b/WebSocket4Net.iOS/WebSocket4Net.iOS.csproj @@ -76,6 +76,9 @@ MessageReceivedEventArgs.cs + + PongReceivedEventArgs.cs + OpCode.cs diff --git a/WebSocket4Net/Command/Pong.cs b/WebSocket4Net/Command/Pong.cs index 2e36dad8..43468633 100644 --- a/WebSocket4Net/Command/Pong.cs +++ b/WebSocket4Net/Command/Pong.cs @@ -1,21 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Text; -using SuperSocket.ClientEngine; - -namespace WebSocket4Net.Command -{ - public class Pong : WebSocketCommandBase - { - public override void ExecuteCommand(WebSocket session, WebSocketCommandInfo commandInfo) - { - session.LastActiveTime = DateTime.Now; - session.LastPongResponse = commandInfo.Text; - } - - public override string Name - { - get { return OpCode.Pong.ToString(); } - } - } -} +using System; +using System.Collections.Generic; +using System.Text; +using SuperSocket.ClientEngine; + +namespace WebSocket4Net.Command +{ + public class Pong : WebSocketCommandBase + { + public override void ExecuteCommand(WebSocket session, WebSocketCommandInfo commandInfo) + { + session.LastActiveTime = DateTime.Now; + session.LastPongResponse = commandInfo.Text; + + session.FirePongReceived(commandInfo.Data); + } + + public override string Name + { + get { return OpCode.Pong.ToString(); } + } + } +} diff --git a/WebSocket4Net/DataReceivedEventArgs.cs b/WebSocket4Net/DataReceivedEventArgs.cs index 47754222..2248b489 100644 --- a/WebSocket4Net/DataReceivedEventArgs.cs +++ b/WebSocket4Net/DataReceivedEventArgs.cs @@ -1,16 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace WebSocket4Net -{ - public class DataReceivedEventArgs : EventArgs - { - public DataReceivedEventArgs(byte[] data) - { - Data = data; - } - - public byte[] Data { get; private set; } - } -} +using System; +using System.Collections.Generic; +using System.Text; + +namespace WebSocket4Net +{ + public class DataReceivedEventArgs : EventArgs + { + public DataReceivedEventArgs(byte[] data) + { + Data = data; + } + + public byte[] Data { get; private set; } + } +} diff --git a/WebSocket4Net/PongReceivedEventArgs.cs b/WebSocket4Net/PongReceivedEventArgs.cs new file mode 100644 index 00000000..ed0b050a --- /dev/null +++ b/WebSocket4Net/PongReceivedEventArgs.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace WebSocket4Net +{ + public class PongReceivedEventArgs : EventArgs + { + public PongReceivedEventArgs(byte[] data) + { + Data = data; + } + + public byte[] Data { get; private set; } + } +} diff --git a/WebSocket4Net/WebSocket.cs b/WebSocket4Net/WebSocket.cs index 995c27e9..66dfd383 100644 --- a/WebSocket4Net/WebSocket.cs +++ b/WebSocket4Net/WebSocket.cs @@ -392,12 +392,21 @@ private void OnPingTimerCallback(object state) return; } - var protocolProcessor = state as IProtocolProcessor; + SendPing(); + } + + public void SendPing() + { + if (!ProtocolProcessor.SupportPingPong) + { + throw new Exception(string.Format("Protocol ({0}) doesn't support Ping/Pong", ProtocolProcessor.Version)); + } + m_LastPingRequest = DateTime.Now.ToString(); try { - protocolProcessor.SendPing(this, m_LastPingRequest); + ProtocolProcessor.SendPing(this, m_LastPingRequest); } catch (Exception e) { @@ -430,7 +439,6 @@ internal void FireMessageReceived(string message) } private EventHandler m_DataReceived; - public event EventHandler DataReceived { add { m_DataReceived += value; } @@ -445,6 +453,21 @@ internal void FireDataReceived(byte[] data) m_DataReceived(this, new DataReceivedEventArgs(data)); } + private EventHandler m_PongReceived; + public event EventHandler PongReceived + { + add { m_PongReceived += value; } + remove { m_PongReceived -= value; } + } + + internal void FirePongReceived(byte[] data) + { + if (m_PongReceived == null) + return; + + m_PongReceived(this, new PongReceivedEventArgs(data)); + } + private const string m_NotOpenSendingMessage = "You must send data by websocket after websocket is opened!"; private bool EnsureWebSocketOpen() diff --git a/WebSocket4Net/WebSocket4Net.Net20.csproj b/WebSocket4Net/WebSocket4Net.Net20.csproj index cc48b737..f20f1026 100644 --- a/WebSocket4Net/WebSocket4Net.Net20.csproj +++ b/WebSocket4Net/WebSocket4Net.Net20.csproj @@ -61,6 +61,7 @@ + diff --git a/WebSocket4Net/WebSocket4Net.Net35.csproj b/WebSocket4Net/WebSocket4Net.Net35.csproj index 364a8620..cd962aa3 100644 --- a/WebSocket4Net/WebSocket4Net.Net35.csproj +++ b/WebSocket4Net/WebSocket4Net.Net35.csproj @@ -62,6 +62,7 @@ + diff --git a/WebSocket4Net/WebSocket4Net.Net40.csproj b/WebSocket4Net/WebSocket4Net.Net40.csproj index 770e7a1f..b878b9ce 100644 --- a/WebSocket4Net/WebSocket4Net.Net40.csproj +++ b/WebSocket4Net/WebSocket4Net.Net40.csproj @@ -60,6 +60,7 @@ + diff --git a/WebSocket4Net/WebSocket4Net.Net45.csproj b/WebSocket4Net/WebSocket4Net.Net45.csproj index cfa3afdd..1dc44ee0 100644 --- a/WebSocket4Net/WebSocket4Net.Net45.csproj +++ b/WebSocket4Net/WebSocket4Net.Net45.csproj @@ -54,6 +54,7 @@ +