2016-07-23 6 views
1

私はいくつかのライブのつぶやきを取得するために、フラスコsocketioを使用しようとしている。現在、私はそれがどのように動作するかを確認するためにsocketioで遊んでいます。しかし、私のテストでは、私は時々400(BAD REQUEST)を取得します。私は何が問題なのだろうかと思っています。フラスコ

socket.io.js:3511 POST http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYW0D&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)Request.create @ socket.io.js:3511Request @ socket.io.js:3424XHR.request @ socket.io.js:3352XHR.doWrite @ socket.io.js:3365(anonymous function) @ socket.io.js:3884(anonymous function) @ socket.io.js:4747proxy @ socket.io.js:1197(anonymous function) @ socket.io.js:4762(anonymous function) @ socket.io.js:4742exports.encodePacket @ socket.io.js:4541encodeOne @ socket.io.js:4741eachWithIndex @ socket.io.js:4760map @ socket.io.js:4767exports.encodePayload @ socket.io.js:4746Polling.write @ socket.io.js:3883Transport.send @ socket.io.js:2912Socket.flush @ socket.io.js:2623Socket.sendPacket @ socket.io.js:2683Socket.ping @ socket.io.js:2587(anonymous function) @ socket.io.js:2574 socket.io.js:3511 GET http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYPYb&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)Request.create @ socket.io.js:3511Request @ socket.io.js:3424XHR.request @ socket.io.js:3352XHR.doPoll @ socket.io.js:3382Polling.poll @ socket.io.js:3795Polling.onData @ socket.io.js:3834(anonymous function) @ socket.io.js:3385Emitter.emit @ socket.io.js:4389Request.onData @ socket.io.js:3546Request.onLoad @ socket.io.js:3627xhr.onreadystatechange @ socket.io.js:3499 socket.io.js:3511 POST http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYeOF&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)

私のサーバー側のコード(Pythonのフラスコ):これらは、初期ます

socket = io.connect("http://" + document.domain + ":" + location.port + "/npTweet"); 
console.log("http://" + document.domain + ":" + location.port + "/npTweet"); 
// listen to the event 'connected' 
socket.on("connected", function(data){ 
    console.log("listening connected..."); 
    socket.emit("startTweets") 
}); 

socket.on("streamTweets", function(data){ 
    console.log("listen streamTweets..."); 
    console.log(data.stream_result) 
}); 

from flask import Flask 
from flask_socketio import SocketIO 
app = Flask(__name__) 
app.config["SECRET_KEY"] = "secret!" 
socketio = SocketIO(app) 

def root(): 
    return app.send_static_file("index.html") 

@socketio.on("connect", namespace="/npTweet") 
def connectServer(): 
    print("Client connected") 
    socketio.emit("connected", namespace="/npTweet") 


@socketio.on("startTweets", namespace="/npTweet") 
def tweetStreaming(): 
    print("Start streaming tweets...") 
    socketio.emit("streamTweets", {"stream_result": "test"}, namespace="/npTweet") 


if __name__ == "__main__": 
    socketio.run(app, debug=True) 

私のクライアント側のコードここ

はエラーサンプルですindex.htmlページを起動すると出力されます:

http://127.0.0.1:5000/npTweet 
listening connected... 
listen streamTweets... 
test 

が、第2 30かそこらのような後に、それは次のような結果に続いて、前回の400不正な要求エラーを持つようになった:

listening connected... 
listen streamTweets... 
test 

は、その後、30秒などの後、それは悪い要求を行いますそしてその結果は何度も繰り返されます。どこが間違っているのですか?どんな助けもありがとう。クライアント側のための

、私はsocketioはPython用1.4.5

をJS使用しています: フラスコ-SocketIOの== 2.5 のpython-engineio == 0.9.2

答えて

1

これはまたとして掲載されましたGitHubの問題here

問題がフラスコサーバーが新しいフラスコ0.11 CLIによると、flask runとして開始されたということでした。残念なことに、このサーバーの起動方法は、Socket.IOコードをバイパスします。解決策は、if __name__ == 'main':ブロック内のコードが実行されるようにスクリプトを実行することです。