我目前正在学习CS50的Web开发课程(刚刚结束JavaScript讲座),并且偶然发现了使用SocketIO的Web套接字问题。我运行了以下链接提供的源代码:Source code。我运行了程序:“ vote1”。
当我运行程序时(设置环境变量并运行flask run
之后),需要花一些时间加载并在终端中显示以下内容:
127.0.0.1 - - [10/Jun/2019 16:57:23] "GET /socket.io/?EIO=3&transport=polling&t=1560178643212-45 HTTP/1.1" 200 -
127.0.0.1 - - [10/Jun/2019 16:58:24] "GET /socket.io/?EIO=3&transport=polling&t=1560178643232-46&sid=d8c5c5cf1dcc4cd8b06d4c629c980539 HTTP/1.1" 400 -
127.0.0.1 - - [10/Jun/2019 16:58:24] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [10/Jun/2019 16:58:24] "POST /socket.io/?EIO=3&transport=polling&t=1560178668231-47&sid=d8c5c5cf1dcc4cd8b06d4c629c980539 HTTP/1.1" 400 -
127.0.0.1 - - [10/Jun/2019 16:58:24] "GET /static/index.js HTTP/1.1" 200 -
127.0.0.1 - - [10/Jun/2019 16:58:24] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [10/Jun/2019 16:58:24] "GET /socket.io/?EIO=3&transport=polling&t=1560178704847-0 HTTP/1.1" 200 -
点击“是”按钮后,“是”票本来会增加,但不会增加,而是在大约30秒后在终端上显示:
127.0.0.1 - - [10/Jun/2019 17:02:01] "GET /socket.io/?EIO=3&transport=polling&t=1560178861393-1&sid=852b4c8df564432292c44c878643cf5d HTTP/1.1" 200 -
127.0.0.1 - - [10/Jun/2019 17:02:01] "POST /socket.io/?EIO=3&transport=polling&t=1560178869697-2&sid=852b4c8df564432292c44c878643cf5d HTTP/1.1" 400 -
127.0.0.1 - - [10/Jun/2019 17:02:01] "GET /socket.io/?EIO=3&transport=polling&t=1560178921417-3&sid=852b4c8df564432292c44c878643cf5d HTTP/1.1" 400 -
127.0.0.1 - - [10/Jun/2019 17:02:01] "POST /socket.io/?EIO=3&transport=polling&t=1560178921418-4&sid=852b4c8df564432292c44c878643cf5d HTTP/1.1" 400 -
127.0.0.1 - - [10/Jun/2019 17:02:02] "GET /socket.io/?EIO=3&transport=polling&t=1560178922496-5 HTTP/1.1" 200 -
我正在Linux Mint上使用Firefox(以防出现问题)。
关于如何解决此错误以便程序正常运行的任何想法?
编辑: 我发现您应该包括:
if __name__ == 'main':
socketio.run(app)
我添加了此内容,但仍然存在相同的问题。
该帖子获得了一些否决权,我认为这是因为我发布了指向源代码的链接。对于那些想查看application.py文件和JavaScript文件的人,我添加了以下代码:
Application.py:
import os
import requests
from flask import Flask, jsonify, render_template, request
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
votes = {"yes": 0, "no": 0, "maybe": 0}
@app.route("/")
def index():
return render_template("index.html", votes=votes)
@socketio.on("submit vote")
def vote(data):
selection = data["selection"]
votes[selection] += 1
emit("vote totals", votes, broadcast=True)
Index.js:
document.addEventListener('DOMContentLoaded', () => {
// Connect to websocket
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);
// When connected, configure buttons
socket.on('connect', () => {
// Each button should emit a "submit vote" event
document.querySelectorAll('button').forEach(button => {
button.onclick = () => {
const selection = button.dataset.vote;
socket.emit('submit vote', {'selection': selection});
};
});
});
// When a new vote is announced, add to the unordered list
socket.on('vote totals', data => {
document.querySelector('#yes').innerHTML = data.yes;
document.querySelector('#no').innerHTML = data.no;
document.querySelector('#maybe').innerHTML = data.maybe;
});
});
希望这会有所帮助:)
答案 0 :(得分:0)
我已经解决了这个问题。在Flask-SocketIO的文档中,它指出您应该使用:
if __name__ == '__main__':
socketio.run(app)
与其使用flask run
来运行应用程序,而不必像运行普通的Python程序一样运行它。