简体   繁体   English

Python Flask-socketio工作,未从JS客户端接收数据

[英]Python Flask-socketio working, not receiving data from JS client

I am trying to setup a basic web socket using flask-socketio and the javascript socket-io library on the front end. 我正在尝试使用前端的flask-socketio和javascript socket-io库设置基本的Web套接字。 The frontend successful registers the connection. 前端成功注册连接。 However, in the python console, my log messages are not working and there is no indication that the server is responding to my requests. 但是,在python控制台中,我的日志消息不起作用,也没有迹象表明服务器正在响应我的请求。

All I get in the console on connect is: "GET /socket.io/?EIO=3&transport=polling&t=MJHl5WD HTTP/1.1" 200 - 127.0.0.1 - - [25/Jul/2018 09:58:58] "POST /socket.io/?EIO=3&transport=polling&t=MJHl5WQ&sid=b2a1f92c18e349c5a11df25e5001766c HTTP/1.1" 200 - 我在控制台上获得的所有内容是: "GET /socket.io/?EIO=3&transport=polling&t=MJHl5WD HTTP/1.1" 200 - 127.0.0.1 - - [25/Jul/2018 09:58:58] "POST /socket.io/?EIO=3&transport=polling&t=MJHl5WQ&sid=b2a1f92c18e349c5a11df25e5001766c HTTP/1.1" 200 -

So the connection is working, however, nothing else on the back end is outputting as it should. 因此,连接正常,但是后端没有其他输出。

Here is the JS: 这是JS:

var socket=io('http://127.0.0.1:5000');
socket.on('connect',function(){
    console.log("connected");
socket.emit('client_connected',{data:"testsend"});

});
socket.on('update',function(data){
console.log(data)
});

And the python: 和python:

import json
import urllib3
from bs4 import BeautifulSoup
from flask import Flask, jsonify, render_template, request
from flask_socketio import SocketIO, send, emit

app=Flask(__name__)
socketio= SocketIO(app)
if __name__ == '__main__':
    socketio.run(app)
socketio.emit('update',{'data':"test"});

@socketio.on('client_connected')
def handle_client_connect_event(data):
    print("connected")
    print(str(data))    
@socketio.on('disconnect')
def disconnected():
    print('disconnected')
@socketio.on('connect')
def connected():
    print('connected')

None of the @socketio.on lines work, and the 'update' event isn't working either. @socketio.on行均不起作用,并且'update'事件也不起作用。 Help would be greatly appreciated. 帮助将不胜感激。

The event definitions are below the socketio.run() , which starts the server and blocks, so they never get registered. 事件定义在socketio.run()下面,该事件启动服务器并进行阻止,因此它们永远不会被注册。 If you move those above this call they should just work. 如果将这些移动到此调用上方,它们应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM