Location>code7788 >text

Fleck: a lightweight C# open source WebSocket server-side library

Popularity:744 ℃/2024-12-17 23:19:04

Recommended a simple to use , lightweight C# open source WebSocket server-side library to facilitate the rapid implementation of WebSocket development .

01 Project description

Fleck is a lightweight WebSocket server library written in C#. It provides a simple and intuitive API that makes it easy for developers to integrate WebSocket functionality into their applications, such as some common scenarios for real-time communication applications, online games, chat applications and real-time data updates.

Fleck was designed with the goal of ease of use and high performance while maintaining code simplicity.

02 Project strengths and weaknesses

1. Advantages

Easy to use:Fleck's API is designed to be simple and does not require complex configuration or inheritance, allowing developers to get started quickly.

No dependencies:Fleck does not rely on HttpListener or , which means that it can run on systems without these components, such as Windows 7 and Server 2008.

2. Disadvantages

Functional Limitations:Fleck focuses on simplicity and lightweight, and does not include advanced features such as complex authentication mechanisms or detailed statistical information.

03 Method of use

1, a simple WebSocket server example

// Create a new instance of a WebSocket server that listens on port 8181 on all network interfaces.
var server = new WebSocketServer("ws://0.0.0.0:8181");

// Call the Start method of the server instance to start the server.
// The Start method accepts a lambda expression as an argument that defines how to handle a new WebSocket connection.
(socket =>
{
    // Fires the OnOpen event when a WebSocket connection is opened and outputs "Open!" to the console.
     = () => ("Open!").

    // When the WebSocket connection is closed, fire the OnClose event and output "Close!" to the console.
     = () => ("Close!"); // When the WebSocket connection is closed, trigger the OnClose event and output "Close!" to the console.

    // The OnMessage event is triggered when the server receives a message from the client.
    // The event handler takes a message argument containing the message received from the client.
    // The received message is then sent back to the client using the method.
     = message => (message).
}).

2. Secure WebSockets (wss://)

// Create a new instance of a WebSocket server listening on port 8431 on all network interfaces and using the wss protocol, WebSocket Secure.
var server = new WebSocketServer("wss://0.0.0.0:8431");

// Set up an X509 certificate for the server, which is used to establish a TLS/SSL encrypted connection.
// "" is the name of the certificate file, which usually contains the certificate and private key.
 = new X509Certificate2("");

// Call the Start method of the server instance to start the server.
(socket =>
{
    //... . use as normal
});

3. Sub-protocols

// Create a new instance of a WebSocket server listening on port 8181 on all network interfaces and using the ws protocol, which is unencrypted WebSocket.
var server = new WebSocketServer("ws://0.0.0.0:8181");

// Set the list of sub-protocols supported by the server. Subprotocols allow the client and server to negotiate the use of specific communication protocols.
// In this example, the server declares that it supports the sub-protocols "superchat" and "chat".
 = new []{ "superchat", "chat" }

// Call the Start method of the server instance to start the server.
// The Start method accepts a lambda expression as a parameter that defines how to handle a new WebSocket connection.
(socket =>
{
    // is populated
});

4、Customized log

ILog logger = (typeof(FleckLog));

 = (level, message, ex) => {
  switch(level) {
    case :
      (message, ex);
      break;
    case :
      (message, ex);
      break;
    case :
      (message, ex);
      break;
    default:
      (message, ex);
      break;
  }
};

5. Disable the Nagle algorithm

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

6. Automatic monitoring error restart

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

04

WebSocket version support

Hixie-Draft-76/Hybi-00(Safari 5,Chrome < 14,Firefox 4)

Hybi-07(Firefox 6)

Hybi-10(Chrome 14-16,Firefox 7)

Hybi-13(Chrome 17+,Firefox 11+,Safari 6+,Edge 13+(?))

05 Project address

/statianzo/Fleck

More open source projects: /bianchenglequ/NetCodeTop

- End -

Recommended Reading

2 zero-based introductory framing tutorials!

Recommended a Star more than 2K .Net lightweight CMS open source projects

Pidgin: A Lightweight, Fast and Flexible C# Parsing Library

Atata: A Selenium-based Web Framework for C# Automated Testing

mongo-csharp-driver: MongoDB official C# client driver!