简体   繁体   中英

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. But I'm not sure what is not working properly.

Here is my server code

server.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
  • Debugger is active!
  • Debugger 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. The client connects but instantly disconnects. I tried with Firefox websocket client too. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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