doghappy / socket.io-client-csharp

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

Can't connect socketIO #332

Closed phanthanhdat077 closed 11 months ago

phanthanhdat077 commented 11 months ago

Dictionary<string, string> query = new Dictionary<string, string>(); query["token"] = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhMjY1NzkzNi1mN2QxLTQ1M2MtOTNiMi05YjJkZTEyODNkNGQiLCJpYXQiOjE2ODIwOTA4MjgsInN1YiI6IjIiLCJpc3MiOiJURUNIUkVTIiwiZXhwIjoxNjk3NjQyODI4LCJ1c2VyX2lkIjo1MTE0LCJjdXJyZW50X3RpbWVzdGFtcCI6MTY4MjA5MDgyODQwNn0.07KUOAozBm7twGFqFXaCrgdEej_wUsowgnOO3RepoEc"; var options = new SocketIOOptions() { Reconnection = true, ReconnectionDelay = 250, Auth = query, AutoUpgrade = true, Query = query, }; var uri = new Uri("https://beta.realtime.order.techres.vn/restaurants_3103_branches_5622"); var socket = new SocketIO(uri, options); socket.ConnectAsync(); socket.OnConnected += (s, e) =>{ WriteLog.logs("Connected " + socket.Namespace); } Why my code can not connect socketIO to https domain. I tried with http still can connect but https can't. Any help?

doghappy commented 11 months ago

Hi, are you sure that connection info are correct? have you tried using nodejs?

phanthanhdat077 commented 11 months ago

that domain is still active: https://beta.realtime.order.techres.vn Any suggestions? like I need to change some options or SSL certificate. but I don't know how to use SSL certificate with SocketIO. I don't know the error on the client or server because I can't debug the event connected or disconnected. I can connect http like this "http://172.16.10.117:1483/restaurants_3103_branches_5622" but https can't "https://beta.realtime.order.techres.vn/restaurants_3103_branches_5622"

doghappy commented 11 months ago

I have tried to connect to your server, but failed. could you show me your options of server.js?

phanthanhdat077 commented 11 months ago

I have tried to connect to your server, but failed. could you show me your options of server.js?

afterInit(server: Server) {

    this.namespace = server.of(/^\/\w+$/); // Lấy chuỗi sau dấu '/' làm namespace   

    this.namespace.on('connection', (socket) => {

        this.handleConnection(socket);

        socket.on('join_room', (room) => {
            console.log('User JOIN ROOM');
            socket.join(room);
            socket.emit('joinned_room', `${socket.id} joining room ${room}`);
        });

        socket.on('leave_room', (room) => {
            console.log(`${socket.id} leaving room ${room}`);
            socket.leave(room);
            socket.emit('left_room', `${socket.id} left room ${room}`);
        });

        socket.on('test_connection', (data) => {
            socket.emit('connected_success', `${socket.id} connected success!`);
        });

    });
}
phanthanhdat077 commented 11 months ago

import { log } from 'console'; import { io, Socket } from 'socket.io-client';

// Connect to server socket //const socket: Socket = io('http://localhost:3000/'); const auth = { token: 'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhNTAzN2M5YS1lNzQ3LTQ2ODItOGZmNi1kZDcxMTZiY2Q4YjMiLCJpYXQiOjE2ODM2MjA0MDAsInN1YiI6IjIiLCJpc3MiOiJURUNIUkVTIiwiZXhwIjoxNjk5MTcyNDAwLCJ1c2VyX2lkIjo1ODA0LCJjdXJyZW50X3RpbWVzdGFtcCI6MTY4MzYyMDQwMDc5MH0.UtPpiJmTI0km_RCxwkxhkJ7t2BstgCzwFa2YYTu-E-k' };

const socket: Socket = io('http://localhost:1498/restaurants_3364_branches_6015', { auth });

// Event 'connect' socket.on('connect', () => { console.log('Connected to server socket'); // Gửi một sự kiện 'message' tới server socket.emit('join_room', 'restaurants/3364/branches/6015/orders'); // socket.emit('message', 'gffgggggggggggggggggggggggggggggg');

}); The backend dev said I just need the token to connect

doghappy commented 11 months ago

following code should be work

            var options = new SocketIOOptions()
            {
                AutoUpgrade = false,
                Auth = new
                {
                    token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhNTAzN2M5YS1lNzQ3LTQ2ODItOGZmNi1kZDcxMTZiY2Q4YjMiLCJpYXQiOjE2ODM2MjA0MDAsInN1YiI6IjIiLCJpc3MiOiJURUNIUkVTIiwiZXhwIjoxNjk5MTcyNDAwLCJ1c2VyX2lkIjo1ODA0LCJjdXJyZW50X3RpbWVzdGFtcCI6MTY4MzYyMDQwMDc5MH0.UtPpiJmTI0km_RCxwkxhkJ7t2BstgCzwFa2YYTu-E-k"
                }
            };
            var uri = new Uri("https://beta.realtime.order.techres.vn/restaurants_3364_branches_6015");
            var socket = new SocketIO(uri, options);
            socket.OnConnected += (sender, e) =>
            {
                Console.WriteLine("Connected " + socket.Namespace);
            };
            await socket.ConnectAsync();
phanthanhdat077 commented 11 months ago

following code should be work

            var options = new SocketIOOptions()
            {
                AutoUpgrade = false,
                Auth = new
                {
                    token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhNTAzN2M5YS1lNzQ3LTQ2ODItOGZmNi1kZDcxMTZiY2Q4YjMiLCJpYXQiOjE2ODM2MjA0MDAsInN1YiI6IjIiLCJpc3MiOiJURUNIUkVTIiwiZXhwIjoxNjk5MTcyNDAwLCJ1c2VyX2lkIjo1ODA0LCJjdXJyZW50X3RpbWVzdGFtcCI6MTY4MzYyMDQwMDc5MH0.UtPpiJmTI0km_RCxwkxhkJ7t2BstgCzwFa2YYTu-E-k"
                }
            };
            var uri = new Uri("https://beta.realtime.order.techres.vn/restaurants_3364_branches_6015");
            var socket = new SocketIO(uri, options);
            socket.OnConnected += (sender, e) =>
            {
                Console.WriteLine("Connected " + socket.Namespace);
            };
            await socket.ConnectAsync();

it worked. Thank for help