Flask-Socketio:无法在事件处理程序中打印

Flask-Socketio : Unable to print within event handlers

我正在尝试创建一个聊天应用程序。我正在使用 Flask-SocketIO 和 Flask-blueprints。

我的js代码是这样的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.min.js"></script>
<script type="text/javascript">

var socket = io.connect("http://127.0.0.1:5000/aptitude/startAptitude");
console.log("connected");

socket.on("connect", function () {
  socket.emit("my event", {
    data: "User Connected",
  });
  console.log("socket is on");
  var form = $("form").on("submit", function (e) {
    e.preventDefault();
    let user_name = $("input.username").val();
    let user_input = $("input.message").val();
    console.log("form data is", user_name, "  ", user_input);
    socket.emit("my event", {
      user_name: user_name,
      message: user_input,
    });
    console.log("socket emitted");
    $("input.message").val("").focus();
  });
});
socket.on("my response", function (msg) {
  console.log("responese", msg);
  if (typeof msg.user_name !== "undefined") {
    $("h3").remove();
    $("div.message_holder").append(
      '<div><b style="color: #000">' +
        msg.user_name +
        "</b> " +
        msg.message +
        "</div>"
    );
  }
});

</script>
蓝图中的

和routes.py是这样的:

from RecruitmentService import socketio as sc
from RecruitmentService import app
from flask_socketio import SocketIO
from flask_socketio import send, emit
mod = Blueprint('aptitude', __name__ ,template_folder='templates')

@mod.route('/getAptitudeQuestions', methods=['GET'])
def getAptiQuestions():
  questionset,answerset = getQuestions()
  return Response(json.dumps(questionset), status=200, mimetype='application/json')
    
@mod.route('/startAptitude', methods=['GET','POST'])
def startAptitude():
  # return "Hello world"
  print("--------------------------------------------")
  print("socket is,", sc)
  return render_template('session.html')

def messageReceived(methods=['GET', 'POST']):
  print('message was received!!!')

@sc.on('my event')
def handle_my_custom_event(json):
  print("hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii")
  print('received my event: ' + str(json))
  sc.emit('my response', "im here", callback=messageReceived)
  return "yes"

当我 运行 应用程序时,我看到控制台语句为:

connected
startAptitude:30 socket is on
startAptitude:35 form data is sender    chat message
startAptitude:40 socket emitted

但是事件处理程序中给出的打印语句不会在终端中打印出来。我不确定事件是否被触发。

终端输出是这样的:

* Restarting with stat
* Debugger is active!
* Debugger PIN: 120-320-553
(22408) wsgi starting up on http://127.0.0.1:5000
(22408) accepted ('127.0.0.1', 52027)
127.0.0.1 - - [23/Jul/2020 19:14:41] "GET /socket.io/?EIO=3&transport=polling&t=NDxwq3E HTTP/1.1" 200 349 0.001998
127.0.0.1 - - [23/Jul/2020 19:14:41] "POST /socket.io/?EIO=3&transport=polling&t=NDxwq3i&sid=021dfdd91a4e4b22bb4dcfd726465c93 HTTP/1.1" 200 219 0.001026
(22408) accepted ('127.0.0.1', 52030)
127.0.0.1 - - [23/Jul/2020 19:14:42] "GET /socket.io/?EIO=3&transport=polling&t=NDxwq4M&sid=021dfdd91a4e4b22bb4dcfd726465c93 HTTP/1.1" 200 183 0.001000
127.0.0.1 - - [23/Jul/2020 19:14:42] "GET /socket.io/?EIO=3&transport=polling&t=NDxwq4o&sid=021dfdd91a4e4b22bb4dcfd726465c93 HTTP/1.1" 200 183 0.001144
(22408) accepted ('127.0.0.1', 52035)
(22408) accepted ('127.0.0.1', 52036)
--------------------------------------------
socket is, <flask_socketio.SocketIO object at 0x04CC6F90>
127.0.0.1 - - [23/Jul/2020 19:14:47] "GET /aptitude/startAptitude HTTP/1.1" 200 2095 0.022539
127.0.0.1 - - [23/Jul/2020 19:14:47] "GET /socket.io/?EIO=3&transport=websocket&sid=021dfdd91a4e4b22bb4dcfd726465c93 HTTP/1.1" 200 0 5.763522

任何帮助都意义重大。谢谢

当您连接到 Socket.IO 服务器时,您通常不使用 URL,您只需连接到服务器,让系统使用默认端点:

var socket = io.connect("http://127.0.0.1:5000");

这让很多人感到困惑,但是当您添加路径时,Socket.IO 将其视为 namespace,而不是路线。

所以发生的事情是您的客户端正在连接自定义命名空间,但您的服务器正在侦听默认命名空间,因此事件永远不会触发。