-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathProgram.cs
More file actions
172 lines (133 loc) · 5.35 KB
/
Program.cs
File metadata and controls
172 lines (133 loc) · 5.35 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
namespace Example2
{
public class Program
{
public static void Main (string[] args)
{
// Create a new instance of the WebSocketServer class.
//
// If you would like to provide the secure connection, you should
// create a new instance with the "secure" parameter set to true or
// with a wss scheme WebSocket URL.
var wssv = new WebSocketServer (4649);
//var wssv = new WebSocketServer (5963, true);
//var wssv = new WebSocketServer (System.Net.IPAddress.Any, 4649);
//var wssv = new WebSocketServer (System.Net.IPAddress.Any, 5963, true);
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Any, 4649);
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Any, 5963, true);
//var wssv = new WebSocketServer ("ws://0.0.0.0:4649");
//var wssv = new WebSocketServer ("wss://0.0.0.0:5963");
//var wssv = new WebSocketServer ("ws://[::0]:4649");
//var wssv = new WebSocketServer ("wss://[::0]:5963");
//var wssv = new WebSocketServer (System.Net.IPAddress.Loopback, 4649);
//var wssv = new WebSocketServer (System.Net.IPAddress.Loopback, 5963, true);
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Loopback, 4649);
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Loopback, 5963, true);
//var wssv = new WebSocketServer ("ws://localhost:4649");
//var wssv = new WebSocketServer ("wss://localhost:5963");
//var wssv = new WebSocketServer ("ws://127.0.0.1:4649");
//var wssv = new WebSocketServer ("wss://127.0.0.1:5963");
//var wssv = new WebSocketServer ("ws://[::1]:4649");
//var wssv = new WebSocketServer ("wss://[::1]:5963");
#if DEBUG
// To change the logging level.
wssv.Log.Level = LogLevel.Trace;
// To provide the HTTP Authentication (Basic/Digest).
/*
wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
wssv.Realm = "WebSocket Test";
wssv.UserCredentialsFinder =
id => {
var name = id.Name;
// Return user name, password, and roles.
return name == "nobita"
? new NetworkCredential (name, "password", "gunfighter")
: null; // If the user credentials are not found.
};
*/
// To remove the inactive sessions periodically.
//wssv.KeepClean = true;
// To resolve to wait for socket in TIME_WAIT state.
//wssv.ReuseAddress = true;
// To provide the secure connection.
/*
var cert = ConfigurationManager.AppSettings["ServerCertFile"];
var passwd = ConfigurationManager.AppSettings["CertFilePassword"];
wssv.SslConfiguration.ServerCertificate = new X509Certificate2 (
cert,
passwd
);
*/
// To change the wait time for the response to the WebSocket Ping or Close.
//wssv.WaitTime = TimeSpan.FromSeconds (2);
#endif
// Add the WebSocket services.
wssv.AddWebSocketService<Echo> ("/Echo");
// With initializing.
wssv.AddWebSocketService<Chat> (
"/Chat",
s => {
s.Prefix = "Anon#";
#if DEBUG
// To respond to the cookies.
/*
s.CookiesResponder =
(reqCookies, resCookies) => {
foreach (var cookie in reqCookies) {
cookie.Expired = true;
resCookies.Add (cookie);
}
};
*/
// To emit a WebSocket.OnMessage event when receives a ping.
//s.EmitOnPing = true;
// To ignore the Sec-WebSocket-Extensions header.
//s.IgnoreExtensions = true;
// To disable a delay when send or receive buffer of the underlying
// TCP socket is not full.
s.NoDelay = true;
// To validate the Origin header.
/*
s.OriginValidator =
val => {
// Check the value of the Origin header, and return true if valid.
Uri origin;
return !val.IsNullOrEmpty ()
&& Uri.TryCreate (val, UriKind.Absolute, out origin)
&& origin.Host == "localhost";
};
*/
// To send the Sec-WebSocket-Protocol header that has a subprotocol
// name.
//s.Protocol = "chat";
// To respond to the user headers.
s.UserHeadersResponder =
(reqHeaders, userHeaders) => {
var val = reqHeaders["RequestForID"];
if (!val.IsNullOrEmpty ())
userHeaders[val] = s.ID;
};
#endif
}
);
// Start the server.
wssv.Start ();
if (wssv.IsListening) {
var fmt = "Listening on port {0}, and providing WebSocket services:";
Console.WriteLine (fmt, wssv.Port);
foreach (var path in wssv.WebSocketServices.Paths)
Console.WriteLine ("- {0}", path);
}
Console.WriteLine ("\nPress Enter key to stop the server...");
Console.ReadLine ();
// Stop the server.
wssv.Stop ();
}
}
}