websocket.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:django-notifs 作者: danidee10 项目源码 文件源码
def application(env, start_response):
    """Setup the Websocket Server and read messages off the queue."""
    connection = pika.BlockingConnection(
        pika.connection.URLParameters(RABBIT_MQ_URL)
    )
    channel = connection.channel()

    queue = env['PATH_INFO'].replace('/', '')

    channel.queue_declare(queue=queue)

    uwsgi.websocket_handshake(
        env['HTTP_SEC_WEBSOCKET_KEY'],
        env.get('HTTP_ORIGIN', '')
    )

    def keepalive():
        """Keep the websocket connection alive (called each minute)."""
        print('PING/PONG...')
        try:
            uwsgi.websocket_recv_nb()
            connection.add_timeout(60, keepalive)
        except OSError as error:
            print('Killing the connection...')
            sys.exit(0)

    def callback(ch, method, properties, body):
        """Callback called when a message has been received."""
        try:
            uwsgi.websocket_send(body)
        except OSError as error:
            print('Could not send message over the websocket', error)
            sys.exit(0)

    keepalive()
    channel.basic_consume(callback, queue=queue, no_ack=True)
    channel.start_consuming()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号