Skip to content

Commit c51aec5

Browse files
authored
* port dotnet/corefx#10792 * add EchoApp sample
1 parent 1315946 commit c51aec5

File tree

11 files changed

+387
-23
lines changed

11 files changed

+387
-23
lines changed

WebSockets.sln

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSoc
3232
EndProject
3333
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.WebSockets.ConformanceTest", "test\Microsoft.AspNetCore.WebSockets.ConformanceTest\Microsoft.AspNetCore.WebSockets.ConformanceTest.xproj", "{74F45408-1959-4FEE-9511-25D40F4913FD}"
3434
EndProject
35-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TestClient", "samples\TestClient\TestClient.xproj", "{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}"
35+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Server.Kestrel", "..\KestrelHttpServer\src\Microsoft.AspNetCore.Server.Kestrel\Microsoft.AspNetCore.Server.Kestrel.xproj", "{F510611A-3BEE-4B88-A613-5F4A74ED82A1}"
36+
EndProject
37+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Server.Kestrel.Https", "..\KestrelHttpServer\src\Microsoft.AspNetCore.Server.Kestrel.Https\Microsoft.AspNetCore.Server.Kestrel.Https.xproj", "{5F64B3C3-0C2E-431A-B820-A81BBFC863DA}"
38+
EndProject
39+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EchoApp", "samples\EchoApp\EchoApp.xproj", "{421954B0-5C6B-4092-8D4D-EACA4CE60AFB}"
3640
EndProject
3741
Global
3842
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -68,10 +72,18 @@ Global
6872
{74F45408-1959-4FEE-9511-25D40F4913FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
6973
{74F45408-1959-4FEE-9511-25D40F4913FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
7074
{74F45408-1959-4FEE-9511-25D40F4913FD}.Release|Any CPU.Build.0 = Release|Any CPU
71-
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
72-
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
73-
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
74-
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9}.Release|Any CPU.Build.0 = Release|Any CPU
75+
{F510611A-3BEE-4B88-A613-5F4A74ED82A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
76+
{F510611A-3BEE-4B88-A613-5F4A74ED82A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
77+
{F510611A-3BEE-4B88-A613-5F4A74ED82A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
78+
{F510611A-3BEE-4B88-A613-5F4A74ED82A1}.Release|Any CPU.Build.0 = Release|Any CPU
79+
{5F64B3C3-0C2E-431A-B820-A81BBFC863DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
80+
{5F64B3C3-0C2E-431A-B820-A81BBFC863DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
81+
{5F64B3C3-0C2E-431A-B820-A81BBFC863DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
82+
{5F64B3C3-0C2E-431A-B820-A81BBFC863DA}.Release|Any CPU.Build.0 = Release|Any CPU
83+
{421954B0-5C6B-4092-8D4D-EACA4CE60AFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
84+
{421954B0-5C6B-4092-8D4D-EACA4CE60AFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
85+
{421954B0-5C6B-4092-8D4D-EACA4CE60AFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
86+
{421954B0-5C6B-4092-8D4D-EACA4CE60AFB}.Release|Any CPU.Build.0 = Release|Any CPU
7587
EndGlobalSection
7688
GlobalSection(SolutionProperties) = preSolution
7789
HideSolutionNode = FALSE
@@ -84,6 +96,6 @@ Global
8496
{CDE16880-0374-46FA-8896-99F1B90B4B6F} = {2C7947A5-9FBD-4267-97C1-2D726D7B3BAF}
8597
{5AFA74F5-9B1D-4FC5-815F-EF471F5AC1EF} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
8698
{74F45408-1959-4FEE-9511-25D40F4913FD} = {C45106D0-76C8-4776-A140-F7DD83CA2958}
87-
{8C8EAC01-DC49-4C5E-B348-E4E46FE675F9} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
99+
{421954B0-5C6B-4092-8D4D-EACA4CE60AFB} = {9E55FC5B-FD9C-4266-AB24-F3AA649D7C8B}
88100
EndGlobalSection
89101
EndGlobal

samples/EchoApp/EchoApp.xproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>421954b0-5c6b-4092-8d4d-eaca4ce60afb</ProjectGuid>
11+
<RootNamespace>EchoApp</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<ItemGroup>
21+
<DnxInvisibleContent Include="bower.json" />
22+
<DnxInvisibleContent Include=".bowerrc" />
23+
</ItemGroup>
24+
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
25+
</Project>

samples/EchoApp/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
7+
8+
namespace EchoApp
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
var host = new WebHostBuilder()
15+
.UseKestrel()
16+
.UseContentRoot(Directory.GetCurrentDirectory())
17+
.UseIISIntegration()
18+
.UseStartup<Startup>()
19+
.Build();
20+
21+
host.Run();
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:3159/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"EchoApp": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"launchUrl": "http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

samples/EchoApp/Startup.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.WebSockets;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Logging;
13+
14+
namespace EchoApp
15+
{
16+
public class Startup
17+
{
18+
// This method gets called by the runtime. Use this method to add services to the container.
19+
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
20+
public void ConfigureServices(IServiceCollection services)
21+
{
22+
}
23+
24+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
25+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
26+
{
27+
loggerFactory.AddConsole();
28+
29+
if (env.IsDevelopment())
30+
{
31+
app.UseDeveloperExceptionPage();
32+
}
33+
34+
app.UseWebSockets();
35+
36+
app.Use(async (context, next) =>
37+
{
38+
if (context.WebSockets.IsWebSocketRequest)
39+
{
40+
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
41+
await Echo(webSocket);
42+
}
43+
else
44+
{
45+
await next();
46+
}
47+
});
48+
49+
app.UseFileServer();
50+
}
51+
52+
private async Task Echo(WebSocket webSocket)
53+
{
54+
var buffer = new byte[1024 * 4];
55+
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
56+
while (!result.CloseStatus.HasValue)
57+
{
58+
// If the client send "ServerClose", then they want a server-originated close to occur
59+
if(result.MessageType == WebSocketMessageType.Text)
60+
{
61+
var str = Encoding.UTF8.GetString(buffer, 0, result.Count);
62+
if(str.Equals("ServerClose"))
63+
{
64+
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing from Server", CancellationToken.None);
65+
return;
66+
}
67+
}
68+
69+
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
70+
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
71+
}
72+
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
73+
}
74+
}
75+
}

samples/EchoApp/project.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"dependencies": {
3+
"Microsoft.NETCore.App": {
4+
"version": "1.0.0",
5+
"type": "platform"
6+
},
7+
"Microsoft.AspNetCore.Diagnostics": "1.1.0-*",
8+
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-*",
9+
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*",
10+
"Microsoft.Extensions.Logging.Console": "1.1.0-*",
11+
"Microsoft.AspNetCore.StaticFiles": "1.1.0-*",
12+
"Microsoft.AspNetCore.WebSockets": "0.2.0-*"
13+
},
14+
15+
"tools": {
16+
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-*"
17+
},
18+
19+
"frameworks": {
20+
"netcoreapp1.0": {
21+
"imports": [
22+
"dotnet5.6",
23+
"portable-net45+win8"
24+
]
25+
}
26+
},
27+
28+
"buildOptions": {
29+
"emitEntryPoint": true,
30+
"preserveCompilationContext": true
31+
},
32+
33+
"runtimeOptions": {
34+
"configProperties": {
35+
"System.GC.Server": true
36+
}
37+
},
38+
39+
"publishOptions": {
40+
"include": [
41+
"wwwroot",
42+
"web.config"
43+
]
44+
},
45+
46+
"scripts": {
47+
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
48+
}
49+
}

samples/EchoApp/web.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
4+
<!--
5+
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
6+
-->
7+
8+
<system.webServer>
9+
<handlers>
10+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
11+
</handlers>
12+
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
13+
</system.webServer>
14+
</configuration>

0 commit comments

Comments
 (0)