Skip to content

Request delay when using Flask-SocketIO with Unity client #1547

Description

@AndiHal

Question

Hello. I am currently developing an app where I require quite extensive exchange of data between a python application and the Unity game engine. I made a test implementation with Flask-SocketIO and this socket.io unity client. The setup of both was pretty easy and the connection worked well. However, now the problem. I wanted to see how quickly and effectively the data exchange works and wrote an application that emits a message every 0.1 seconds from the unity client. Then I noticed that on the Flask-SocketIO side, a message only arrives every 2 seconds. I have tried around and it seems to be the fastest there is a message logged on server side. Interestingly, the emitting of the messages on the client side seems to work well. I coded it, so the client sends 100 messages and then stops. When the client has been finished for a long time, messages still arrive at the server (in 2 second steps). So they are in some kind of queue.

I am working on version 4.3.2 of Flask-SocketIO. This was neccessary to connect with the Unity client.

Server code:

rom flask import Flask, request, render_template
from flask import jsonify
from flask_socketio import SocketIO, send, emit
from flask_sockets import Sockets

positionData = [0];
app = Flask(__name__)
#sockets = Sockets(app)
from dashboard import init_dashboard
app = init_dashboard(app)
socketio = SocketIO( app, logger=True, engineio_logger=True , async_handlers = True)


@app.route('/testApi', methods = ['GET', 'POST'])
def booksFunction():
   if request.method == 'GET':
       return jsonify(username = "test", email = ["testmail", "aaa"], test =  "test")
   elif request.method == 'POST':
       print(request.json);
       positionData.append(request.json["x"]);
       return "nothing"

@app.route('/')
def hello_world():
    return render_template('landingpage.html')


@socketio.on('connect')
def test_connect():
    print("Client connnected")
    return "hello"
    #emit('chat', {'data': 'Connected'})

@socketio.on('message')
def handle_message(data):
    print('received message: ' + data)
    return "message"

@socketio.on('positionData')
def handle_message(data):
    print('received position: ' + data)
    return "hello"

if __name__ == '__main__':

    socketio.run(host='127.0.0.1')

Client code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Socket.Quobject.SocketIoClientDotNet.Client;
using System.Net.WebSockets;

public class SocketConnection : MonoBehaviour
{
    bool connected = false;
    private QSocket socket;

    void Start()
    {
        Debug.Log("start");
        socket = IO.Socket("http://localhost:5000/");

        socket.On(QSocket.EVENT_CONNECT, data => {
            Debug.Log("Connected");
            socket.Send("Client has connected");
            connected = true;
          // socket.Emit("positionaData", "Position");
           
           
        });

        socket.On("chat", data => {
            Debug.Log("data : " + data);
        });

        

        StartCoroutine(SendPositionData());
    }

    IEnumerator SendPositionData()
    {
   
        for(int i = 0; i < 100; i++)
        {
            yield return new WaitForSeconds(0.1f);

            socket.Emit("positionData", "1337");

            Debug.Log("data sent");

        }
 
        yield return null;

    }

    private void Update()
    {
     
    }


    private void OnDestroy()
    {
        socket.Disconnect();
    }
}

Logs

received event "positionData" from 7bbd1ff0bf294344870a6c02ada47ebc [/]
127.0.0.1 - - [10/May/2021 10:22:04] "POST /socket.io/?EIO=3&transport=polling&sid=7bbd1ff0bf294344870a6c02ada47ebc&t=637562389228381485-5&b64=1 HTTP/1.0" 200 -
7bbd1ff0bf294344870a6c02ada47ebc: Received packet MESSAGE data 2[
  "positionData",
  "1337"
]
received event "positionData" from 7bbd1ff0bf294344870a6c02ada47ebc [/]
127.0.0.1 - - [10/May/2021 10:22:06] "POST /socket.io/?EIO=3&transport=polling&sid=7bbd1ff0bf294344870a6c02ada47ebc&t=637562389248414534-6&b64=1 HTTP/1.0" 200 -
7bbd1ff0bf294344870a6c02ada47ebc: Received packet MESSAGE data 2[
  "positionData",
  "1337"
]
received event "positionData" from 7bbd1ff0bf294344870a6c02ada47ebc [/]
127.0.0.1 - - [10/May/2021 10:22:08] "POST /socket.io/?EIO=3&transport=polling&sid=7bbd1ff0bf294344870a6c02ada47ebc&t=637562389268467265-7&b64=1 HTTP/1.0" 200 -
7bbd1ff0bf294344870a6c02ada47ebc: Received packet MESSAGE data 2[
  "positionData",
  "1337"
]
received event "positionData" from 7bbd1ff0bf294344870a6c02ada47ebc [/]
127.0.0.1 - - [10/May/2021 10:22:10] "POST /socket.io/?EIO=3&transport=polling&sid=7bbd1ff0bf294344870a6c02ada47ebc&t=637562389288501077-8&b64=1 HTTP/1.0" 200 -

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions