简体   繁体   English

无法建立基本的简单flask SocketIO websocket服务器

[英]Unable to establish a basic simple flask SocketIO websocket server

I am trying to host a simple websocket using Flask SocketIO with nothing but client disconnect and client connect command.我正在尝试使用 Flask SocketIO 托管一个简单的 websocket ,除了客户端断开连接和客户端连接命令。 But I'm not sure what is not working properly.但我不确定什么工作不正常。

Here is my server code这是我的服务器代码

server.py服务器.py

from flask import Flask
from flask_socketio import  SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'very-secret'
socketio = SocketIO(app)


@socketio.on('connect')
def client_connect():
    print("HII")
    emit({'data': 'Hii'})

@socketio.on('disconnect')
def client_disconnect():
    print('client disconnected')

@socketio.on('message')
def message(msg):
    print(msg)
    emit({'data':'lol'})


if __name__ == '__main__':
    app.debug = True
    socketio.run(app, port=2345)

When I start the server it says当我启动服务器时,它说

  • Restarting with stat用 stat 重启
  • Debugger is active!调试器处于活动状态!
  • Debugger PIN: 774-937-680调试器 PIN:774-937-680

But doesn't say "Running on *********" message.但不会说“在 ********* 上运行”消息。 I am unable to find out what is the problem.我无法找出问题所在。

For client, I'm using some Chrome websocket client.对于客户端,我正在使用一些 Chrome websocket 客户端。 The client connects but instantly disconnects.客户端连接但立即断开连接。 I tried with Firefox websocket client too.我也试过 Firefox websocket 客户端。 Same thing with that.与此相同。

Thank you in advance.先感谢您。

the syntax for emit is发出的语法是

emit(event,*args, **kwargs)

you haven't mentioned the event on which you want to emit the data.您还没有提到要发出数据的事件。 Try this might work试试这个可能有用

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

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