Skip to content

Commit d777c1c

Browse files
committed
first commit
0 parents  commit d777c1c

20 files changed

+605
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
websocket-controller
2+
====================
3+
4+
Easy WebSockets for AspNetCore 3.0.
5+
6+
7+
Implement your websocket controller:
8+
````c#
9+
public class MyController : AWebsocketController
10+
{
11+
public override async Task OnOpen(Client client)
12+
{
13+
await client.SendAsync("Hello there");
14+
}
15+
// OnTextMessage(Client client, string text)
16+
// OnBinaryMessage(...)
17+
// OnClose(...)
18+
}
19+
````
20+
21+
And add it in your `Startup.cs`
22+
````c#
23+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
24+
{
25+
app.UseWebSockets();
26+
var myHandler = WebSocketHandler.CreateFor<MyController>(app.ApplicationServices);
27+
app.Run(myHandler.HandleRequest);
28+
}
29+
````
30+
31+
See example for a basic websocket chat. To run, `cd example` and `dotnet run`, then open http://localhost:5000/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "src", "src\src.csproj", "{255700B0-36C9-4C82-A73B-35D18F7B8D31}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example", "example\example.csproj", "{35212A5A-CD5A-4FA3-B40B-150BED908A97}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{255700B0-36C9-4C82-A73B-35D18F7B8D31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{255700B0-36C9-4C82-A73B-35D18F7B8D31}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{255700B0-36C9-4C82-A73B-35D18F7B8D31}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{255700B0-36C9-4C82-A73B-35D18F7B8D31}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{35212A5A-CD5A-4FA3-B40B-150BED908A97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{35212A5A-CD5A-4FA3-B40B-150BED908A97}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{35212A5A-CD5A-4FA3-B40B-150BED908A97}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{35212A5A-CD5A-4FA3-B40B-150BED908A97}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin
2+
/obj

example/ChatController.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Linq;
2+
using System.Net.WebSockets;
3+
using System.Threading.Tasks;
4+
using TimoStamm.WebSockets.Controller;
5+
6+
namespace example
7+
{
8+
public class ChatController : AWebsocketController
9+
{
10+
private readonly ClientCollection _clients;
11+
private static int _clientCounter;
12+
13+
public ChatController(ClientCollection clients)
14+
{
15+
_clients = clients;
16+
}
17+
18+
19+
public override async Task OnOpen(Client client)
20+
{
21+
var nick = client.Context.Items["nick"] = ++_clientCounter;
22+
var msg = $"{nick} has entered the chat.";
23+
if (_clients.Count == 1)
24+
{
25+
msg += " You are alone. Open another browser window...";
26+
}
27+
await Task.WhenAll(_clients.Select(c => c.SendAsync(msg)));
28+
}
29+
30+
31+
public override async Task OnClose(Client client, WebSocketCloseStatus closeStatus,
32+
string closeStatusDescription)
33+
{
34+
var nick = client.Context.Items["nick"];
35+
var msg = $"{nick} has left the chat.";
36+
await Task.WhenAll(_clients.Select(c => c.SendAsync(msg)));
37+
}
38+
39+
40+
public override async Task OnTextMessage(Client client, string text)
41+
{
42+
var nick = client.Context.Items["nick"];
43+
var msg = $"{nick}: {text}";
44+
await Task.WhenAll(_clients.Select(c => c.SendAsync(msg)));
45+
}
46+
47+
}
48+
}

example/MyController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using TimoStamm.WebSockets.Controller;
3+
4+
namespace example
5+
{
6+
public class MyController : AWebsocketController
7+
{
8+
9+
public override async Task OnOpen(Client client)
10+
{
11+
await client.SendAsync("Hello there");
12+
}
13+
14+
}
15+
}

example/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace example
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
22+
}
23+
}
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:13053",
7+
"sslPort": 44318
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"example": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

example/Startup.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using TimoStamm.WebSockets.Controller;
5+
6+
namespace example
7+
{
8+
public class Startup
9+
{
10+
// This method gets called by the runtime. Use this method to add services to the container.
11+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
12+
public void ConfigureServices(IServiceCollection services)
13+
{
14+
}
15+
16+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
17+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
18+
{
19+
app.UseDefaultFiles();
20+
app.UseStaticFiles();
21+
22+
app.UseWebSockets();
23+
24+
app.Map("/ws-chat", a =>
25+
{
26+
var handler = WebSocketHandler.CreateFor<ChatController>(app.ApplicationServices);
27+
a.Run(handler.HandleRequest);
28+
});
29+
}
30+
}
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}

example/appsettings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

0 commit comments

Comments
 (0)