【发布时间】:2019-06-10 15:08:18
【问题描述】:
我目前正在学习 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 文件的人,我添加了以下代码:
应用程序.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;
});
});
希望这会有所帮助:)
【问题讨论】:
标签: javascript python flask socket.io