Open chrish2 opened 1 year ago
This doesn't work because there are 2 namespaces:
var client = new SocketIOClient.SocketIO("http://server_address:port/");
Hope this helps.
(edited because of grammar error)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.
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.
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?
@chrish2 , @JerkoLannoo
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();
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?
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)
@chrish2 @JerkoLannoo thank you sir soo much , it works
Hi all,
After spending few hours on figuring out how to implemt this, here is final solution that may help. I am using Apache server with Proxy Pass and SSL, you can find two examples below. Option one is insecure http, seconds is configured a bit different with Option parameter and has few settings set.
This is based on package version 3.1.2 and socket server version 4.8.0.
using SocketIOClient;
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading.Tasks;
using SocketIOClient.Transport;
using SocketIO.Core;
using SocketIOClient;
using SocketIO;
using static System.Collections.Specialized.BitVector32;
using static System.Windows.Forms.DataFormats;
using System.Security.Cryptography.Xml;
namespace Socketi
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
ConnectToSocket();
}
private async void ConnectToSocket()
{
// 1) # Use on localhost
// const string serverUrl = "http://localhost:5300";
// var socket = new SocketIOClient.SocketIO(serverUrl);
// ==
// 2) # Use with SSL in profuction
// Apache with proxy pass to nodejs internal localhost:5300 (for example)
const string serverUrl = "https://YOUR_DOMAIN";
var socket = new SocketIOClient.SocketIO(serverUrl, new SocketIOOptions
{
Reconnection = true,
AutoUpgrade = false, // default is true, and has to be false for polling and Apacke Proxy pass in production
Transport= TransportProtocol.Polling, // default is WebSocket, and has to be Polling for Apache Proxy pass in production
});
// ==
// Handle connection events (optional but recommended)
socket.On("connect", (data) =>
{
Console.WriteLine("Connected to Socket.IO server!");
});
socket.OnConnected += async (sender, e) =>
{
await socket.EmitAsync("message", "Heloooo from client!");
};
socket.On("message", response =>
{
Console.WriteLine(response);
});
socket.On("disconnect", (socket) =>
{
Console.WriteLine("Disconnected from Socket.IO server.");
});
// Connect to the server (make this asynchronous)
await socket.ConnectAsync();
// Disconnect before closing the application (optional)
// socket.DisconnectAsync();
}
}
}
require('dotenv').config();
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const PORT = process.env.SERVER_PORT || 3000;
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
app.get('/', (req, res) => {
// Serve the index.html file
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// Handle socket connection
io.on('connection', (socket) => {
console.log('New client connected: ' + socket.id);
// Send message to client
io.emit('message', 'Hello from server');
// Handle disconnection
socket.on('disconnect', () => {
console.log('Korisnik diskonectovan: ' + socket.id);
});
// Handle message from client
socket.on('message', (data) => {
console.log('Message received! - ' + data);
socket.emit('message', 'Hello from server ' + date);
});
});
// New route to send message to all connected clients
app.get('/send', (req, res) => {
const message = req.query.message;
if (message) {
io.emit('message', message);
res.send(`Message "${message}" sent to all connected clients`);
} else {
res.status(400).send('Message parameter is required');
}
});
// Start the server
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
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.