-
Notifications
You must be signed in to change notification settings - Fork 982
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
119 changed files
with
9,718 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace WebSockets.Client | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Security; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Threading.Tasks; | ||
using DotNetty.Buffers; | ||
using DotNetty.Codecs.Http; | ||
using DotNetty.Codecs.Http.WebSockets; | ||
using DotNetty.Codecs.Http.WebSockets.Extensions.Compression; | ||
using DotNetty.Handlers.Tls; | ||
using DotNetty.Transport.Bootstrapping; | ||
using DotNetty.Transport.Channels; | ||
using DotNetty.Transport.Channels.Sockets; | ||
using DotNetty.Transport.Libuv; | ||
using Examples.Common; | ||
|
||
class Program | ||
{ | ||
static async Task RunClientAsync() | ||
{ | ||
var builder = new UriBuilder | ||
{ | ||
Scheme = ClientSettings.IsSsl ? "wss" : "ws", | ||
Host = ClientSettings.Host.ToString(), | ||
Port = ClientSettings.Port | ||
}; | ||
|
||
string path = ExampleHelper.Configuration["path"]; | ||
if (!string.IsNullOrEmpty(path)) | ||
{ | ||
builder.Path = path; | ||
} | ||
|
||
Uri uri = builder.Uri; | ||
ExampleHelper.SetConsoleLogger(); | ||
|
||
bool useLibuv = ClientSettings.UseLibuv; | ||
Console.WriteLine("Transport type : " + (useLibuv ? "Libuv" : "Socket")); | ||
|
||
IEventLoopGroup group; | ||
if (useLibuv) | ||
{ | ||
group = new EventLoopGroup(); | ||
} | ||
else | ||
{ | ||
group = new MultithreadEventLoopGroup(); | ||
} | ||
|
||
X509Certificate2 cert = null; | ||
string targetHost = null; | ||
if (ClientSettings.IsSsl) | ||
{ | ||
cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password"); | ||
targetHost = cert.GetNameInfo(X509NameType.DnsName, false); | ||
} | ||
try | ||
{ | ||
var bootstrap = new Bootstrap(); | ||
bootstrap | ||
.Group(group) | ||
.Option(ChannelOption.TcpNodelay, true); | ||
if (useLibuv) | ||
{ | ||
bootstrap.Channel<TcpChannel>(); | ||
} | ||
else | ||
{ | ||
bootstrap.Channel<TcpSocketChannel>(); | ||
} | ||
|
||
// Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. | ||
// If you change it to V00, ping is not supported and remember to change | ||
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. | ||
var handler =new WebSocketClientHandler( | ||
WebSocketClientHandshakerFactory.NewHandshaker( | ||
uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders())); | ||
|
||
bootstrap.Handler(new ActionChannelInitializer<IChannel>(channel => | ||
{ | ||
IChannelPipeline pipeline = channel.Pipeline; | ||
if (cert != null) | ||
{ | ||
pipeline.AddLast("tls", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost))); | ||
} | ||
|
||
pipeline.AddLast( | ||
new HttpClientCodec(), | ||
new HttpObjectAggregator(8192), | ||
WebSocketClientCompressionHandler.Instance, | ||
handler); | ||
})); | ||
|
||
IChannel ch = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port)); | ||
await handler.HandshakeCompletion; | ||
|
||
Console.WriteLine("WebSocket handshake completed.\n"); | ||
Console.WriteLine("\t[bye]:Quit \n\t [ping]:Send ping frame\n\t Enter any text and Enter: Send text frame"); | ||
while (true) | ||
{ | ||
string msg = Console.ReadLine(); | ||
if (msg == null) | ||
{ | ||
break; | ||
} | ||
else if ("bye".Equals(msg.ToLower())) | ||
{ | ||
await ch.WriteAndFlushAsync(new CloseWebSocketFrame()); | ||
break; | ||
} | ||
else if ("ping".Equals(msg.ToLower())) | ||
{ | ||
var frame = new PingWebSocketFrame(Unpooled.WrappedBuffer(new byte[] { 8, 1, 8, 1 })); | ||
await ch.WriteAndFlushAsync(frame); | ||
} | ||
else | ||
{ | ||
WebSocketFrame frame = new TextWebSocketFrame(msg); | ||
await ch.WriteAndFlushAsync(frame); | ||
} | ||
} | ||
|
||
await ch.CloseAsync(); | ||
} | ||
finally | ||
{ | ||
await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)); | ||
} | ||
} | ||
|
||
static void Main() => RunClientAsync().Wait(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace WebSockets.Client | ||
{ | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DotNetty.Codecs.Http; | ||
using DotNetty.Codecs.Http.WebSockets; | ||
using DotNetty.Common.Concurrency; | ||
using DotNetty.Common.Utilities; | ||
using DotNetty.Transport.Channels; | ||
|
||
public class WebSocketClientHandler : SimpleChannelInboundHandler<object> | ||
{ | ||
readonly WebSocketClientHandshaker handshaker; | ||
readonly TaskCompletionSource completionSource; | ||
|
||
public WebSocketClientHandler(WebSocketClientHandshaker handshaker) | ||
{ | ||
this.handshaker = handshaker; | ||
this.completionSource = new TaskCompletionSource(); | ||
} | ||
|
||
public Task HandshakeCompletion => this.completionSource.Task; | ||
|
||
public override void ChannelActive(IChannelHandlerContext ctx) => | ||
this.handshaker.HandshakeAsync(ctx.Channel).LinkOutcome(this.completionSource); | ||
|
||
public override void ChannelInactive(IChannelHandlerContext context) | ||
{ | ||
Console.WriteLine("WebSocket Client disconnected!"); | ||
} | ||
|
||
protected override void ChannelRead0(IChannelHandlerContext ctx, object msg) | ||
{ | ||
IChannel ch = ctx.Channel; | ||
if (!this.handshaker.IsHandshakeComplete) | ||
{ | ||
try | ||
{ | ||
this.handshaker.FinishHandshake(ch, (IFullHttpResponse)msg); | ||
Console.WriteLine("WebSocket Client connected!"); | ||
this.completionSource.TryComplete(); | ||
} | ||
catch (WebSocketHandshakeException e) | ||
{ | ||
Console.WriteLine("WebSocket Client failed to connect"); | ||
this.completionSource.TrySetException(e); | ||
} | ||
|
||
return; | ||
} | ||
|
||
|
||
if (msg is IFullHttpResponse response) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Unexpected FullHttpResponse (getStatus={response.Status}, content={response.Content.ToString(Encoding.UTF8)})"); | ||
} | ||
|
||
if (msg is TextWebSocketFrame textFrame) | ||
{ | ||
Console.WriteLine($"WebSocket Client received message: {textFrame.Text()}"); | ||
} | ||
else if (msg is PongWebSocketFrame) | ||
{ | ||
Console.WriteLine("WebSocket Client received pong"); | ||
} | ||
else if (msg is CloseWebSocketFrame) | ||
{ | ||
Console.WriteLine("WebSocket Client received closing"); | ||
ch.CloseAsync(); | ||
} | ||
} | ||
|
||
public override void ExceptionCaught(IChannelHandlerContext ctx, Exception exception) | ||
{ | ||
Console.WriteLine("Exception: " + exception); | ||
this.completionSource.TrySetException(exception); | ||
ctx.CloseAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netcoreapp1.1;net451</TargetFrameworks> | ||
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">1.6.1</NetStandardImplicitPackageVersion> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(TargetFramework)' == 'net451' "> | ||
<RuntimeIdentifier>win-x64</RuntimeIdentifier> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="..\..\shared\dotnetty.com.pfx" Link="dotnetty.com.pfx"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\DotNetty.Buffers\DotNetty.Buffers.csproj" /> | ||
<ProjectReference Include="..\..\src\DotNetty.Codecs.Http\DotNetty.Codecs.Http.csproj" /> | ||
<ProjectReference Include="..\..\src\DotNetty.Codecs\DotNetty.Codecs.csproj" /> | ||
<ProjectReference Include="..\..\src\DotNetty.Common\DotNetty.Common.csproj" /> | ||
<ProjectReference Include="..\..\src\DotNetty.Handlers\DotNetty.Handlers.csproj" /> | ||
<ProjectReference Include="..\..\src\DotNetty.Transport.Libuv\DotNetty.Transport.Libuv.csproj" /> | ||
<ProjectReference Include="..\..\src\DotNetty.Transport\DotNetty.Transport.csproj" /> | ||
<ProjectReference Include="..\Examples.Common\Examples.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ssl": "false", | ||
"host": "127.0.0.1", | ||
"port": "8080", | ||
"path": "/websocket", | ||
"libuv": "true" | ||
} |
Oops, something went wrong.