通过c#与socket.io服务器通信

时间:2011-06-27 22:51:27

标签: c# sockets client websocket socket.io

是否有一个遵循socket.io协议的c#客户端?我有一个socket.io服务器通过一个网站与socket.io javascript客户端进行通信,但我还需要连接一个可以发送和接收消息的c#片段。目前是否有一种干净的方法可以做到这一点,或者我必须写自己的客户。

6 个答案:

答案 0 :(得分:43)

codeplex上还有一个项目(NuGet),它是socket.io的C#客户端。 (我是这个项目的作者 - 所以我有偏见)我无法在客户端找到我需要的东西,所以我构建它并将其重新发布到开放状态。

示例客户端样式:

socket.On("news", (data) =>    {
Console.WriteLine(data);
});

答案 1 :(得分:26)

使用以下库:https://github.com/sta/websocket-sharp 它可以通过NuGet获得:

PM> Install-Package WebSocketSharp -Pre

要连接到Socket.IO 1.0 +服务器,请使用以下语法:

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);
}

换句话说,将其附加到localhost:port - “socket.io/?EIO=2&transport=websocket”。

我的完整服务器代码:https://gist.github.com/anonymous/574133a15f7faf39fdb5

答案 2 :(得分:3)

好吧,我发现了另一个.net库,该库可以与socket.io一起使用。它也是最新的。点击以下链接,

Quobject/SocketIoClientDotNet

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();

希望,它可以帮助某人。

答案 3 :(得分:1)

此软件包支持最新协议。
Github-https://github.com/HavenDV/H.Socket.IO/
C#实时示例-https://dotnetfiddle.net/FWMpQ3/
VB.NET实时示例-https://dotnetfiddle.net/WzIdnG/
努吉特:

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();
}

答案 4 :(得分:0)

我尝试了以上所有方法,但是不知何故他们没有与我正在集成的服务进行对话(也许该服务有问题,我不知道是哪个)。所以我写了我自己的。

https://github.com/it9gamelog/socketio-with-ws-client

一种简约的单文件客户端实现。由于socket-io是一项垂死的技术,并且规范非常复杂,因此两侧的错误可能永远不会在任何时间得到修复。单一文件方法至少更易于调整,扩展和调试。

答案 5 :(得分:-2)

这取决于您的网络服务器的外观。在某些情况下,也可能适用于为常规套接字制作一个监听器 否则,您可能需要创建自己的客户端。但是,您可能只需要实现WebSocket传输,因此它应该相当简单。

对于它的价值,我建议您查看问题“Is there a WebSocket client implemented for .NET?”和my (fairly simple) WebSocket Socket.IO transport client implementation for Java