Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed method to send Ping and Event to be notified when Pong is received #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions WebSocket4Net.MonoDroid/WebSocket4Net.MonoDroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.MonoTouch/WebSocket4Net.MonoTouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.Silverlight/WebSocket4Net.SL40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.Silverlight/WebSocket4Net.SL50.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.UWP/WebSocket4Net.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.WP71/WebSocket4Net.WP71.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.WP80/WebSocket4Net.WP80.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions WebSocket4Net.iOS/WebSocket4Net.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<Compile Include="..\WebSocket4Net\MessageReceivedEventArgs.cs">
<Link>MessageReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\PongReceivedEventArgs.cs">
<Link>PongReceivedEventArgs.cs</Link>
</Compile>
<Compile Include="..\WebSocket4Net\OpCode.cs">
<Link>OpCode.cs</Link>
</Compile>
Expand Down
44 changes: 23 additions & 21 deletions WebSocket4Net/Command/Pong.cs
Original file line number Diff line number Diff line change
@@ -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(); }
}
}
}
32 changes: 16 additions & 16 deletions WebSocket4Net/DataReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
16 changes: 16 additions & 0 deletions WebSocket4Net/PongReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
29 changes: 26 additions & 3 deletions WebSocket4Net/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -430,7 +439,6 @@ internal void FireMessageReceived(string message)
}

private EventHandler<DataReceivedEventArgs> m_DataReceived;

public event EventHandler<DataReceivedEventArgs> DataReceived
{
add { m_DataReceived += value; }
Expand All @@ -445,6 +453,21 @@ internal void FireDataReceived(byte[] data)
m_DataReceived(this, new DataReceivedEventArgs(data));
}

private EventHandler<PongReceivedEventArgs> m_PongReceived;
public event EventHandler<PongReceivedEventArgs> 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()
Expand Down
1 change: 1 addition & 0 deletions WebSocket4Net/WebSocket4Net.Net20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="LINQ.cs" />
<Compile Include="MessageReceivedEventArgs.cs" />
<Compile Include="OpCode.cs" />
<Compile Include="PongReceivedEventArgs.cs" />
<Compile Include="Properties\AssemblyInfo.Net20.cs" />
<Compile Include="Protocol\CloseStatusCodeHybi10.cs" />
<Compile Include="Protocol\CloseStatusCodeRfc6455.cs" />
Expand Down
1 change: 1 addition & 0 deletions WebSocket4Net/WebSocket4Net.Net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="JsonWebSocket.Net.cs" />
<Compile Include="MessageReceivedEventArgs.cs" />
<Compile Include="OpCode.cs" />
<Compile Include="PongReceivedEventArgs.cs" />
<Compile Include="Properties\AssemblyInfo.Net35.cs" />
<Compile Include="Protocol\CloseStatusCodeHybi10.cs" />
<Compile Include="Protocol\CloseStatusCodeRfc6455.cs" />
Expand Down
1 change: 1 addition & 0 deletions WebSocket4Net/WebSocket4Net.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="JsonWebSocket.DataContractJson.cs" />
<Compile Include="MessageReceivedEventArgs.cs" />
<Compile Include="OpCode.cs" />
<Compile Include="PongReceivedEventArgs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Protocol\CloseStatusCodeHybi10.cs" />
<Compile Include="Protocol\CloseStatusCodeRfc6455.cs" />
Expand Down
1 change: 1 addition & 0 deletions WebSocket4Net/WebSocket4Net.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Command\WebSocketCommandBase.cs" />
<Compile Include="Command\Pong.cs" />
<Compile Include="Command\Text.cs" />
<Compile Include="PongReceivedEventArgs.cs" />
<Compile Include="DataReceivedEventArgs.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="GlobalAssemblyInfo.cs" />
Expand Down