Flask实现群聊

Posted pythonz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask实现群聊相关的知识,希望对你有一定的参考价值。

后端

from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from geventwebsocket.websocket import WebSocket
from flask import Flask,request,render_template
user_socket_list = []
app = Flask(__name__)

@app.route("/conn_ws")
#http协议{‘GATEWAY_INTERFACE‘: ‘CGI/1.1‘, ‘SERVER_SOFTWARE‘: ‘gevent/1.3 Python/3.7‘, ‘SCRIPT_NAME‘: ‘‘, ‘wsgi.version‘: (1, 0), ‘wsgi.multithread‘: False, ‘wsgi.multiprocess‘: False, ‘wsgi.run_once‘: False, ‘wsgi.url_scheme‘: ‘http‘, ‘wsgi.errors‘: <
#socket协议{‘GATEWAY_INTERFACE‘: ‘CGI/1.1‘, ‘SERVER_SOFTWARE‘: ‘gevent/1.3 Python/3.7‘, ‘SCRIPT_NAME‘: ‘‘, ‘wsgi.version‘: (1, 0), ‘wsgi.multithread‘: False, ‘wsgi.multiprocess‘: False, ‘wsgi.run_once‘: False, ‘wsgi.url_scheme‘: ‘http‘, ‘wsgi.errors‘: <_io.TextIOWrapper name=‘<stderr>‘ mode=‘w‘ encoding=‘UTF-8‘>, ‘SERVER_NAME‘: ‘6FBK8EBBBDZ159K‘, ‘SERVER_PORT‘: ‘9527‘, ‘REQUEST_METHOD‘: ‘GET‘, ‘PATH_INFO‘: ‘/conn_ws‘, ‘QUERY_STRING‘: ‘‘, ‘SERVER_PROTOCOL‘: ‘HTTP/1.1‘, ‘REMOTE_ADDR‘: ‘127.0.0.1‘, ‘REMOTE_PORT‘: ‘62890‘, ‘HTTP_HOST‘: ‘127.0.0.1:9527‘, ‘HTTP_CONNECTION‘: ‘keep-alive‘, ‘HTTP_CACHE_CONTROL‘: ‘max-age=0‘, ‘HTTP_UPGRADE_INSECURE_REQUESTS‘: ‘1‘, ‘HTTP_USER_AGENT‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/70.0.3538.67 Safari/537.36‘, ‘HTTP_ACCEPT‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8‘, ‘HTTP_ACCEPT_ENCODING‘: ‘gzip, deflate, br‘, ‘HTTP_ACCEPT_LANGUAGE‘: ‘zh-CN,zh;q=0.9‘, ‘HTTP_COOKIE‘: ‘session=bb1d56c2-aaa0-4188-aa1b-10920775eb49‘, ‘wsgi.input‘: <gevent.pywsgi.Input object at 0x0000000003A55948>, ‘werkzeug.request‘: <Request ‘http://127.0.0.1:9527/conn_ws‘ [GET]>}
def ws_app():
    user_socket = request.environ.get("wsgi.websocket")
    user_socket_list.append(user_socket)
    print(len(user_socket_list),user_socket_list)
    while True:
        msg = user_socket.receive()
        print(msg)
        for usocket in user_socket_list:
            usocket.send(msg)

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

if __name__ == __main__:
    http_serv=WSGIServer(("0.0.0.0",9527),app,handler_class=WebSocketHandler)
    http_serv.serve_forever()

后端:

<body>
<input type="text" id="send_str">
<button id="send_btn" onclick="send()">发送消息</button>
<p>
<div id="chat_list">
</div>
</p>
</body>
<script type="application/javascript">
    var ws = new WebSocket("ws://192.168.11.135:9527/conn_ws")
    ws.onmessage = function (messageEvent) {
        console.log(messageEvent.data);
        var ptag = document.createElement("p");
        ptag.innerText = messageEvent.data;
        document.getElementById("chat_list").appendChild(ptag);
    }
    {#document.getElementById("send_btn").addEventListener("onclick")#}
   function send() {
        var send_str = document.getElementById("send_str").value;
        ws.send(send_str)
    }
</script>

 

以上是关于Flask实现群聊的主要内容,如果未能解决你的问题,请参考以下文章

flask+seq2seq实现聊天机器人在线群聊

基于flask 写的web_socket 单聊和群聊

websocket实现简单的群聊

websocket实现群聊和单聊(转)

python-flask复习——- flask请求上下文源码解读http聊天室单聊/群聊(基于gevent-websocket)

Flask 编写http接口api及接口自动化测试