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

.net core 下运行 supersocket

时间:2021-02-25 11:54:20      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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<StringPackageInfo, CommandLinePipelineFilter>()
            .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

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