
31 changed files with 883 additions and 0 deletions
-
12websocket-sharp.userprefs
-
27websocket-sharp/AssemblyInfo.cs
-
51websocket-sharp/Ext.cs
-
41websocket-sharp/IWsStream.cs
-
450websocket-sharp/WebSocket.cs
-
40websocket-sharp/WsState.cs
-
90websocket-sharp/WsStream.cs
-
BINwebsocket-sharp/bin/Debug/websocket-sharp.dll
-
BINwebsocket-sharp/bin/Debug/websocket-sharp.dll.mdb
-
BINwebsocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll
-
BINwebsocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
-
BINwebsocket-sharp/bin/Release/websocket-sharp.dll
-
BINwebsocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll
-
67websocket-sharp/websocket-sharp.csproj
-
BINwebsocket-sharp/websocket-sharp.pidb
-
27wsclient/AssemblyInfo.cs
-
BINwsclient/bin/Debug/websocket-sharp.dll
-
BINwsclient/bin/Debug/websocket-sharp.dll.mdb
-
BINwsclient/bin/Debug/wsclient.exe
-
BINwsclient/bin/Debug/wsclient.exe.mdb
-
BINwsclient/bin/Debug_Ubuntu/websocket-sharp.dll
-
BINwsclient/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
-
BINwsclient/bin/Debug_Ubuntu/wsclient.exe
-
BINwsclient/bin/Debug_Ubuntu/wsclient.exe.mdb
-
BINwsclient/bin/Release/websocket-sharp.dll
-
BINwsclient/bin/Release/wsclient.exe
-
BINwsclient/bin/Release_Ubuntu/websocket-sharp.dll
-
BINwsclient/bin/Release_Ubuntu/wsclient.exe
-
77wsclient/wsclient.cs
-
1wsclient/wsclient.csproj
-
BINwsclient/wsclient.pidb
@ -0,0 +1,12 @@ |
|||
<Properties> |
|||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug_Ubuntu" ctype="Workspace" /> |
|||
<MonoDevelop.Ide.Workbench ActiveDocument="websocket-sharp/WebSocket.cs" ctype="Workbench"> |
|||
<Files> |
|||
<File FileName="websocket-sharp/WebSocket.cs" Line="5" Column="54" /> |
|||
</Files> |
|||
</MonoDevelop.Ide.Workbench> |
|||
<MonoDevelop.Ide.DebuggingService.Breakpoints> |
|||
<BreakpointStore /> |
|||
</MonoDevelop.Ide.DebuggingService.Breakpoints> |
|||
<MonoDevelop.Ide.DebuggingService.PinnedWatches ctype="PinnedWatchStore" /> |
|||
</Properties> |
@ -0,0 +1,27 @@ |
|||
using System.Reflection; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
// Information about this assembly is defined by the following attributes.
|
|||
// Change them to the values specific to your project.
|
|||
|
|||
[assembly: AssemblyTitle("websocket-sharp")] |
|||
[assembly: AssemblyDescription("A C# implementation of a WebSocket protocol client")] |
|||
[assembly: AssemblyConfiguration("")] |
|||
[assembly: AssemblyCompany("")] |
|||
[assembly: AssemblyProduct("websocket-sharp.dll")] |
|||
[assembly: AssemblyCopyright("sta.blockhead")] |
|||
[assembly: AssemblyTrademark("")] |
|||
[assembly: AssemblyCulture("")] |
|||
|
|||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
|||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
|||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
|||
|
|||
[assembly: AssemblyVersion("1.0.0.*")] |
|||
|
|||
// The following attributes are used to specify the signing key for the assembly,
|
|||
// if desired. See the Mono documentation for more information about signing.
|
|||
|
|||
//[assembly: AssemblyDelaySign(false)]
|
|||
//[assembly: AssemblyKeyFile("")]
|
|||
|
@ -0,0 +1,51 @@ |
|||
#region MIT License
|
|||
/** |
|||
* Ext.cs |
|||
* |
|||
* The MIT License |
|||
* |
|||
* Copyright (c) 2010 sta.blockhead |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#endregion
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace WebSocketSharp |
|||
{ |
|||
public static class Ext |
|||
{ |
|||
public static bool EqualsWithSaveTo(this int asbyte, char c, List<byte> dist) |
|||
{ |
|||
byte b = (byte)asbyte; |
|||
dist.Add(b); |
|||
return b == Convert.ToByte(c); |
|||
} |
|||
|
|||
public static void NotEqualsDo(this string a, string b, Action<string> action) |
|||
{ |
|||
if (a != b) |
|||
{ |
|||
action(a); |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
#region MIT License
|
|||
/** |
|||
* IWsStream.cs |
|||
* |
|||
* The MIT License |
|||
* |
|||
* Copyright (c) 2010 sta.blockhead |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#endregion
|
|||
|
|||
using System; |
|||
|
|||
namespace WebSocketSharp |
|||
{ |
|||
public interface IWsStream : IDisposable |
|||
{ |
|||
void Close(); |
|||
int Read(byte[] buffer, int offset, int size); |
|||
int ReadByte(); |
|||
void Write(byte[] buffer, int offset, int count); |
|||
void WriteByte(byte value); |
|||
} |
|||
} |
@ -0,0 +1,450 @@ |
|||
#region MIT License
|
|||
/** |
|||
* WebSocket.cs |
|||
* |
|||
* A C# implementation of a WebSocket protocol client. |
|||
* This code derived from WebSocket.java (http://github.com/adamac/Java-WebSocket-client).
|
|||
* |
|||
* The MIT License |
|||
* |
|||
* Copyright (c) 2009 Adam MacBeth |
|||
* Copyright (c) 2010 sta.blockhead |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#endregion
|
|||
|
|||
#if NOTIFY
|
|||
using Notifications; |
|||
#endif
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Net; |
|||
using System.Net.Security; |
|||
using System.Net.Sockets; |
|||
using System.Text; |
|||
using System.Threading; |
|||
|
|||
namespace WebSocketSharp |
|||
{ |
|||
public delegate void MessageEventHandler(object sender, string eventdata); |
|||
|
|||
public class WebSocket : IDisposable |
|||
{ |
|||
private Uri uri; |
|||
public string Url |
|||
{ |
|||
get |
|||
{ |
|||
return uri.ToString(); |
|||
} |
|||
} |
|||
|
|||
private volatile WsState readyState; |
|||
public WsState ReadyState |
|||
{ |
|||
get |
|||
{ |
|||
return readyState; |
|||
} |
|||
|
|||
private set |
|||
{ |
|||
switch (value) |
|||
{ |
|||
case WsState.OPEN: |
|||
if (OnOpen != null) |
|||
{ |
|||
OnOpen(this, EventArgs.Empty); |
|||
} |
|||
goto default; |
|||
case WsState.CLOSING: |
|||
case WsState.CLOSED: |
|||
close(value); |
|||
break; |
|||
default: |
|||
readyState = value; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
private StringBuilder unTransmittedBuffer; |
|||
public String UnTransmittedBuffer |
|||
{ |
|||
get { return unTransmittedBuffer.ToString(); } |
|||
} |
|||
|
|||
private long bufferedAmount; |
|||
public long BufferedAmount |
|||
{ |
|||
get { return bufferedAmount; } |
|||
} |
|||
|
|||
private string protocol; |
|||
public string Protocol |
|||
{ |
|||
get { return protocol; } |
|||
} |
|||
|
|||
private TcpClient tcpClient; |
|||
private NetworkStream netStream; |
|||
private SslStream sslStream; |
|||
private IWsStream wsStream; |
|||
private Thread msgThread; |
|||
#if NOTIFY
|
|||
private Notification msgNf; |
|||
public Notification MsgNf |
|||
{ |
|||
get { return msgNf; } |
|||
} |
|||
#endif
|
|||
public event EventHandler OnOpen; |
|||
public event MessageEventHandler OnMessage; |
|||
public event MessageEventHandler OnError; |
|||
public event EventHandler OnClose; |
|||
|
|||
public WebSocket(string url) |
|||
: this(url, String.Empty) |
|||
{ |
|||
} |
|||
|
|||
public WebSocket(string url, string protocol) |
|||
{ |
|||
this.uri = new Uri(url); |
|||
string scheme = uri.Scheme; |
|||
|
|||
if (scheme != "ws" && scheme != "wss") |
|||
{ |
|||
throw new ArgumentException("Unsupported scheme: " + scheme); |
|||
} |
|||
|
|||
this.readyState = WsState.CONNECTING; |
|||
this.unTransmittedBuffer = new StringBuilder(); |
|||
this.bufferedAmount = 0; |
|||
this.protocol = protocol; |
|||
} |
|||
|
|||
public void Connect() |
|||
{ |
|||
createConnection(); |
|||
doHandshake(); |
|||
|
|||
this.msgThread = new Thread(new ThreadStart(message)); |
|||
msgThread.IsBackground = true; |
|||
msgThread.Start(); |
|||
} |
|||
|
|||
public void Send(string data) |
|||
{ |
|||
if (readyState == WsState.CONNECTING) |
|||
{ |
|||
throw new InvalidOperationException("Handshake not complete."); |
|||
} |
|||
|
|||
byte[] dataBuffer = Encoding.UTF8.GetBytes(data); |
|||
|
|||
try |
|||
{ |
|||
wsStream.WriteByte(0x00); |
|||
wsStream.Write(dataBuffer, 0, dataBuffer.Length); |
|||
wsStream.WriteByte(0xff); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
unTransmittedBuffer.Append(data); |
|||
bufferedAmount += dataBuffer.Length; |
|||
|
|||
if (OnError != null) |
|||
{ |
|||
OnError(this, e.Message); |
|||
} |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Error @Send: {0}", e.Message); |
|||
#endif
|
|||
} |
|||
} |
|||
|
|||
public void Close() |
|||
{ |
|||
ReadyState = WsState.CLOSING; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
Close(); |
|||
} |
|||
|
|||
private void close(WsState state) |
|||
{ |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @close: Current thread IsBackground: {0}", Thread.CurrentThread.IsBackground); |
|||
#endif
|
|||
if (readyState == WsState.CLOSING || |
|||
readyState == WsState.CLOSED) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
readyState = state; |
|||
|
|||
if (OnClose != null) |
|||
{ |
|||
OnClose(this, EventArgs.Empty); |
|||
} |
|||
|
|||
if (wsStream != null && tcpClient.Connected) |
|||
{ |
|||
try |
|||
{ |
|||
wsStream.WriteByte(0xff); |
|||
wsStream.WriteByte(0x00); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Error @close: {0}", e.Message); |
|||
#endif
|
|||
} |
|||
} |
|||
|
|||
if (!(Thread.CurrentThread.IsBackground) && |
|||
msgThread != null && msgThread.IsAlive) |
|||
{ |
|||
msgThread.Join(); |
|||
} |
|||
|
|||
if (wsStream != null) |
|||
{ |
|||
wsStream.Dispose(); |
|||
wsStream = null; |
|||
} |
|||
|
|||
if (tcpClient != null) |
|||
{ |
|||
tcpClient.Close(); |
|||
tcpClient = null; |
|||
} |
|||
} |
|||
|
|||
private void createConnection() |
|||
{ |
|||
string scheme = uri.Scheme; |
|||
string host = uri.DnsSafeHost; |
|||
int port = uri.Port; |
|||
|
|||
if (port <= 0) |
|||
{ |
|||
if (scheme == "wss") |
|||
{ |
|||
port = 443; |
|||
} |
|||
else |
|||
{ |
|||
port = 80; |
|||
} |
|||
} |
|||
|
|||
this.tcpClient = new TcpClient(host, port); |
|||
this.netStream = tcpClient.GetStream(); |
|||
|
|||
if (scheme == "wss") |
|||
{ |
|||
this.sslStream = new SslStream(netStream); |
|||
sslStream.AuthenticateAsClient(host); |
|||
this.wsStream = new WsStream<SslStream>(sslStream); |
|||
} |
|||
else |
|||
{ |
|||
this.wsStream = new WsStream<NetworkStream>(netStream); |
|||
} |
|||
} |
|||
|
|||
private void doHandshake() |
|||
{ |
|||
string path = uri.PathAndQuery; |
|||
string host = uri.DnsSafeHost; |
|||
string origin = "http://" + host; |
|||
|
|||
int port = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port; |
|||
if (port != 80) |
|||
{ |
|||
host += ":" + port; |
|||
} |
|||
|
|||
string subprotocol = protocol != String.Empty |
|||
? String.Format("Sec-WebSocket-Protocol: {0}\r\n", protocol) |
|||
: protocol; |
|||
|
|||
string request = "GET " + path + " HTTP/1.1\r\n" + |
|||
"Upgrade: WebSocket\r\n" + |
|||
"Connection: Upgrade\r\n" + |
|||
subprotocol + |
|||
"Host: " + host + "\r\n" + |
|||
"Origin: " + origin + "\r\n" + |
|||
"\r\n"; |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request); |
|||
#endif
|
|||
byte[] sendBuffer = Encoding.UTF8.GetBytes(request); |
|||
wsStream.Write(sendBuffer, 0, sendBuffer.Length); |
|||
|
|||
string[] response; |
|||
List<byte> rawdata = new List<byte>(); |
|||
|
|||
while (true) |
|||
{ |
|||
if (wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) && |
|||
wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata) && |
|||
wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) && |
|||
wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata)) |
|||
{ |
|||
break; |
|||
} |
|||
} |
|||
|
|||
response = Encoding.UTF8.GetString(rawdata.ToArray()) |
|||
.Replace("\r\n", "\n").Replace("\n\n", "\n") |
|||
.Split('\n'); |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @doHandshake: Handshake from server:"); |
|||
foreach (string s in response) |
|||
{ |
|||
Console.WriteLine("{0}", s); |
|||
} |
|||
#endif
|
|||
Action<string> action = s => { throw new IOException("Invalid handshake response: " + s); }; |
|||
response[0].NotEqualsDo("HTTP/1.1 101 Web Socket Protocol Handshake", action); |
|||
response[1].NotEqualsDo("Upgrade: WebSocket", action); |
|||
response[2].NotEqualsDo("Connection: Upgrade", action); |
|||
|
|||
for (int i = 3; i < response.Length; i++) |
|||
{ |
|||
if (response[i].Contains("WebSocket-Protocol:")) |
|||
// if (response[i].Contains("Sec-WebSocket-Protocol:"))
|
|||
{ |
|||
int j = response[i].IndexOf(":"); |
|||
protocol = response[i].Substring(j + 1).Trim(); |
|||
} |
|||
} |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @doHandshake: Sub protocol: {0}", protocol); |
|||
#endif
|
|||
|
|||
ReadyState = WsState.OPEN; |
|||
} |
|||
|
|||
private void message() |
|||
{ |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @message: Current thread IsBackground: {0}", Thread.CurrentThread.IsBackground); |
|||
#endif
|
|||
string data; |
|||
#if NOTIFY
|
|||
this.msgNf = new Notification(); |
|||
msgNf.AddHint("append", "allowed"); |
|||
#endif
|
|||
while (readyState == WsState.OPEN) |
|||
{ |
|||
while (readyState == WsState.OPEN && netStream.DataAvailable) |
|||
{ |
|||
data = receive(); |
|||
|
|||
if (OnMessage != null && data != null) |
|||
{ |
|||
OnMessage(this, data); |
|||
} |
|||
} |
|||
} |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @message: Exit message method."); |
|||
#endif
|
|||
} |
|||
|
|||
private string receive() |
|||
{ |
|||
try |
|||
{ |
|||
byte frame_type = (byte)wsStream.ReadByte(); |
|||
byte b; |
|||
|
|||
if ((frame_type & 0x80) == 0x80) |
|||
{ |
|||
// Skip data frame
|
|||
int len = 0; |
|||
int b_v; |
|||
|
|||
do |
|||
{ |
|||
b = (byte)wsStream.ReadByte(); |
|||
b_v = b & 0x7f; |
|||
len = len * 128 + b_v; |
|||
} |
|||
while ((b & 0x80) == 0x80); |
|||
|
|||
for (int i = 0; i < len; i++) |
|||
{ |
|||
wsStream.ReadByte(); |
|||
} |
|||
|
|||
if (frame_type == 0xff && len == 0) |
|||
{ |
|||
ReadyState = WsState.CLOSED; |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Info @receive: Server start closing handshake."); |
|||
#endif
|
|||
} |
|||
} |
|||
else if (frame_type == 0x00) |
|||
{ |
|||
List<byte> raw_data = new List<byte>(); |
|||
|
|||
while (true) |
|||
{ |
|||
b = (byte)wsStream.ReadByte(); |
|||
|
|||
if (b == 0xff) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
raw_data.Add(b); |
|||
} |
|||
|
|||
return Encoding.UTF8.GetString(raw_data.ToArray()); |
|||
} |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
if (OnError != null) |
|||
{ |
|||
OnError(this, e.Message); |
|||
} |
|||
|
|||
ReadyState = WsState.CLOSED; |
|||
#if DEBUG
|
|||
Console.WriteLine("WS: Error @receive: {0}", e.Message); |
|||
#endif
|
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,40 @@ |
|||
#region MIT License
|
|||
/** |
|||
* WsState.cs |
|||
* |
|||
* The MIT License |
|||
* |
|||
* Copyright (c) 2010 sta.blockhead |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#endregion
|
|||
|
|||
using System; |
|||
|
|||
namespace WebSocketSharp |
|||
{ |
|||
public enum WsState |
|||
{ |
|||
CONNECTING, |
|||
OPEN, |
|||
CLOSING, |
|||
CLOSED |
|||
} |
|||
} |
@ -0,0 +1,90 @@ |
|||
#region MIT License
|
|||
/** |
|||
* WsStream.cs |
|||
* |
|||
* The MIT License |
|||
* |
|||
* Copyright (c) 2010 sta.blockhead |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#endregion
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Net.Security; |
|||
using System.Net.Sockets; |
|||
using System.Reflection; |
|||
|
|||
namespace WebSocketSharp |
|||
{ |
|||
public class WsStream<T> : IWsStream |
|||
where T : Stream |
|||
{ |
|||
private T innerStream; |
|||
|
|||
public WsStream(T innerStream) |
|||
{ |
|||
if (innerStream == null) |
|||
{ |
|||
throw new ArgumentNullException("innerStream"); |
|||
} |
|||
|
|||
Type streamType = innerStream.GetType(); |
|||
if (streamType != typeof(NetworkStream) && |
|||
streamType != typeof(SslStream)) |
|||
{ |
|||
throw new ArgumentException("Unsupported Stream type: " + streamType.ToString()); |
|||
} |
|||
|
|||
this.innerStream = innerStream; |
|||
} |
|||
|
|||
public void Close() |
|||
{ |
|||
innerStream.Close(); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
innerStream.Dispose(); |
|||
} |
|||
|
|||
public int Read(byte[] buffer, int offset, int size) |
|||
{ |
|||
return innerStream.Read(buffer, offset, size); |
|||
} |
|||
|
|||
public int ReadByte() |
|||
{ |
|||
return innerStream.ReadByte(); |
|||
} |
|||
|
|||
public void Write(byte[] buffer, int offset, int count) |
|||
{ |
|||
innerStream.Write(buffer, offset, count); |
|||
} |
|||
|
|||
public void WriteByte(byte value) |
|||
{ |
|||
innerStream.WriteByte(value); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,67 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<PropertyGroup> |
|||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|||
<ProductVersion>9.0.21022</ProductVersion> |
|||
<SchemaVersion>2.0</SchemaVersion> |
|||
<ProjectGuid>{B357BAC7-529E-4D81-A0D2-71041B19C8DE}</ProjectGuid> |
|||
<OutputType>Library</OutputType> |
|||
<RootNamespace>WebSocketSharp</RootNamespace> |
|||
<AssemblyName>websocket-sharp</AssemblyName> |
|||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|||
<DebugSymbols>true</DebugSymbols> |
|||
<DebugType>full</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<OutputPath>bin\Debug</OutputPath> |
|||
<DefineConstants>DEBUG</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
<ConsolePause>false</ConsolePause> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|||
<DebugType>none</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<OutputPath>bin\Release</OutputPath> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
<ConsolePause>false</ConsolePause> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Ubuntu|AnyCPU' "> |
|||
<DebugSymbols>true</DebugSymbols> |
|||
<DebugType>full</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<OutputPath>bin\Debug_Ubuntu</OutputPath> |
|||
<DefineConstants>DEBUG,NOTIFY</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
<ConsolePause>false</ConsolePause> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release_Ubuntu|AnyCPU' "> |
|||
<DebugType>none</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<OutputPath>bin\Release_Ubuntu</OutputPath> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
<ConsolePause>false</ConsolePause> |
|||
<DefineConstants>NOTIFY</DefineConstants> |
|||
</PropertyGroup> |
|||
<ItemGroup> |
|||
<Reference Include="System" /> |
|||
<Reference Include="System.Core" /> |
|||
<Reference Include="notify-sharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=2df29c54e245917a"> |
|||
<Package>notify-sharp</Package> |
|||
</Reference> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Compile Include="AssemblyInfo.cs" /> |
|||
<Compile Include="Ext.cs" /> |
|||
<Compile Include="WebSocket.cs" /> |
|||
<Compile Include="WsState.cs" /> |
|||
<Compile Include="IWsStream.cs" /> |
|||
<Compile Include="WsStream.cs" /> |
|||
</ItemGroup> |
|||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|||
</Project> |
@ -0,0 +1,27 @@ |
|||
using System.Reflection; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
// Information about this assembly is defined by the following attributes.
|
|||
// Change them to the values specific to your project.
|
|||
|
|||
[assembly: AssemblyTitle("wsclient")] |
|||
[assembly: AssemblyDescription("")] |
|||
[assembly: AssemblyConfiguration("")] |
|||
[assembly: AssemblyCompany("")] |
|||
[assembly: AssemblyProduct("")] |
|||
[assembly: AssemblyCopyright("")] |
|||
[assembly: AssemblyTrademark("")] |
|||
[assembly: AssemblyCulture("")] |
|||
|
|||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
|||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
|||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
|||
|
|||
[assembly: AssemblyVersion("1.0.*")] |
|||
|
|||
// The following attributes are used to specify the signing key for the assembly,
|
|||
// if desired. See the Mono documentation for more information about signing.
|
|||
|
|||
//[assembly: AssemblyDelaySign(false)]
|
|||
//[assembly: AssemblyKeyFile("")]
|
|||
|
@ -0,0 +1,77 @@ |
|||
using Notifications; |
|||
using System; |
|||
using System.Threading; |
|||
using WebSocketSharp; |
|||
|
|||
namespace Example |
|||
{ |
|||
public class Program |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
//using (WebSocket ws = new WebSocket("ws://localhost:8000/"))
|
|||
using (WebSocket ws = new WebSocket("ws://localhost:8000/", "chat")) |
|||
{ |
|||
/*ws.OnOpen += (o, e) => |
|||
{ |
|||
//Do something.
|
|||
}; |
|||
*/ |
|||
ws.OnMessage += (o, e) => |
|||
{ |
|||
#if LINUX
|
|||
#if NOTIFY
|
|||
ws.MsgNf.Summary = "[WebSocket] Message"; |
|||
ws.MsgNf.Body = e; |
|||
ws.MsgNf.IconName = "notification-message-im"; |
|||
ws.MsgNf.Show(); |
|||
#else
|
|||
Notification nf = new Notification("[WebSocket] Message", |
|||
e, |
|||
"notification-message-im"); |
|||
nf.Show(); |
|||
#endif
|
|||
#else
|
|||
Console.WriteLine(e); |
|||
#endif
|
|||
}; |
|||
|
|||
ws.OnError += (o, e) => |
|||
{ |
|||
#if LINUX
|
|||
Notification nf = new Notification("[WebSocket] Error", |
|||
e, |
|||
"notification-network-disconnected"); |
|||
nf.Show(); |
|||
#else
|
|||
Console.WriteLine("Error: ", e); |
|||
#endif
|
|||
}; |
|||
/*ws.OnClose += (o, e) => |
|||
{ |
|||
//Do something.
|
|||
}; |
|||
*/ |
|||
ws.Connect(); |
|||
|
|||
Thread.Sleep(500); |
|||
Console.WriteLine("\nType \"exit\" to exit.\n"); |
|||
|
|||
string data; |
|||
while (true) |
|||
{ |
|||
Thread.Sleep(500); |
|||
|
|||
Console.Write("> "); |
|||
data = Console.ReadLine(); |
|||
if (data == "exit") |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
ws.Send(data); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1 @@ |
|||
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{52805AEC-EFB1-4F42-BB8E-3ED4E692C568}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>WsClient</RootNamespace>
<AssemblyName>wsclient</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Ubuntu|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug_Ubuntu</OutputPath>
<DefineConstants>DEBUG,LINUX,NOTIFY</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release_Ubuntu|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release_Ubuntu</OutputPath>
<DefineConstants>LINUX,NOTIFY</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="notify-sharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=2df29c54e245917a">
<Package>notify-sharp</Package>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="wsclient.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\websocket-sharp\websocket-sharp.csproj">
<Project>{B357BAC7-529E-4D81-A0D2-71041B19C8DE}</Project>
<Name>websocket-sharp</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue