-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
65 lines (53 loc) · 2.11 KB
/
Startup.cs
File metadata and controls
65 lines (53 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Net.WebSockets;
using System.Threading;
using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TeamZ.Services;
namespace TeamZ
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<INameService, NameService>();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseWebSockets();
app.Use(async (http, next) =>
{
if (http.WebSockets.IsWebSocketRequest)
{
var webSocket = await http.WebSockets.AcceptWebSocketAsync();
if (webSocket != null && webSocket.State == WebSocketState.Open)
{
WebSocketClient.AddSocket(webSocket);
byte[] buffer = new byte[1024];
WebSocketReceiveResult received = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (received.MessageType != WebSocketMessageType.Close)
{
var str = System.Text.Encoding.UTF8.GetString(buffer);
var trimmed = str.Trim();
Console.WriteLine(trimmed);
WebSocketClient.Broadcast("Hi there");
received = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
buffer = new byte[received.Count];
}
}
}
else
{
await next();
}
});
loggerFactory.AddConsole();
app.UseIISPlatformHandler();
app.UseDeveloperExceptionPage();
app.UseMvcWithDefaultRoute();
app.UseStaticFiles();
}
}
}