群聊

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" id="send_str">
<button id="send_btn" οnclick="send()"> 发送消息</button>
<p>
<div id="chat_list">

</div>
</p>
</body>
<script type="application/javascript">
    var ws=new WebSocket("ws://192.168.11.78:9527/conn_ws");   // 自己的ip地址,页面才往自己页面显示内容
    ws.onmessage =function (messageEvent) {
        console.log(messageEvent.data);
        var ptag = document.createElement("p");
        ptag.innerText =messageEvent.data;
        document.getElementById("chat_list").appendChild(ptag);
    };
    function send() {
        var send_str = document.getElementById("send_str").value;
        ws.send(send_str);
    }

</script>

</html>

 

from flask import Flask,request,render_template

from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from geventwebsocket.websocket import WebSocket

app = Flask(__name__)
user_socket_list=[]

@app.route("/conn_ws")
def ws_app():
    # print(request.environ)
    """
    wsgi.websocket:<geventwebsocket.websocket.WebSocket object at 0x0000000003Bc8528>
    :return:
    """
    user_socket = request.environ.get("wsgi.websocket")
    user_socket_list.append(user_socket)
    while True:

        msg =user_socket.receive()
        print(msg)
        for usocket in user_socket_list:
            usocket.send(msg)
            #user_socket.send(msg)

    #1 已经开启的websocket连接
    #3 开启了websocket连接 然后 断开了


    # return "123"
@app.route("/")
def index():
    return  render_template("my_ws.html")

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

  my_ws.htm

l

转载于:https://www.cnblogs.com/liurenli/p/10590253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值