Mychat-Flask-SocketIO简易聊天室

发布时间:2023-05-25 付费文章:9.9元

Base

Github加速

 
点此查看

Civil

土木分类资料

 
点此查看

Python

Python编程学习

 
点此查看

Games

JS前端编程学习

 
点此查看

方案一:

 

 

 

 

核心代码:

from flask import Flask, render_template
from flask_socketio import SocketIO
from werkzeug import debug
import os

import gevent

app = Flask(__name__)

app.secret_key = os.environ.get("SECRET_KEY")

socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')


@socketio.on('message')
def handle_message(data):
    print('received message: ' + data)
    socketio.emit('updateui', data)

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

方案二:

 

 

 

 

核心代码:

@socketio.on("message")
def message(data):
    message = data["message"]
    username = data["username"]
    timestamp = time.strftime('%b-%d %I:%M%p', time.localtime())
    channel = data["channel"]
    msg = [channel.strip(), username, message, timestamp]
    # print(msg)
    if len(messageList) < 200:
        messageList.append(msg)
    else:
        messageList.append(msg)
        messageList.remove(messageList[0])
    emit("show message", {"message": message, "username": username, 
                "timestamp": timestamp, "channel": channel}, broadcast=True)

如忘记保存,或后续再查看,可凭“订单号” 手动获取