作为消息代理通过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:包稳定性
=====
安装
===
代码块::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):
@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:包稳定性