Skip to content

Closing of connection when client doesn't answer pings #185

@fschuetz04

Description

@fschuetz04

I successfully created a python 3.5 websocket server with the following code, but I'm now struggling to close the connection after n missed pings. How is this possible?

import asyncio
import websockets
import threading

def keepalive(conn):
    last_pong = time.time()
    def wrapper():
        pong = conn.ping()
        print('ping sent')
        t = threading.Timer(10, wrapper)
        t.start()
    wrapper()

async def handler(conn, path):
    await conn.send('hello')
    keepalive(conn)
    while True:
        data = await conn.recv()
        print('data: ', data)

start_server = websockets.serve(handler, 'localhost', 5000)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions