doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
715 stars 124 forks source link

How to use this library in a C# .NET project #343

Open chrish2 opened 9 months ago

chrish2 commented 9 months ago

I am kinda new to the C# and .NET. I installed this package in the Nuget, and I copy and paste the example code from the Readme.
I import the SocketIO and SocketIOClient in my program. However, It DOES NOT work. Could you provide a very simple example c# code that I can directly run without too many changes?

Am I correct to install this package from Nuget ? or if there are any other ways to put this library in to a project (Since I am new to C#)? Which one should I import to my project (SocketIO or SocketIOClient)Thank you.

JerkoLannoo commented 8 months ago

This doesn't work because there are 2 namespaces:

chrish2 commented 7 months ago

Thank you and this works! Are there any c# project in Github that uses SocketIOClient? I just want to learn how to use it from a well written project instead of I try hundreds times to let it work. Thank you.

NandishNR commented 2 months ago

This doesn't work because there are 2 namespaces:

  • one with the name "SocketIO"
  • and one with the name "SocketIOClient", containing a class called "SocketIO", which they are referring to in the "Quick Start" section. If you use the name "SocketIO", C# thinks you are referring to the namespace, which is not possible. You need to specify the namespace (SocketIOClient) and then the class name (SocketIO), like this: var client = new SocketIOClient.SocketIO("http://server_address:port/"); Hope this helps. (edited because of grammar error)

I have followed the same but notning is working. Execution goes inside ConnectAsync will not come out only.

JerkoLannoo commented 2 months ago

I have followed the same but notning is working. Execution goes inside ConnectAsync will not come out only.

What do you mean? What error do you get?

NandishNR commented 2 months ago

@chrish2 , @JerkoLannoo

  1. Server package version: "https://www.npmjs.com/package/socket.io/v/2.5.0"
  2. Client package version: Latest version, 2.0.0, 2.3.0,3.0.0, etc..
  3. Client built using .Net 6.0
  4. Able to build connection using postman
  5. Checked Wireshark logs as well - nothing blocked on laptop like antivirus or proxy

Below is the code, where not able to build connection and execution will not comeout from "await _socketIO.ConnectAsync();". Connected flag is false and not able emit the message.

SocketIOOptions options = new SocketIOOptions(); options.Path = SocketPath; options.Query = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("token", Accesstoken), new KeyValuePair<string, string>("deviceId", DeviceId) }; options.Transport = SocketIOClient.Transport.TransportProtocol.WebSocket; options.Reconnection = true; options.ReconnectionDelayMax = 10; options.ReconnectionDelay = 1000; options.ConnectionTimeout = TimeSpan.FromMilliseconds(10000);

Uri uri = new Uri(SocketUri); SocketIOClient.SocketIO _socketIO = new SocketIOClient.SocketIO(uri, options); _socketIO.OnConnected += OnSocketIOConnected; _socketIO.OnReconnected += OnSocketIOReconnected; _socketIO.OnError += OnSocketIOError; await _socketIO.ConnectAsync();

NandishNR commented 2 months ago

Hi @chrish2 , @JerkoLannoo,

Upgraded sever to latest package and able to connect .net client. However, what ever sever emit the events .net client is not received but received in react client.

Any inputs on this?