flask socketio:实例:1 ChatRoom

原理图:

flask socketio:实例:1 ChatRoom_第1张图片

参考代码:

chatserver.py

# 实现一个网页聊天器

from flask import Flask
from flask_socketio import SocketIO, send

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
app.config['DEBUG'] = True
socketio = SocketIO(app, cors_allowed_origins="*")

# 监听message event
@socketio.on("message")
def handleMessage(msg):
    print("Message: " + msg)
    send(msg, broadcast=True) # broadcast=True:广播 服务器收到某个客户发来的data,并把该data转发给所有连接到该服务器的客户

if __name__ == "__main__":
    socketio.run(app)

chatpage.html

 
 

你可能感兴趣的:(Flask,flask,socketio,chatroom)