Socket 正在创建单个实例。如何从多个客户端创建此应用程序?
Socket is creating a single instance . How to create this application from multiple clients?
我正在使用 Flask 服务器。我从前端捕获人脸并将其发送到烧瓶服务器,服务器检测到人脸,returns 如果有人脸或 not.But 问题是客户端和客户端之间只有一个连接服务器。你能告诉我如何为套接字创建多个客户端吗?除了(房间和命名空间)谢谢!
这是一些代码
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);
}
我犯的错误很小,我使用 socket.emit
返回响应,它向所有客户端发送响应,所以我将其替换为 emit
,它只向特定客户端发送响应。
我正在使用 Flask 服务器。我从前端捕获人脸并将其发送到烧瓶服务器,服务器检测到人脸,returns 如果有人脸或 not.But 问题是客户端和客户端之间只有一个连接服务器。你能告诉我如何为套接字创建多个客户端吗?除了(房间和命名空间)谢谢!
这是一些代码
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);
}
我犯的错误很小,我使用 socket.emit
返回响应,它向所有客户端发送响应,所以我将其替换为 emit
,它只向特定客户端发送响应。