LightSocket一个基于Dotnetty 高性能Socket库

LightSocket 对Dotnetty的TCP,WebSocket,UDP进行了封装,简化了一系列复杂配置

基于.Net Standard 2.1,引入ArrayPool操作Byte,减少服务器gc压力

源码:https://github.com/LIFICN/LightSocket

TCP Server:

    TcpServer socketServer = new TcpServer(1010, true);  // true open libuv
    await socketServer.BuildAsync(config =>
    {
        config.OnOpen = (channel) =>
        {
            Console.WriteLine($"{channel.Id.AsShortText()} Opened");
        };
        config.OnMessage = async (channel, bytes) =>
        {
            Console.WriteLine($"Received {channel.Id.AsShortText()} Message:{Encoding.UTF8.GetString(bytes.AsSpan())}");
            await channel.SendForTcpAsync(bytes);
        };
        config.OnClose = (channel) =>
        {
            Console.WriteLine($"{channel.Id.AsShortText()} Closed");
        };
        config.OnError = (channel, ex) =>
        {
            channel.CloseAsync();
            Console.WriteLine($"Server Exception:{ex.Message}");
        };
    });

WebSocket Server:

      //call ws://127.0.0.1:1010/
      WebSocketServer socketServer = new WebSocketServer(1010, "/", true);  // true open libuv
      await socketServer.BuildAsync(config =>
      {
          config.OnOpen = (channel, request) =>
          {
              Console.WriteLine($"{channel.Id.AsShortText()} Opened");
          };
          config.OnPing = (channel, bytes) =>
          {
              Console.WriteLine("Ping");
          };
          config.OnPong = (channel, bytes) =>
          {
              Console.WriteLine("Pong");
          };
          config.OnMessage = async (channel, msg) =>
          {
              Console.WriteLine($"Received {channel.Id.AsShortText()} Message:{msg}");
              await channel.SendForWebSocketAsync(msg);
          };
          config.OnBinary = (channel, file) =>
          {
              Console.WriteLine("Binary");
          };
          config.OnClose = (channel) =>
          {
              Console.WriteLine($"{channel.Id.AsShortText()} Closed");
          };
          config.OnError = (channel, ex) =>
          {
              channel.CloseAsync();
              Console.WriteLine($"Server Exception:{ex.Message}");
          };
      });

UDP Server:

        UdpServer socketServer = new UdpServer(1010);
        await socketServer.BuildAsync(config =>
        {
            config.OnStart = (channel) =>
            {
                Console.WriteLine($"Udp Server Started,Port:{socketServer.Port}");
            };
            config.OnMessage = async (channel, bytes, point) =>
            {
                Console.WriteLine($"Received {point.ToString()} Message:{Encoding.UTF8.GetString(bytes.AsSpan())}");
                await channel.SendForUdpAsync(bytes, point);
            };
            config.OnClose = (channel) =>
            {
                Console.WriteLine($"Udp Server Closed");
            };
            config.OnError = (channel, ex) =>
            {
                channel.CloseAsync();
                Console.WriteLine($"Server Exception:{ex.Message}");
            };
        });

  

你可能感兴趣的:(LightSocket一个基于Dotnetty 高性能Socket库)