Is there a c# client that follows the socket.io protocol? I have a socket.io server that is communicating with a socket.io javascript client via a website, but i also need to connect a c# piece to it that can send and receive messages. Is there a clean way to do this currently or will I have to write my own client.
There is a project on codeplex ( NuGet as well ) that is a C# client for socket.io. (I am the author of this project - so I'm biased) I couldn't find exactly what I needed in a client, so I built it and released it back into the open.
Example client style:
socket.On("news", (data) => {
Console.WriteLine(data);
});
Use the following library: https://github.com/sta/websocket-sharp It is available via NuGet:
PM> Install-Package WebSocketSharp -Pre
To connect to a Socket.IO 1.0 + server, use the following syntax:
using (var ws = new WebSocket("ws://127.0.0.1:1337/socket.io/?EIO=2&transport=websocket"))
{
ws.OnMessage += (sender, e) =>
Console.WriteLine("New message from controller: " + e.Data);
ws.Connect();
Console.ReadKey(true);
}
In other words, append this to the localhost:port - "socket.io/?EIO=2&transport=websocket".
My full server code: https://gist.github.com/anonymous/574133a15f7faf39fdb5
This package supports the latest protocol.
Github - https://github.com/HavenDV/H.Socket.IO/
C#
Live Example - https://dotnetfiddle.net/FWMpQ3/
VB.NET
Live Example - https://dotnetfiddle.net/WzIdnG/
Nuget:
Install-Package H.Socket.IO
using System;
using System.Threading.Tasks;
using H.Socket.IO;
#nullable enable
public class ChatMessage
{
public string? Username { get; set; }
public string? Message { get; set; }
public long NumUsers { get; set; }
}
public async Task ConnectToChatNowShTest()
{
await using var client = new SocketIoClient();
client.Connected += (sender, args) => Console.WriteLine($"Connected: {args.Namespace}");
client.Disconnected += (sender, args) => Console.WriteLine($"Disconnected. Reason: {args.Reason}, Status: {args.Status:G}");
client.EventReceived += (sender, args) => Console.WriteLine($"EventReceived: Namespace: {args.Namespace}, Value: {args.Value}, IsHandled: {args.IsHandled}");
client.HandledEventReceived += (sender, args) => Console.WriteLine($"HandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");
client.UnhandledEventReceived += (sender, args) => Console.WriteLine($"UnhandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");
client.ErrorReceived += (sender, args) => Console.WriteLine($"ErrorReceived: Namespace: {args.Namespace}, Value: {args.Value}");
client.ExceptionOccurred += (sender, args) => Console.WriteLine($"ExceptionOccurred: {args.Value}");
client.On("login", () =>
{
Console.WriteLine("You are logged in.");
});
client.On("login", json =>
{
Console.WriteLine($"You are logged in. Json: \"{json}\"");
});
client.On<ChatMessage>("login", message =>
{
Console.WriteLine($"You are logged in. Total number of users: {message.NumUsers}");
});
client.On<ChatMessage>("user joined", message =>
{
Console.WriteLine($"User joined: {message.Username}. Total number of users: {message.NumUsers}");
});
client.On<ChatMessage>("user left", message =>
{
Console.WriteLine($"User left: {message.Username}. Total number of users: {message.NumUsers}");
});
client.On<ChatMessage>("typing", message =>
{
Console.WriteLine($"User typing: {message.Username}");
});
client.On<ChatMessage>("stop typing", message =>
{
Console.WriteLine($"User stop typing: {message.Username}");
});
client.On<ChatMessage>("new message", message =>
{
Console.WriteLine($"New message from user \"{message.Username}\": {message.Message}");
});
await client.ConnectAsync(new Uri("wss://socketio-chat-h9jt.herokuapp.com/"));
await client.Emit("add user", "C# H.Socket.IO Test User");
await Task.Delay(TimeSpan.FromMilliseconds(200));
await client.Emit("typing");
await Task.Delay(TimeSpan.FromMilliseconds(200));
await client.Emit("new message", "hello");
await Task.Delay(TimeSpan.FromMilliseconds(200));
await client.Emit("stop typing");
await Task.Delay(TimeSpan.FromSeconds(2));
await client.DisconnectAsync();
}
It also supports namespaces:
// Will be sent with all messages(Unless otherwise stated).
// Also automatically connects to it.
client.DefaultNamespace = "my";
// or
// Connects to "my" namespace.
await client.ConnectAsync(new Uri(LocalCharServerUrl), cancellationToken, "my");
// Sends message to "my" namespace.
await client.Emit("message", "hello", "my", cancellationToken);
Well, I found another .Net library which works great with socket.io. It is the most updated too. Follow the below link,
using Quobject.SocketIoClientDotNet.Client;
var socket = IO.Socket("http://localhost");
socket.On(Socket.EVENT_CONNECT, () =>
{
socket.Emit("hi");
});
socket.On("hi", (data) =>
{
Console.WriteLine(data);
socket.Disconnect();
});
Console.ReadLine();
Hope, it helps someone.
I tried all of the above but somehow they doesn't talk with the service I am integrating with (maybe the service is bugged, I don't know which). So I wrote my own.
https://github.com/it9gamelog/socketio-with-ws-client
A minimalistic, single-file client implementation. Since socket-io is a dying technology, and the specification is quite complicated, bugs on either side might just never get fixed at any time. A single file approach is at least easier to tune, expand and debug.
This depends on how your webserver looks. In some cases it might be applicable to make a listener for regular sockets too.
Otherwise, you will probably have to make your own client. However, you will probably only need to implement the WebSocket transport so it should be fairly straightforward anyway.
For what it's worth I'd suggest looking at the question "Is there a WebSocket client implemented for .NET?" and my (fairly simple) WebSocket Socket.IO transport client implementation for Java.
© 2022 - 2024 — McMap. All rights reserved.