Skip to content

Commit 6d0a3f0

Browse files
authored
Websocket samples added (#198)
1 parent 9447197 commit 6d0a3f0

18 files changed

+584
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ If you are looking for Iot.Device samples, they are all in the [Iot.Device repos
5454
* [SSL sample pack](samples/SSL)
5555
* [WiFI samples](samples/Wifi)
5656
* [WiFi Soft AP sample](samples/WiFiAP)
57+
* [WebSocket samples] (samples/WebSockets)
5758

5859
## MQTT
5960

samples/WebSockets/README.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
# HTTP sample pack
1+
# WebSocket sample pack
22

33
Shows how to use the WebSockets related APIs in [System.Net.WebSockets](http://docs.nanoframework.net/api/System.Net.WebSockets.html).
44

55
## Samples
66

7-
### WebSockets sample listener
87

9-
[Sample1](./Websockets.Sample1) shows how to configure and start a WebSocket Server and Client.
8+
### WebSockets Server Sample
9+
10+
[Server.RgbSample](./WebSockets.Server.RgbSample) shows howto use Websocket Server with a Webserver hosting a WebApp that controlls a rgb led.
11+
12+
### WebSockets Client Sample
13+
14+
[ServerClient.Sample](./Websockets.ServerClient.Sample) shows how to use the Websocket Client.
15+
16+
### WebSockets Server and Client sample
17+
18+
[ServerClient.Sample](./Websockets.ServerClient.Sample) shows how to configure and start a WebSocket Server and (ssl) Client.
1019

1120
## Hardware requirements
1221

1322
An hardware device with networking capabilities running a nanoFramework image.
14-
This sample was tested with an STM32F769IDiscovery target board, but can be easily changed to any other target that has networking capabilities.
23+
The Websocket Server Sample requires a M5Stack Atom Light board, but can be easily changed to another board connected to a rgb led.
1524

1625
## Related topics
1726

1827
### Reference
1928

29+
- [nanoFramework.Net.WebSockets](https://github.com/nanoframework/System.Net.WebSockets/blob/develop/README.md)
2030
- [System.Net.WebSockets](http://docs.nanoframework.net/api/System.Net.WebSockets.html)
2131

22-
## Build the sample
32+
## Building the samples
2333

24-
1. Start Microsoft Visual Studio 2019 (VS 2017 should be OK too) and select `File > Open > Project/Solution`.
34+
1. Start Microsoft Visual Studio 2022 (VS 2019 and VS 2017 should be OK too) and select `File > Open > Project/Solution`.
2535
1. Starting in the folder where you unzipped the samples/cloned the repository, go to the subfolder for this specific sample. Double-click the Visual Studio Solution (.sln) file.
2636
1. Press `Ctrl+Shift+B`, or select `Build > Build Solution`.
2737

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>853b34d5-3988-412c-bad5-d2bdec214187</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>NFWebSockets.Client.Example</RootNamespace>
16+
<AssemblyName>NFWebSockets.Client.Example</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>packages\nanoFramework.CoreLibrary.1.12.0-preview.5\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Runtime.Events">
29+
<HintPath>packages\nanoFramework.Runtime.Events.1.10.0-preview.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.System.Collections">
32+
<HintPath>packages\nanoFramework.System.Collections.1.4.0-preview.14\lib\nanoFramework.System.Collections.dll</HintPath>
33+
</Reference>
34+
<Reference Include="nanoFramework.System.Text">
35+
<HintPath>packages\nanoFramework.System.Text.1.1.3-preview.13\lib\nanoFramework.System.Text.dll</HintPath>
36+
</Reference>
37+
<Reference Include="System.Device.WiFi">
38+
<HintPath>packages\nanoFramework.System.Device.WiFi.1.4.0-preview.34\lib\System.Device.WiFi.dll</HintPath>
39+
</Reference>
40+
<Reference Include="System.Net">
41+
<HintPath>packages\nanoFramework.System.Net.1.8.0-preview.26\lib\System.Net.dll</HintPath>
42+
</Reference>
43+
<Reference Include="System.Net.WebSockets">
44+
<HintPath>packages\nanoFramework.System.Net.WebSockets.Client.1.0.0-preview.75\lib\System.Net.WebSockets.dll</HintPath>
45+
</Reference>
46+
<Reference Include="System.Threading">
47+
<HintPath>packages\nanoFramework.System.Threading.1.0.4-preview.14\lib\System.Threading.dll</HintPath>
48+
</Reference>
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="packages.config" />
52+
</ItemGroup>
53+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
54+
<ProjectExtensions>
55+
<ProjectCapabilities>
56+
<ProjectConfigurationsDeclaredAsItems />
57+
</ProjectCapabilities>
58+
</ProjectExtensions>
59+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32014.148
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "NFWebSockets.Client.Example", "NFWebSockets.Client.Example.nfproj", "{853B34D5-3988-412C-BAD5-D2BDEC214187}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{853B34D5-3988-412C-BAD5-D2BDEC214187}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{853B34D5-3988-412C-BAD5-D2BDEC214187}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{853B34D5-3988-412C-BAD5-D2BDEC214187}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{853B34D5-3988-412C-BAD5-D2BDEC214187}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{853B34D5-3988-412C-BAD5-D2BDEC214187}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{853B34D5-3988-412C-BAD5-D2BDEC214187}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {BE824ADF-44E1-47A6-B38E-A73F8EAF06CB}
26+
EndGlobalSection
27+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Net.WebSockets;
3+
using System.Net.WebSockets.WebSocketFrame;
4+
using System.Text;
5+
using System.Threading;
6+
using nanoFramework.Networking;
7+
8+
namespace NFWebSockets.Client.Example
9+
{
10+
public class Program
11+
{
12+
public static void Main()
13+
{
14+
//connect to wifi
15+
const string Ssid = "REPLACE-WITH-YOUR-SSID";
16+
const string Password = "REPLACE-WITH-YOUR-WIFI-KEY";
17+
18+
// Give 60 seconds to the wifi join to happen
19+
CancellationTokenSource cs = new(60000);
20+
var success = WiFiNetworkHelper.ScanAndConnectDhcp(Ssid, Password, token: cs.Token);
21+
if (!success)
22+
{
23+
//Red Light indicates no Wifi connection
24+
throw new Exception("Couldn't connect to the Wifi network");
25+
}
26+
27+
//setup WebSocketClient
28+
ClientWebSocket websocketClient = new ClientWebSocket(new ClientWebSocketOptions()
29+
{
30+
//Change the heart beat to a 30 second interval
31+
KeepAliveInterval = TimeSpan.FromSeconds(30)
32+
});
33+
34+
//Handler for receiving websocket messages.
35+
websocketClient.MessageReceived += WebsocketClient_MessageReceived;
36+
//Setup custom header
37+
var headers = new ClientWebSocketHeaders();
38+
headers["userId"] = "nano";
39+
40+
//Connect the client to the websocket server with custom headers
41+
websocketClient.Connect("ws://REPLACE-WITH-YOUR-WEBSOCKET-SERVER-ADDRESS", headers);
42+
43+
//Send a message very 5 seconds
44+
while (websocketClient.State == WebSocketState.Open)
45+
{
46+
websocketClient.SendString("Hello nanoFramework Websocket!");
47+
Thread.Sleep(5000);
48+
}
49+
}
50+
51+
private static void WebsocketClient_MessageReceived(object sender, MessageReceivedEventArgs e)
52+
{
53+
var client = (ClientWebSocket)sender;
54+
55+
//If message is of type Text, echo message back to client
56+
if (e.Frame.MessageType == WebSocketMessageType.Text)
57+
{
58+
//check if message is not fragmented
59+
if (!e.Frame.IsFragmented)
60+
{
61+
string message = Encoding.UTF8.GetString(e.Frame.Buffer, 0, e.Frame.MessageLength);
62+
client.SendString(message);
63+
}
64+
//close connection because fragmented messages are not allowed
65+
else
66+
{
67+
client.Close(WebSocketCloseStatus.PolicyViolation, "Fragmented messages are not allowed"!);
68+
}
69+
}
70+
}
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.5" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.Runtime.Events" version="1.10.0-preview.6" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.System.Collections" version="1.4.0-preview.14" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.System.Device.WiFi" version="1.4.0-preview.34" targetFramework="netnanoframework10" />
7+
<package id="nanoFramework.System.Net" version="1.8.0-preview.26" targetFramework="netnanoframework10" />
8+
<package id="nanoFramework.System.Net.WebSockets.Client" version="1.0.0-preview.75" targetFramework="netnanoframework10" />
9+
<package id="nanoFramework.System.Text" version="1.1.3-preview.13" targetFramework="netnanoframework10" />
10+
<package id="nanoFramework.System.Threading" version="1.0.4-preview.14" targetFramework="netnanoframework10" />
11+
</packages>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>5f445929-f0d2-4280-ac92-47ee05a8efca</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>NFWebSockets.Server.RgbExample</RootNamespace>
16+
<AssemblyName>NFWebSockets.Server.RgbExample</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="Iot.Device.Button">
26+
<HintPath>packages\nanoFramework.Iot.Device.Button.1.0.288-preview.48\lib\Iot.Device.Button.dll</HintPath>
27+
</Reference>
28+
<Reference Include="mscorlib">
29+
<HintPath>packages\nanoFramework.CoreLibrary.1.12.0-preview.5\lib\mscorlib.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.AtomLite">
32+
<HintPath>packages\nanoFramework.AtomLite.1.0.1-preview.43\lib\nanoFramework.AtomLite.dll</HintPath>
33+
</Reference>
34+
<Reference Include="nanoFramework.Hardware.Esp32">
35+
<HintPath>packages\nanoFramework.Hardware.Esp32.1.3.5-preview.6\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
36+
</Reference>
37+
<Reference Include="nanoFramework.Hardware.Esp32.Rmt">
38+
<HintPath>packages\nanoFramework.Hardware.Esp32.Rmt.1.2.2-preview.6\lib\nanoFramework.Hardware.Esp32.Rmt.dll</HintPath>
39+
</Reference>
40+
<Reference Include="nanoFramework.Runtime.Events">
41+
<HintPath>packages\nanoFramework.Runtime.Events.1.10.0-preview.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
42+
</Reference>
43+
<Reference Include="nanoFramework.System.Collections">
44+
<HintPath>packages\nanoFramework.System.Collections.1.4.0-preview.14\lib\nanoFramework.System.Collections.dll</HintPath>
45+
</Reference>
46+
<Reference Include="nanoFramework.System.Text">
47+
<HintPath>packages\nanoFramework.System.Text.1.1.3-preview.13\lib\nanoFramework.System.Text.dll</HintPath>
48+
</Reference>
49+
<Reference Include="nanoFramework.WebServer">
50+
<HintPath>packages\nanoFramework.WebServer.1.0.0-preview.218\lib\nanoFramework.WebServer.dll</HintPath>
51+
</Reference>
52+
<Reference Include="System.Device.Adc">
53+
<HintPath>packages\nanoFramework.System.Device.Adc.1.0.2-preview.6\lib\System.Device.Adc.dll</HintPath>
54+
</Reference>
55+
<Reference Include="System.Device.Dac">
56+
<HintPath>packages\nanoFramework.System.Device.Dac.1.4.3-preview.6\lib\System.Device.Dac.dll</HintPath>
57+
</Reference>
58+
<Reference Include="System.Device.Gpio">
59+
<HintPath>packages\nanoFramework.System.Device.Gpio.1.0.3-preview.8\lib\System.Device.Gpio.dll</HintPath>
60+
</Reference>
61+
<Reference Include="System.Device.I2c">
62+
<HintPath>packages\nanoFramework.System.Device.I2c.1.0.3-preview.6\lib\System.Device.I2c.dll</HintPath>
63+
</Reference>
64+
<Reference Include="System.Device.Spi">
65+
<HintPath>packages\nanoFramework.System.Device.Spi.1.0.3-preview.19\lib\System.Device.Spi.dll</HintPath>
66+
</Reference>
67+
<Reference Include="System.Device.WiFi">
68+
<HintPath>packages\nanoFramework.System.Device.WiFi.1.4.0-preview.34\lib\System.Device.WiFi.dll</HintPath>
69+
</Reference>
70+
<Reference Include="System.Drawing">
71+
<HintPath>packages\nanoFramework.System.Drawing.1.0.288-preview.45\lib\System.Drawing.dll</HintPath>
72+
</Reference>
73+
<Reference Include="System.Net">
74+
<HintPath>packages\nanoFramework.System.Net.1.8.0-preview.26\lib\System.Net.dll</HintPath>
75+
</Reference>
76+
<Reference Include="System.Net.Http">
77+
<HintPath>packages\nanoFramework.System.Net.Http.Server.1.4.0-preview.22\lib\System.Net.Http.dll</HintPath>
78+
</Reference>
79+
<Reference Include="System.Net.WebSockets">
80+
<HintPath>packages\nanoFramework.System.Net.WebSockets.Server.1.0.0-preview.75\lib\System.Net.WebSockets.dll</HintPath>
81+
</Reference>
82+
<Reference Include="System.Threading">
83+
<HintPath>packages\nanoFramework.System.Threading.1.0.4-preview.14\lib\System.Threading.dll</HintPath>
84+
</Reference>
85+
<Reference Include="Windows.Storage">
86+
<HintPath>packages\nanoFramework.Windows.Storage.1.4.6-preview.25\lib\Windows.Storage.dll</HintPath>
87+
</Reference>
88+
<Reference Include="Windows.Storage.Streams">
89+
<HintPath>packages\nanoFramework.Windows.Storage.Streams.1.12.4-preview.14\lib\Windows.Storage.Streams.dll</HintPath>
90+
</Reference>
91+
</ItemGroup>
92+
<ItemGroup>
93+
<None Include="packages.config" />
94+
</ItemGroup>
95+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
96+
<ProjectExtensions>
97+
<ProjectCapabilities>
98+
<ProjectConfigurationsDeclaredAsItems />
99+
</ProjectCapabilities>
100+
</ProjectExtensions>
101+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32014.148
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "NFWebSockets.Server.RgbExample", "NFWebSockets.Server.RgbExample.nfproj", "{5F445929-F0D2-4280-AC92-47EE05A8EFCA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5F445929-F0D2-4280-AC92-47EE05A8EFCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5F445929-F0D2-4280-AC92-47EE05A8EFCA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5F445929-F0D2-4280-AC92-47EE05A8EFCA}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{5F445929-F0D2-4280-AC92-47EE05A8EFCA}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{5F445929-F0D2-4280-AC92-47EE05A8EFCA}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{5F445929-F0D2-4280-AC92-47EE05A8EFCA}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {F7C20893-22C0-4EAB-B3F2-EBE6A41FCEE7}
26+
EndGlobalSection
27+
EndGlobal

0 commit comments

Comments
 (0)