FlaskSocketio:无法在事件处理程序中打印

2024-09-27 00:20:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建聊天应用程序。我用的是烧瓶SocketIO和烧瓶蓝图

我的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

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


Tags: nameiohttpmessageinputgetison
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:42

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

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

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

因此,发生的情况是,您的客户机正在连接自定义名称空间,而您的服务器正在侦听默认名称空间,因此事件永远不会触发

相关问题 更多 >

    热门问题