我正在尝试设置我的第一个flask_socketio应用程序,并且遇到了客户端将随机与服务器断开连接的问题。
我在应用程序中有一个名为“ propertytaxes”的自定义函数,该函数根据日志执行预期的操作,当我尝试将信息从服务器传递回客户端时,断开连接会发生。
这是我的app.py代码:
from flask import Flask, render_template, flash, redirect, request, url_for, Response, send_file
from flask_socketio import SocketIO
import propertytaxes
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, logger=True, engineio_logger=True)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('confirm')
def confirmconnect(data):
print(data)
@socketio.on('submit')
def taxesreceive(data):
print(data)
# Create datatable for pins and taxes
df = pd.DataFrame(columns=["PIN", "Taxes"], data=[])
# Format the pins correctly
try:
for x in newparcel:
propertytaxes(x)
df = df.append(pd.DataFrame({'PIN': x, "Taxes": propertytaxes.num}, index=[0]), ignore_index=True)
socketio.emit('progress', "<tr class='pin-row'><td class='pin-cell'>{}</td><td class='tax-cell'>{}</td></tr>".format(x,propertytaxes.num))
except SyntaxError:
socketio.emit('progress', "<tr class='pin-row'><td class='error-cell1'>{}</td><td class='error-cell2'>{}</td></tr>".format(x,"FORMATTING ERROR!"))
socketio.emit('response', "<tr class='pin-row'><td class='pin-cell'><b>TOTAL</b></td><td class='tax-cell'><b>{}</b></td></tr>".format(finalnum))
if __name__ == '__main__':
socketio.run(app, debug=True)
相关JS代码:
submit.on('click', function(e){
e.preventDefault()
validate(input)
var check = true;
for(var i=0; i<input.length; i++) {
if(validate(input[i]) == false){
showValidate(input[i]);
check=false;
}
else{
if($('#results-table') !== 'undefined'){
$('#results-table').remove();
}
if($('#downloadsection') !== 'undefined'){
$('#downloadsection').remove();
}
if($('#loading') !== 'undefined'){
$('#loading').remove();
}
let pins = input.val()
socket.emit('submit', pins)
$( '#results' ).append('<div id="loading" class="animated infinite pulse"><b>Finding your property taxes...</b></div>')
$( '#results' ).append("<table id='results-table'><tbody class='pin-table'><tr class='row-head'><td class='pin-td-head'>PIN(S)</td><td class='tax-td-head'>Taxes</td></tr></tbody></table>")
}
}
return check;
})
socket.on('progress', function(msg) {
if(msg !== 'undefined') {
$( '.pin-table' ).append(msg)
}
})
socket.on('response', function(msg) {
if(msg !== 'undefined') {
$('#loading').remove();
input.val('');
$( '.pin-table' ).append(msg);
$('#results').append(download);
}
})
我希望socketio在@socketio.on('submit')
for loop
内不断发出信息行,但是相反,在for loop
内的第二个或第三个进程之后,我收到以下错误:
emitting event "response" to all [/]
123eecc0eda24a88bc399248dc2cce90: Client is gone, closing socket
123eecc0eda24a88bc399248dc2cce90: Client is gone, closing socket
1ec1434d61c941cc9ec5ff0bf148ddad: Client is gone, closing socket
1ec1434d61c941cc9ec5ff0bf148ddad: Client is gone, closing socket
123eecc0eda24a88bc399248dc2cce90: Received packet PING data None
Receive error -- socket is closed
1ec1434d61c941cc9ec5ff0bf148ddad: Received packet PING data None
Receive error -- socket is closed
127.0.0.1 - - [03/Apr/2019 15:37:03] "GET /socket.io/?EIO=3&transport=websocket&sid=1ec1434d61c941cc9ec5ff0bf148ddad HTTP/1.1" 200 0 34.393236
127.0.0.1 - - [03/Apr/2019 15:37:03] "GET /socket.io/?EIO=3&transport=websocket&sid=123eecc0eda24a88bc399248dc2cce90 HTTP/1.1" 200 0 34.848501
47539815c48a48e099ad9d1645dba03d: Sending packet OPEN data {'sid': '47539815c48a48e099ad9d1645dba03d', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 25000}
47539815c48a48e099ad9d1645dba03d: Sending packet MESSAGE data 0
127.0.0.1 - - [03/Apr/2019 15:37:05] "GET /socket.io/?EIO=3&transport=polling&t=MdawsDz HTTP/1.1" 200 381 0.002992
23b9186ccfcd4e0ca037806152d43f18: Sending packet OPEN data {'sid': '23b9186ccfcd4e0ca037806152d43f18', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 25000}
23b9186ccfcd4e0ca037806152d43f18: Sending packet MESSAGE data 0
127.0.0.1 - - [03/Apr/2019 15:37:05] "GET /socket.io/?EIO=3&transport=polling&t=MdawsEB HTTP/1.1" 200 381 0.001997
(12944) accepted ('127.0.0.1', 63246)
47539815c48a48e099ad9d1645dba03d: Received request to upgrade to websocket
47539815c48a48e099ad9d1645dba03d: Received packet MESSAGE data 2["confirm",{"data":"User Connected"}]
received event "confirm" from 47539815c48a48e099ad9d1645dba03d [/]
{'data': 'User Connected'}
127.0.0.1 - - [03/Apr/2019 15:37:05] "POST /socket.io/?EIO=3&transport=polling&t=MdawsEH&sid=47539815c48a48e099ad9d1645dba03d HTTP/1.1" 200 219 0.001993
127.0.0.1 - - [03/Apr/2019 15:37:05] "GET /socket.io/?EIO=3&transport=polling&t=MdawsEI&sid=47539815c48a48e099ad9d1645dba03d HTTP/1.1" 200 215 0.000999
(12944) accepted ('127.0.0.1', 63247)
23b9186ccfcd4e0ca037806152d43f18: Received request to upgrade to websocket
23b9186ccfcd4e0ca037806152d43f18: Received packet MESSAGE data 2["confirm",{"data":"User Connected"}]
received event "confirm" from 23b9186ccfcd4e0ca037806152d43f18 [/]
{'data': 'User Connected'}
127.0.0.1 - - [03/Apr/2019 15:37:05] "POST /socket.io/?EIO=3&transport=polling&t=MdawsET&sid=23b9186ccfcd4e0ca037806152d43f18 HTTP/1.1" 200 219 0.000996
47539815c48a48e099ad9d1645dba03d: Upgrade to websocket successful
127.0.0.1 - - [03/Apr/2019 15:37:05] "GET /socket.io/?EIO=3&transport=polling&t=MdawsEU&sid=23b9186ccfcd4e0ca037806152d43f18 HTTP/1.1" 200 215 0.002988
23b9186ccfcd4e0ca037806152d43f18: Upgrade to websocket successful