forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
122 lines (100 loc) · 4.94 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
120
121
122
using System.Net.WebSockets;
using System.Net.WebSockets.Server;
using nanoFramework.Networking;
using System;
using System.Collections;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
namespace NFWebSockets.Example
{
public class Program
{
public static void Main()
{
//Nanoframework makes it easy to create a websocket server. And to create a new WebSocket Client. Here's a quick example of how easy.
//use the URL provided in the debug output and connect your own client. Or use your device to connect to your websocket server in the cloud.
NetworkHelpers.SetupAndConnectNetwork(true);
Debug.WriteLine("Waiting for network up and IP address...");
NetworkHelpers.IpAddressAvailable.WaitOne();
Debug.WriteLine("Waiting for valid date&time...");
NetworkHelpers.DateTimeAvailable.WaitOne();
Debug.WriteLine("All set!");
string ip = "127.0.0.1";
//Lets create a new default webserver.
WebSocketServer webSocketServer = new WebSocketServer();
webSocketServer.Start();
//Let's echo all incomming messages from clients to all connected clients including the sender.
webSocketServer.MessageReceived += WebSocketServer_MesageReceived;
Debug.WriteLine($"WebSocket server is up and running, connect on: ws://{ip}:{webSocketServer.Port}{webSocketServer.Prefix}");
//Now let's also attach a local websocket client. Just because we can :-)
ClientWebSocket client = new ClientWebSocket();
///////////////////////////////////////////
// secure connection config
// OK to override the setting here to
client.SslVerification = System.Net.Security.SslVerification.NoVerification;
client.CACertificate = new X509Certificate(_dstRootCAX3);
//connect to the local client and write the messages to the debug output
client.Connect("wss://echo.websocket.org");
//client.Connect("ws://127.0.0.1");
client.MessageReceived += Client_MessageReceived;
int helloCounter = 0;
//While the client is connected will send a friendly hello every second.
while (client.State == System.Net.WebSockets.WebSocketFrame.WebSocketState.Open)
{
client.SendString($"hello {helloCounter++}");
Thread.Sleep(100);
}
Thread.Sleep(Timeout.Infinite);
}
private static void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
{
if (e.Frame.MessageType == System.Net.WebSockets.WebSocketFrame.WebSocketMessageType.Text)
{
Debug.WriteLine($"websocket message: {Encoding.UTF8.GetString(e.Frame.Buffer, 0, e.Frame.Buffer.Length)}");
}
else
{
Debug.WriteLine("websocket binary data");
}
}
private static void WebSocketServer_MesageReceived(object sender, MessageReceivedEventArgs e)
{
var webSocketServer = (WebSocketServer)sender;
var buffer = e.Frame.Buffer;
if (e.Frame.MessageType == System.Net.WebSockets.WebSocketFrame.WebSocketMessageType.Text)
{
webSocketServer.BroadCast(Encoding.UTF8.GetString(buffer, 0, buffer.Length));
}
else
{
webSocketServer.BroadCast(e.Frame.Buffer);
}
}
// Identrust DST Root CA X3
// from https://www.identrust.com/dst-root-ca-x3
private const string _dstRootCAX3 =
@"-----BEGIN CERTIFICATE-----
MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow
PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD
Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O
rz5Iy2Xu/NMhD2XSKtkyj4z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