-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (114 loc) · 4.3 KB
/
Program.cs
File metadata and controls
149 lines (114 loc) · 4.3 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
using System;
using System.Threading;
using WebSocketSharp;
using WebSocketSharp.Net;
namespace Example
{
public class Program
{
public static void Main (string[] args)
{
// Create a new instance of the WebSocket class.
//
// The WebSocket class inherits the System.IDisposable interface, so you can
// use the using statement. And the WebSocket connection will be closed with
// close status 1001 (going away) when the control leaves the using block.
//
// If you would like to connect to the server with the secure connection,
// you should create a new instance with a wss scheme WebSocket URL.
//using (var ws = new WebSocket ("ws://localhost:4649/Echo"))
//using (var ws = new WebSocket ("wss://localhost:5963/Echo"))
using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
//using (var ws = new WebSocket ("wss://localhost:5963/Chat"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:5963/Chat?name=nobita"))
{
#if DEBUG
// To change the logging level.
ws.Log.Level = LogLevel.Trace;
// To enable the Per-message Compression extension.
//ws.Compression = CompressionMethod.Deflate;
// To emit a WebSocket.OnMessage event when receives a ping.
//ws.EmitOnPing = true;
// To enable the redirection.
//ws.EnableRedirection = true;
// To disable a delay when send or receive buffer of the underlying
// TCP socket is not full.
ws.NoDelay = true;
// To send the Origin header.
//ws.Origin = "http://localhost:4649";
// To send the cookies.
//ws.SetCookie (new Cookie ("name", "nobita"));
//ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\""));
// To send the credentials for the HTTP Authentication (Basic/Digest).
//ws.SetCredentials ("nobita", "password", false);
// To connect through the HTTP Proxy server.
//ws.SetProxy ("http://localhost:3128", "nobita", "password");
// To send a user header.
var reqHeader = "RequestForID";
var resHeader = "ID";
ws.SetUserHeader (reqHeader, resHeader);
// To validate the server certificate.
/*
ws.SslConfiguration.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => {
var fmt = "Certificate:\n- Issuer: {0}\n- Subject: {1}";
var msg = String.Format (
fmt,
certificate.Issuer,
certificate.Subject
);
ws.Log.Debug (msg);
return true; // If the server certificate is valid.
};
*/
// To change the wait time for the response to the Ping or Close.
//ws.WaitTime = TimeSpan.FromSeconds (10);
#endif
// Set the WebSocket events.
ws.OnClose +=
(sender, e) => {
var fmt = "[WebSocket Close ({0})] {1}";
Console.WriteLine (fmt, e.Code, e.Reason);
};
ws.OnError +=
(sender, e) => {
var fmt = "[WebSocket Error] {0}";
Console.WriteLine (fmt, e.Message);
};
ws.OnMessage +=
(sender, e) => {
var fmt = e.IsPing
? "[WebSocket Ping] {0}"
: "[WebSocket Message] {0}";
Console.WriteLine (fmt, e.Data);
};
ws.OnOpen +=
(sender, e) => {
#if DEBUG
var val = ws.HandshakeResponseHeaders[resHeader];
if (!val.IsNullOrEmpty ()) {
var fmt = "[WebSocket Open] {0}: {1}";
Console.WriteLine (fmt, resHeader, val);
}
#endif
ws.Send ("Hi, there!");
};
// Connect to the server.
ws.Connect ();
// Connect to the server asynchronously.
//ws.ConnectAsync ();
Console.WriteLine ("\nType \"exit\" to exit.\n");
while (true) {
Thread.Sleep (1000);
Console.Write ("> ");
var msg = Console.ReadLine ();
if (msg == "exit")
break;
// Send a text message.
ws.Send (msg);
}
}
}
}
}