Ad

Socket Is Creating A Single Instance . How To Create This Application From Multiple Clients?

- 1 answer

I am using Flask server. I am capturing face from front-end and sending it to the flask server and server detects the face and returns if there is a face or not.But the problem is only one connection is there between a client and a server. Can you please tell me how to create multiple clients for socket? Except (rooms and namespaces) Thank you !

Here is some code

FlaskServer.py

from flask import Flask, render_template, redirect, url_for
from flask_socketio import SocketIO
import face_reg

app = Flask(__name__)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app,cors_allowed_origins='*')


@app.route('/')
def sessions():
    return render_template('flask_index.html')


def messageReceived(methods=['GET', 'POST']):
    print('message was received!!!')

global result

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

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

@socketio.on('my event')
def handle_my_custom_event(json, methods=['GET', 'POST']):
    result = 1
    socketio.emit('my response', result, callback=messageReceived)


if __name__ == '__main__':
    socketio.run(app,host='127.0.0.1',port=8083)

client.js

var socket = io.connect('https://' + document.domain + ':' + location.port);

 socket.on( 'my response', function( msg ) {

            if(msg == 0){
                window.location.href = "/failure";
                        }
            else if(msg == 1){
                window.location.href = "/success";  
            }
 })

// Sending message to server
    function sendMessage(img,name1){
    console.log("Sending message: ",img)
        socket.emit( 'my event', {
                img_data : img,
                    name : name1
        } )
        //socket.emit(name);
   }
Ad

Answer

The mistake I was doing was very small, I was returning response using socket.emit which was sending response to all client, so I replaced it with emit which sends response to particular client only.

Ad
source: stackoverflow.com
Ad