Fleck

C# Websocket Implementation

Github星跟踪图

Fleck

Build status NuGet

Fleck is a WebSocket server implementation in C#. Branched from the
Nugget project, Fleck requires no inheritance, container, or
additional references.

Fleck has no dependency on HttpListener or HTTP.sys meaning that it
will work on Windows 7 and Server 2008 hosts. WebSocket Remarks - Microsoft Docs

Example

The following is an example that will echo to a client.


var server = new WebSocketServer("ws://0.0.0.0:8181");
server.Start(socket =>
{
  socket.OnOpen = () => Console.WriteLine("Open!");
  socket.OnClose = () => Console.WriteLine("Close!");
  socket.OnMessage = message => socket.Send(message);
});
        

Supported WebSocket Versions

Fleck supports several WebSocket versions of modern web browsers

  • Hixie-Draft-76/Hybi-00 (Safari 5, Chrome < 14, Firefox 4 (when enabled))
  • Hybi-07 (Firefox 6)
  • Hybi-10 (Chrome 14-16, Firefox 7)
  • Hybi-13 (Chrome 17+, Firefox 11+, Safari 6+, Edge 13+(?))

Secure WebSockets (wss://)

Enabling secure connections requires two things: using the scheme wss instead
of ws, and pointing Fleck to an x509 certificate containing a public and
private key

var server = new WebSocketServer("wss://0.0.0.0:8431");
server.Certificate = new X509Certificate2("MyCert.pfx");
server.Start(socket =>
{
  //...use as normal
});

Having issues making a certificate? See this
guide to creating an x509
by @AdrianBathurst

SubProtocol Negotiation

To enable negotiation of subprotocols, specify the supported protocols on
the WebSocketServer.SupportedSubProtocols property. The negotiated
subprotocol will be available on the socket's ConnectionInfo.NegotiatedSubProtocol.

If no supported subprotocols are found on the client request (the
Sec-WebSocket-Protocol header), the connection will be closed.

var server = new WebSocketServer("ws://0.0.0.0:8181");
server.SupportedSubProtocols = new []{ "superchat", "chat" };
server.Start(socket =>
{
  //socket.ConnectionInfo.NegotiatedSubProtocol is populated
});

Custom Logging

Fleck can log into Log4Net or any other third party logging system. Just override the FleckLog.LogAction property with the desired behavior.

ILog logger = LogManager.GetLogger(typeof(FleckLog));

FleckLog.LogAction = (level, message, ex) => {
  switch(level) {
    case LogLevel.Debug:
      logger.Debug(message, ex);
      break;
    case LogLevel.Error:
      logger.Error(message, ex);
      break;
    case LogLevel.Warn:
      logger.Warn(message, ex);
      break;
    default:
      logger.Info(message, ex);
      break;
  }
};

Disable Nagle's Algorithm

Set NoDelay to true on the WebSocketConnection.ListenerSocket

var server = new WebSocketServer("ws://0.0.0.0:8181");
server.ListenerSocket.NoDelay = true;
server.Start(socket =>
{
  //Child connections will not use Nagle's Algorithm
});

Auto Restart After Listen Error

Set RestartAfterListenError to true on the WebSocketConnection

var server = new WebSocketServer("ws://0.0.0.0:8181");
server.RestartAfterListenError = true;
server.Start(socket =>
{
  //...use as normal
});

主要指标

概览
名称与所有者statianzo/Fleck
主编程语言C#
编程语言C# (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2010-10-22 02:35:09
推送于2024-07-03 08:18:44
最后一次提交2021-04-21 19:00:29
发布数23
最新版本名称1.2.0 (发布于 )
第一版名称0.8.0 (发布于 )
用户参与
星数2.4k
关注者数133
派生数592
提交数237
已启用问题?
问题数250
打开的问题数64
拉请求数44
打开的拉请求数17
关闭的拉请求数37
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?