博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
websocket 二合一
阅读量:7231 次
发布时间:2019-06-29

本文共 1242 字,大约阅读时间需要 4 分钟。

server

from flask import Flask, request, render_templatefrom geventwebsocket.handler import WebSocketHandlerfrom gevent.pywsgi import WSGIServerfrom geventwebsocket.websocket import WebSocketimport jsonapp = Flask(__name__)user_socket_dict = {}  # 字典@app.route('/conn_ws/
') #http 协议,def ws_app(user_nick): user_socket = request.environ.get('wsgi.websocket') #type:WebSocket user_socket_dict[user_nick] = user_socket # { 昵称:user信息 } print(len(user_socket_dict), list(user_socket_dict.keys())) while True: # user_socket 是一个内存地址 msg = user_socket.receive() # hang 住了 msg_dict = json.loads(msg) to_user = msg_dict.get('to_user') if to_user == 'all': for ss in list(user_socket_dict.values()): ss.send(msg) else: to_user_socket = user_socket_dict.get(to_user) to_user_socket.send(msg)@app.route('/')def index(): return render_template('my_ws.html')if __name__ == '__main__': # app.run() http_ser = WSGIServer(('0.0.0.0',9009),app,handler_class=WebSocketHandler) # 应用程序网关接口 http_ser.serve_forever()

页面 html  

    
单聊

发送给: 消息:

 

转载于:https://www.cnblogs.com/zhangchen-sx/p/10626882.html

你可能感兴趣的文章
iOS企业证书申请介绍
查看>>
hdu 1950 Bridging signals(最长上升子序列)
查看>>
jquery学习收获
查看>>
es6js promise在ie中报错“未定义”
查看>>
思科HSRP和Port-channel配置
查看>>
常用的sql脚本(陆续更新)
查看>>
mongodb的gridfs
查看>>
api图片传输,转成64位字符串进行传输
查看>>
Matlab高斯分布输入的PID控制
查看>>
【Java】自定义异常
查看>>
Ubuntu14.04server开放rootssh登录权限
查看>>
错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
查看>>
Linux 权限基础说明
查看>>
2017级面向对象程序设计寒假作业3
查看>>
迭代器
查看>>
Linux OpenCV 静态链接错误
查看>>
Java多线程&集合类-详细版
查看>>
Flask即插视图与tornado比较
查看>>
springboot笔记(一)
查看>>
学习 - SpringMVC
查看>>