-
Notifications
You must be signed in to change notification settings - Fork 541
/
Program.cs
119 lines (97 loc) · 3.84 KB
/
Program.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using NetCoreServer;
namespace WssChatServer
{
class ChatSession : WssSession
{
public ChatSession(WssServer server) : base(server) {}
public override void OnWsConnected(HttpRequest request)
{
Console.WriteLine($"Chat WebSocket session with Id {Id} connected!");
// Send invite message
string message = "Hello from WebSocket chat! Please send a message or '!' to disconnect the client!";
SendTextAsync(message);
}
public override void OnWsDisconnected()
{
Console.WriteLine($"Chat WebSocket session with Id {Id} disconnected!");
}
public override void OnWsReceived(byte[] buffer, long offset, long size)
{
string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
Console.WriteLine("Incoming: " + message);
// Multicast message to all connected sessions
((WssServer)Server).MulticastText(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket session caught an error with code {error}");
}
}
class ChatServer : WssServer
{
public ChatServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
protected override SslSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// WebSocket server port
int port = 8443;
if (args.Length > 0)
port = int.Parse(args[0]);
// WebSocket server content path
string www = "../../../../../www/wss";
if (args.Length > 1)
www = args[1];
Console.WriteLine($"WebSocket server port: {port}");
Console.WriteLine($"WebSocket server static content path: {www}");
Console.WriteLine($"WebSocket server website: https://localhost:{port}/chat/index.html");
Console.WriteLine();
// Create and prepare a new SSL server context
var context = new SslContext(SslProtocols.Tls13, new X509Certificate2("server.pfx", "qwerty"));
// Create a new WebSocket server
var server = new ChatServer(context, IPAddress.Any, port);
server.AddStaticContent(www, "/chat");
// Start the server
Console.Write("Server starting...");
server.Start();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Restart the server
if (line == "!")
{
Console.Write("Server restarting...");
server.Restart();
Console.WriteLine("Done!");
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server.MulticastText(line);
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}