Delay Problems when connecting to Flask-SocketIO
AndiHal opened this issue · 1 comments
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 a message is only emitted once every 2 seconds. I have tried around and even when I use a key-stroke event to emit the message to the server, it only works every 2 seconds. Moreover the unity application gets really slow then.
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()
{
if (Input.GetKeyDown(KeyCode.Space))
{
socket.Emit("positionData", "1337");
Debug.Log("data sent");
}
}
private void OnDestroy()
{
socket.Disconnect();
}
}
Unfortunately, I have not tested at Flask.
Try this library!