Modified WebSocket.cs

This commit is contained in:
sta 2013-05-08 13:27:43 +09:00
parent 3432fd1727
commit c513fe7670
10 changed files with 26 additions and 35 deletions

Binary file not shown.

View File

@ -275,14 +275,13 @@ namespace WebSocketSharp {
/// Gets a value indicating whether the WebSocket connection is alive. /// Gets a value indicating whether the WebSocket connection is alive.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the connection is alive; otherwise, <c>false</c>. /// <c>true</c> if the WebSocket connection is alive; otherwise, <c>false</c>.
/// </value> /// </value>
public bool IsAlive { public bool IsAlive {
get { get {
if (_readyState != WsState.OPEN) return _readyState == WsState.OPEN
return false; ? ping(new byte[]{})
: false;
return Ping();
} }
} }
@ -869,21 +868,14 @@ namespace WebSocketSharp {
OnOpen.Emit(this, EventArgs.Empty); OnOpen.Emit(this, EventArgs.Empty);
} }
private bool ping(string message, int millisecondsTimeout) private bool ping(byte[] data)
{ {
var buffer = Encoding.UTF8.GetBytes(message); var frame = createControlFrame(Opcode.PING, new PayloadData(data), _client);
if (buffer.Length > 125) var timeOut = _client ? 5000 : 1000;
{
var msg = "The payload length of a Ping frame must be 125 bytes or less.";
onError(msg);
return false;
}
var frame = createControlFrame(Opcode.PING, new PayloadData(buffer), _client); return send(frame)
if (!send(frame)) ? _receivePong.WaitOne(timeOut)
return false; : false;
return _receivePong.WaitOne(millisecondsTimeout);
} }
private void pong(PayloadData data) private void pong(PayloadData data)
@ -892,12 +884,6 @@ namespace WebSocketSharp {
send(frame); send(frame);
} }
private void pong(string data)
{
var payloadData = new PayloadData(data);
pong(payloadData);
}
private bool processAbnormal(WsFrame frame) private bool processAbnormal(WsFrame frame)
{ {
if (frame != null) if (frame != null)
@ -1485,33 +1471,38 @@ namespace WebSocketSharp {
} }
/// <summary> /// <summary>
/// Pings using the WebSocket connection. /// Sends a Ping using the WebSocket connection.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocket"/> receives a Pong in a time; otherwise, <c>false</c>. /// <c>true</c> if a <see cref="WebSocket"/> instance receives a Pong in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool Ping() public bool Ping()
{ {
return Ping(String.Empty); return ping(new byte[]{});
} }
/// <summary> /// <summary>
/// Pings with the specified <paramref name="message"/> using the WebSocket connection. /// Sends a Ping with the specified <paramref name="message"/> using the WebSocket connection.
/// </summary> /// </summary>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message. /// A <see cref="string"/> that contains a message to send with a Ping.
/// </param> /// </param>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocket"/> receives a Pong in a time; otherwise, <c>false</c>. /// <c>true</c> if a <see cref="WebSocket"/> instance receives a Pong in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool Ping(string message) public bool Ping(string message)
{ {
if (message == null) if (message.IsNullOrEmpty())
message = String.Empty; return ping(new byte[]{});
return _client var data = Encoding.UTF8.GetBytes(message);
? ping(message, 5 * 1000) if (data.Length > 125)
: ping(message, 1 * 1000); {
onError("The payload length of a Ping frame must be 125 bytes or less.");
return false;
}
return ping(data);
} }
/// <summary> /// <summary>