码迷,mamicode.com
首页 > Web开发 > 详细

WebSocketSharp 的使用

时间:2018-11-02 21:36:06      阅读:676      评论:0      收藏:0      [点我收藏+]

标签:cat   ram   void   var   prot   oid   ini   already   main   

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

WebSocketSharp 的使用

标签:cat   ram   void   var   prot   oid   ini   already   main   

原文地址:https://www.cnblogs.com/hippieZhou/p/9898322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!