Skip to content

Commit

Permalink
Added WebSockets support and HTML/JS test file
Browse files Browse the repository at this point in the history
  • Loading branch information
luiscantero committed Feb 19, 2018
1 parent 23f2217 commit 1b565fb
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
32 changes: 32 additions & 0 deletions FileWinSvcWebApi/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@
<assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.4.2.0" newVersion="1.4.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Security.Cryptography.Algorithms" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Xml.ReaderWriter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
3 changes: 3 additions & 0 deletions FileWinSvcWebApi/FileWinSvcWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@
<Reference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.WebSockets, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNetCore.WebSockets.2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.WebSockets.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.WebUtilities, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNetCore.WebUtilities.2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.WebUtilities.dll</HintPath>
</Reference>
Expand Down
36 changes: 36 additions & 0 deletions FileWinSvcWebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -40,6 +42,40 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
"http://localhost:5111")); // Explicit domain and port.

app.UseMvc();

MapWebSocketsApp(app);
}

private void MapWebSocketsApp(IApplicationBuilder app)
{
app.Map("/wsecho", managedWebSocketsApp =>
{
managedWebSocketsApp.UseWebSockets(new WebSocketOptions { });
managedWebSocketsApp.Use(async (context, next) =>
{
if (context.WebSockets.IsWebSocketRequest)
{
Console.WriteLine("Echo server at: " + context.Request.PathBase);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket);
return;
}
await next();
});
});
}

private async Task Echo(WebSocket webSocket)
{
byte[] buffer = new byte[1024 * 4];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
}
}
1 change: 1 addition & 0 deletions FileWinSvcWebApi/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<package id="Microsoft.AspNetCore.Server.Kestrel.Https" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.AspNetCore.WebSockets" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.AspNetCore.WebUtilities" version="2.0.1" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Common" version="2.3.1" targetFramework="net461" />
Expand Down
43 changes: 43 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
// Web API test.
(function () {
var url = "http://localhost:60001/api/files/test.txt";
var client = new XMLHttpRequest();
client.open("get", url);

client.onload = function () {
alert(client.responseText);
}

client.send();
})();

// WebSockets test.
(function () {
// Connect.
var socket = new WebSocket("ws://localhost:60001/wsecho");

// Connect handler.
socket.onopen = function () {
alert("Connected!");

// Send message.
socket.send("Hello"); // String, binary.
}

// Receive handler.
socket.onmessage = function (e) {
alert("Reply: " + e.data);
}
})();

</script>
</body>
</html>

0 comments on commit 1b565fb

Please sign in to comment.