作为消息代理通过redis与websocket通信

websocket_redis的Python项目详细描述


websocket_redis是一个异步python模块,它收集两个项目websockets<;https://github.com/aaugustin/websockets>;和aioredis<;https://github.com/aio libs/aioredis>;`为了使客户端和后端之间的通信尽可能简单,而不会丢失客户端发送的任何消息。


=====
安装
===

代码块::bash

$pip安装websocket\u redis


手动安装:
代码块::bash

#运行测试(可选)
$pip install nose
$make test

`pypi<;https://pypi.python.org/pypi/websocket_redis>;`.


使用示例
----

重写wshandler方法:

。代码::python

import asyncio
from websocket_redis.server.ws_server import wsserver
from websocket_redis.server.ws_handler import wshandler




@asyncio.coroutine
已关闭,客户端:{}。格式(
self.session\u id,self.client))

@asyncio.coroutine
def on_error(self,e):

e,self.client))

@asyncio.coroutine
def on_message(self,文本):

print("已从客户端收到消息{}"。格式(
文本,self.client))

@asyncio.coroutine
发送时的def(self,文本):

print("消息{}"将发送到{}"。格式(
文本,self.client))

服务器:

…代码::python

ws_connection=dict(
host="127.0.0.1",
port=5678)


redis_connection=dict(
address=("localhost",6379)


loop=asyncio.get_event_loop()
server=wsserver(
ws_connection=ws_connection,
redis_connection=redis_connection,
app_name="test_app",
ws_handler_class=mywshandler



尝试:
循环。运行_直到完成(server.run())
循环。永远运行_()
键盘中断除外:
server.close()
loop.close()

async api:

代码::python

import asyncio

消息:

print("new message{}"。format(message.text))
yield from message.reply("hi!")

redis_connection=dict(
address=("localhost",6379)

handler=myapiclientlistener(redis_connection,app_name="test_app")
loop=asyncio.get_event_loop()

loop.run直到完成(handler.run())

线程api:

代码::python

from websocket_redis.api.threading import apilientlistener






redis_connection=dict(
主机="本地主机",
端口=6379

处理程序=myapiclientlistener(redis_connection,app_name="test_app")

handler.run()

……|构建图像::https://travis-ci.org/abdeljelil/websocket_redis.svg?branch=master
:目标:https://travis ci.org/abdeljelil/websocket_redis
:alt:travis ci build
…| cversion image::https://img.shields.io/pypi/v/websocket_redis.svg
:target:https://pypi.python.org/pypi/websocket_redis
:alt:python版本支持
。|版本image::https://img.shields.io/pypi/pyversions/websocket_redis.svg
:目标:https://pypi.python.org/pypi/websocket_redis
:alt:支持的python版本
。| codecov图像::https://codecov.io/github/abdeljelil/websocket_redis/websocket_redis.svg?branch=master
:目标:http://codecov.io/github/abdeljelil/websocket_redis?branch=master
:alt:覆盖范围!<…|状态图像::https://img.shields.io/pypi/status/websocket_redis.svg
:目标:https://pypi.python.org/pypi/websocket_redis
:alt:包稳定性

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java允许具有不同父类的类扩展类,而无需多重继承   java如何创建动态化的JScrollPane w/JPanel作为客户端?   java如何组织和命名包   在Java中读取属性文件   java无法解释的Android意图行为   在Java中动态执行多个BPEL文件的部署   ssl Java 6 SNI(服务器名称指示)?   java我们可以使用Robot框架自动化web和移动应用程序来执行并行执行   java for star pettern的循环   java为什么BinaryReader在线程中,从netty读取错误的数据包?   在java中将华氏度转换为摄氏度   使用Spark和java处理空值和引号编写CSV文件   Java中已排序日期到块的列表   visual studio代码VSCode Java不是linting或自动完成局部变量,而是自动完成Java快捷方式,如“sysout”