.NET WebSocket Client

Comments

Gravatar Image
mqmarathon 11/4/2020 11:36:50 PM
Thank you for sharing this! I needed to implement a WebSocket client in my Windows Form Application to get real-time sensor data (Arduino). I have used a serial port for the data transfer up until today and had to make it wireless with the wifi functionality. So this works perfectly without making a lot of changes in my application. I wonder if there is a way to use SingnalR in Windows Form Application for the same purpose just as a WebSocket client. Is this an unlucky case where I can't use SignalR? Could you let me know your thought on this? If it's possible and you could quickly guide me on how to do it (if it's simple), it would be really great! Thanks a lot!
Gravatar Image
Jeffrey Rosselle 11/12/2020 4:15:40 PM
In general I find setting up a SignalR application easier then just plain websockets. So I wouldn't see why you would not be able to this. Although I have no experience in setting it up for a Windows Form Application. So there might be some issues. I have an older post where I use SignalR (int .NET Framework), maybe it can help you out: https://thecodegarden.net/net-chat-application-azure-service-bus-topics-signalr
Gravatar Image
brad 10/29/2020 9:13:17 PM
This seemingly works great until you want to parallelize sending/receiving messages on multiple threads very fast, then it gets ugly.. Ended up using Websocket.Client nuget package from 'Marfusios' which makes it a bit simpler
Gravatar Image
Götz 10/13/2020 5:09:14 PM
Hi Jeffrey, I just tried to play around with your code and keep getting an error in the Send task Encoding.UTF8.GetBytes(data) does not work: Error CS1503 Argument 1: cannot convert from 'byte[]' to 'System.ArraySegment<byte>' I am just trying to learn c# by doing things I've done in other languages before. I'm only experienced in scripting (python, perl), so I know all the networking stuff, but I'm a little lost with those conversions... I googled but cannot find the solution...
Gravatar Image
Jeffrey Rosselle 10/15/2020 12:15:30 PM
Hi Götz, that's unfortunate... and strange. Can you tell me which SDK you're using?
Gravatar Image
Lorna 11/9/2020 4:07:05 PM
I'm having the same issue and would be grateful of some help. My project is a class library targeting .NET framework 4.6.1. The package I have added is System.Net.WebSockets
Gravatar Image
Lorna 11/10/2020 3:09:01 PM
Please disregard above. I read through the comments and realised this example is for .NET Core. If I create a .NET Core Console App I don't get this error.
Gravatar Image
JonR 8/9/2020 6:06:10 PM
Hi Jeffrey, I am working on a project to create a "bot" for buying/selling. I want to build my project initially in win32 and perhaps migrate to ASP.NET, I have different provider API's to support across different trading platforms. I assume that the WebSocketService can be changed to work on a Win32 background worker class or similar. Is there any chance you could rework the Background Service to accomodate a Win32 environment as an enhancement to your post? I am sure others will be searching the same things as me ... I am happy to sort out a paypal "beer" ! i'll keep an eye out here for your reply ... thanks, jON
Gravatar Image
Steve 9/23/2020 8:35:19 PM
Can anyone explain what server is used in this example? I see two clients, one is a .NET Core console app (thank you!!!!) and the other seems to be another client, but running in ASP.NET Core?
Gravatar Image
Jeffrey Rosselle 9/24/2020 12:07:52 PM
Correct, the client is written in for ASP.NET Core.
Gravatar Image
Another person 7/28/2020 3:37:49 PM
Great understandable example, thanks! Curious, how would you access the websocket from other classes in order to Send messages (not in response to receiving a message)? Wondering if in that case one can't use `using`, and needs to handle closing themselves (and if you have any knowledge about doing that).
Gravatar Image
Jeffrey Rosselle 9/24/2020 12:11:31 PM
I think there are multiple ways for doing this. One could be like the Queued background tasks (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#queued-background-tasks). You could write a queue for messages you want to send and let the other Background Services handle them.