.net core 下运行 supersocket

2021-03-01 05:27

阅读:611

标签:agg   runas   soft   switch   utf8   ext   listener   pip   pipeline   

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.ProtoBase;

namespace myApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = SuperSocketHostBuilder
            .Create()
            .ConfigurePackageHandler(async (session, package) =>
            {
                var result = 0;
                try
                {
                    switch (package.Key.ToUpper())
                    {
                        case ("ADD"):
                            result = package.Parameters
                                .Select(p => int.Parse(p))
                                .Sum();
                            break;

                        case ("SUB"):
                            result = package.Parameters
                                .Select(p => int.Parse(p))
                                .Aggregate((x, y) => x - y);
                            break;

                        case ("MULT"):
                            result = package.Parameters
                                .Select(p => int.Parse(p))
                                .Aggregate((x, y) => x * y);
                            break;
                    }
                }
                catch (Exception ex)
                {
                }
                await session.SendAsync(Encoding.UTF8.GetBytes(result.ToString() + "\r\n"));
            })
            .ConfigureLogging((hostCtx, loggingBuilder) =>
            {
                loggingBuilder.AddConsole();
            })
            .ConfigureSuperSocket(options =>
            {
                options.Name = "Echo Server";
                options.Listeners = new[] {
                    new ListenOptions
                    {
                        Ip = "Any",
                        Port = 4040
                    }
                };
            }).Build();
            await host.RunAsync();
        }
    }
}

参考资料:https://docs.supersocket.net/v2-0/zh-CN/A-Telnet-Example

.net core 下运行 supersocket

标签:agg   runas   soft   switch   utf8   ext   listener   pip   pipeline   

原文地址:https://www.cnblogs.com/hellowzl/p/14442360.html


评论


亲,登录后才可以留言!