doghappy / socket.io-client-csharp

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

Problem with Connenct to flask_socketio #328

Closed puyansaraei closed 10 months ago

puyansaraei commented 1 year ago

Hello,

I want, with your class library, to establish a connection to a "flask_socketio server".

The connection to the socket server is also established. The communication between server and client works. But the "ConnectAsync()" still throws a "TimeOut-Exception".

Because of this, the "Connected" and "sid" values do not update.

Does your client expect a response from server to know if the connection is established?

from flask_socketio import SocketIO, emit

app = Flask(__name__,static_folder='static')
app.config['SECRET_KEY'] = 'secret'
CORS(app,resources={r'/':{'origins':'*'}})
socketio = SocketIO(app,logger=cfg.debug, engineio_logger=cfg.debug)

WSClient = WebSocketClient(socketio)
@socketio.on('connect')
def handle_connect(auth):
    log.logging.info(f'handle_connect start: {auth} : {request.sid} ')
    global objectidentify
    objectidentify.send_message()
    log.logging.info(f'handle_connect end: {auth} : {request.sid}')

class WebSocketClient():
    def __init__(self, socketio:SocketIO):
        self.socketio = socketio

    def send_message(self,type : SocketIOMessageType,message):
        # Senden der Nachricht an den Client
        log.logging.info(f'WebSocketClient : send_message : {type}  :  {message}')
        self.socketio.emit(type, message)

    def handle_message(self, message):
        # Verarbeiten der empfangenen Nachricht
        print(f'WebSocketClient : handle_message : ', message)

class SocketIOMessageType():
    readyToTakeFoto = "readytotakefoto"
    connected = "connected"
SocketIOOptions options = new SocketIOOptions() {
                ReconnectionDelay = 500,
                ConnectionTimeout = new TimeSpan(0,0,0,10,0),
                Reconnection = true,
                EIO= EngineIO.V4,
                Transport = TransportProtocol.Polling,
                Path = "/socket.io",
                AutoUpgrade = true

            };

client = new SocketIO($"http://{this.piIP}", options);

 Task.Run(async () =>
            {
                Console.WriteLine($"SocketIO {piIP}");
                Logger.Log($"ConnectAsync: {piIP}");
                try { await _client.ConnectAsync(); }
                catch(Exception e )
                {
                    Logger.Log($"ConnectAsync Exception: {e.GetType().FullName} : {e.Message}");
                    if (e.InnerException != null )
                    {
                        Logger.Log($"ConnectAsync Exception: {e.InnerException.GetType().FullName} : {e.InnerException.Message}");
                        if (e.InnerException.InnerException != null)
                        {
                            Logger.Log($"ConnectAsync Exception: {e.InnerException.InnerException.GetType().FullName} : {e.InnerException.InnerException.Message}");
                            if (e.InnerException.InnerException.InnerException != null)
                            {
                                Logger.Log($"ConnectAsync Exception: {e.InnerException.InnerException.InnerException.GetType().FullName} : {e.InnerException.InnerException.InnerException.Message}");
                            }
                        }
                    }
                }
            });
doghappy commented 11 months ago

I can connect to flash socket.io

server:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@socketio.event
def my_event(message):
    emit('my response', {'data': 'got it!'})

if __name__ == '__main__':
    socketio.run(app)

client:

            var uri = new Uri("http://127.0.0.1:5000");
            var socket = new SocketIO(uri);
            socket.OnConnected += (sender, e) =>
            {
                Console.WriteLine("Connected " + socket.Namespace);
            };
            await socket.ConnectAsync();