|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace WebSockets.Client |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.IO; |
| 8 | + using System.Net; |
| 9 | + using System.Net.Security; |
| 10 | + using System.Security.Cryptography.X509Certificates; |
| 11 | + using System.Threading.Tasks; |
| 12 | + using DotNetty.Buffers; |
| 13 | + using DotNetty.Codecs.Http; |
| 14 | + using DotNetty.Codecs.Http.WebSockets; |
| 15 | + using DotNetty.Codecs.Http.WebSockets.Extensions.Compression; |
| 16 | + using DotNetty.Handlers.Tls; |
| 17 | + using DotNetty.Transport.Bootstrapping; |
| 18 | + using DotNetty.Transport.Channels; |
| 19 | + using DotNetty.Transport.Channels.Sockets; |
| 20 | + using DotNetty.Transport.Libuv; |
| 21 | + using Examples.Common; |
| 22 | + |
| 23 | + class Program |
| 24 | + { |
| 25 | + static async Task RunClientAsync() |
| 26 | + { |
| 27 | + var builder = new UriBuilder |
| 28 | + { |
| 29 | + Scheme = ClientSettings.IsSsl ? "wss" : "ws", |
| 30 | + Host = ClientSettings.Host.ToString(), |
| 31 | + Port = ClientSettings.Port |
| 32 | + }; |
| 33 | + |
| 34 | + string path = ExampleHelper.Configuration["path"]; |
| 35 | + if (!string.IsNullOrEmpty(path)) |
| 36 | + { |
| 37 | + builder.Path = path; |
| 38 | + } |
| 39 | + |
| 40 | + Uri uri = builder.Uri; |
| 41 | + ExampleHelper.SetConsoleLogger(); |
| 42 | + |
| 43 | + bool useLibuv = ClientSettings.UseLibuv; |
| 44 | + Console.WriteLine("Transport type : " + (useLibuv ? "Libuv" : "Socket")); |
| 45 | + |
| 46 | + IEventLoopGroup group; |
| 47 | + if (useLibuv) |
| 48 | + { |
| 49 | + group = new EventLoopGroup(); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + group = new MultithreadEventLoopGroup(); |
| 54 | + } |
| 55 | + |
| 56 | + X509Certificate2 cert = null; |
| 57 | + string targetHost = null; |
| 58 | + if (ClientSettings.IsSsl) |
| 59 | + { |
| 60 | + cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password"); |
| 61 | + targetHost = cert.GetNameInfo(X509NameType.DnsName, false); |
| 62 | + } |
| 63 | + try |
| 64 | + { |
| 65 | + var bootstrap = new Bootstrap(); |
| 66 | + bootstrap |
| 67 | + .Group(group) |
| 68 | + .Option(ChannelOption.TcpNodelay, true); |
| 69 | + if (useLibuv) |
| 70 | + { |
| 71 | + bootstrap.Channel<TcpChannel>(); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + bootstrap.Channel<TcpSocketChannel>(); |
| 76 | + } |
| 77 | + |
| 78 | + // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. |
| 79 | + // If you change it to V00, ping is not supported and remember to change |
| 80 | + // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. |
| 81 | + var handler =new WebSocketClientHandler( |
| 82 | + WebSocketClientHandshakerFactory.NewHandshaker( |
| 83 | + uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders())); |
| 84 | + |
| 85 | + bootstrap.Handler(new ActionChannelInitializer<IChannel>(channel => |
| 86 | + { |
| 87 | + IChannelPipeline pipeline = channel.Pipeline; |
| 88 | + if (cert != null) |
| 89 | + { |
| 90 | + pipeline.AddLast("tls", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost))); |
| 91 | + } |
| 92 | + |
| 93 | + pipeline.AddLast( |
| 94 | + new HttpClientCodec(), |
| 95 | + new HttpObjectAggregator(8192), |
| 96 | + WebSocketClientCompressionHandler.Instance, |
| 97 | + handler); |
| 98 | + })); |
| 99 | + |
| 100 | + IChannel ch = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port)); |
| 101 | + await handler.HandshakeCompletion; |
| 102 | + |
| 103 | + Console.WriteLine("WebSocket handshake completed.\n"); |
| 104 | + Console.WriteLine("\t[bye]:Quit \n\t [ping]:Send ping frame\n\t Enter any text and Enter: Send text frame"); |
| 105 | + while (true) |
| 106 | + { |
| 107 | + string msg = Console.ReadLine(); |
| 108 | + if (msg == null) |
| 109 | + { |
| 110 | + break; |
| 111 | + } |
| 112 | + else if ("bye".Equals(msg.ToLower())) |
| 113 | + { |
| 114 | + await ch.WriteAndFlushAsync(new CloseWebSocketFrame()); |
| 115 | + break; |
| 116 | + } |
| 117 | + else if ("ping".Equals(msg.ToLower())) |
| 118 | + { |
| 119 | + var frame = new PingWebSocketFrame(Unpooled.WrappedBuffer(new byte[] { 8, 1, 8, 1 })); |
| 120 | + await ch.WriteAndFlushAsync(frame); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + WebSocketFrame frame = new TextWebSocketFrame(msg); |
| 125 | + await ch.WriteAndFlushAsync(frame); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + await ch.CloseAsync(); |
| 130 | + } |
| 131 | + finally |
| 132 | + { |
| 133 | + await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + static void Main() => RunClientAsync().Wait(); |
| 138 | + } |
| 139 | +} |
0 commit comments