欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WebSocketSharp 的使用

程序员文章站 2023-10-27 21:01:40
Server 端示例代码: Client 端示例代码: It supports: WebSocket Client and Server RFC 6455 Per message Compression extension Secure Connection HTTP Authentication ......

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