WebSocketSharp 的使用
Server 端示例代码:
class Program
{
static void Main(string[] args)
{
var wssv = new WebSocketServer("ws://localhost:8080");
wssv.AddWebSocketService<Laputa>("/Laputa");
wssv.Start();
Console.ReadKey(true);
wssv.Stop();
}
}
public class Laputa : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
var msg = e.Data == "BALUS"
? "I've been balused already..."
: "I'm not available now.";
Send(msg);
}
}
Client 端示例代码:
class Program
{
static void Main(string[] args)
{
using (var ws = new WebSocket("ws://localhost:8080/Laputa"))
{
ws.OnMessage += (sender, e) =>
Console.WriteLine("Laputa says: " + e.Data);
ws.Connect();
ws.Send("BALUS");
Console.ReadKey(true);
}
}
}
- It supports:
- WebSocket Client and Server
- RFC 6455
- Per-message Compression extension
- Secure Connection
- HTTP Authentication
- Query String, Origin header and Cookies
- Infinite streaming
- .NET 4.5 or later
Install-Package WebSocketSharp -Version 1.0.3-rc11