コード例 #1
0
        static async Task Main(string[] args)
        {
            var host          = new ServiceHost();
            var configuration = new ConfigurationBuilder().AddJsonFile("Config.json").Build();

            host.ConfigureWeb(configuration, builder => builder.UseStartup <Startup>(), "web");
            host.ConfigureSocket(configuration, "tcp");
            host.ConfigureSocket(configuration, "udp");
            host.ConfigureLogging(builder => builder.UserNLog());

            host.OnHostStarted += p =>
            {
                var web = p.GetWeb();
                web.OnWebSocketReceive += (c, a) => Console.WriteLine($"{c.ID} {Encoding.UTF8.GetString(a.Data)}");
                web.OnWebSocketClose   += (c, a) => Console.WriteLine($"{c.ID} 断开连接");

                var tcp = p.GetTcp();
                tcp.OnConnect += (c, a) => Console.WriteLine($"{c.ID} connected");
                tcp.OnReceive += (c, a) =>
                {
                    Console.WriteLine($"{c.ID} received {Encoding.UTF8.GetString(a.Data)}");
                    var body = @"<h1>Hello World</h1><span>XXXXXXX</span>";
                    _ = c.SendAsync(Encoding.UTF8.GetBytes(
                                        "HTTP/1.1 200 OK\r\n"
                                        + "Date: Sat, 31 Dec 2005 23:59:59 GMT\r\n"
                                        + "Content-Type: text/html;charset=UTF8\r\n"
                                        + $"Content-Length: {Encoding.UTF8.GetByteCount(body)}\r\n\n"
                                        + $"{body}"
                                        ));
                };
                tcp.OnSend  += (c, a) => Console.WriteLine($"{c.ID} sent {Encoding.UTF8.GetString(a.Data)}");
                tcp.OnClose += (c, a) => Console.WriteLine($"{c.ID} closed");


                var udp = p.GetUdp();
                udp.OnReceive += (c, a) =>
                {
                    Console.WriteLine($"{c} received {Encoding.UTF8.GetString(a.Data)}");
                    udp.SendTo(Encoding.UTF8.GetBytes("World"), c);
                };
                udp.OnSend += (c, a) => Console.WriteLine($"{c} sent {Encoding.UTF8.GetString(a.Data)}");
            };
            await host.RunAsync();
        }
コード例 #2
0
 /// <summary>
 /// 配置Socket
 /// </summary>
 /// <param name="host"></param>
 /// <param name="stream"></param>
 /// <param name="path"></param>
 /// <returns></returns>
 public static ServiceHost ConfigureSocketWithXml <T>(this ServiceHost host, Stream stream, string path = null)
 => host.ConfigureSocket(new ConfigurationBuilder().AddXmlStream(stream).Build(), path);
コード例 #3
0
 /// <summary>
 /// 配置Socket
 /// </summary>
 /// <param name="host"></param>
 /// <param name="file"></param>
 /// <param name="path"></param>
 /// <returns></returns>
 public static ServiceHost ConfigureSocketWithXml(this ServiceHost host, string file, string path = null)
 => host.ConfigureSocket(new ConfigurationBuilder().AddXmlFile(file).Build(), path);