diff --git a/engineio/asyncio_client.py b/engineio/asyncio_client.py index c3587910..ccff01e1 100644 --- a/engineio/asyncio_client.py +++ b/engineio/asyncio_client.py @@ -430,10 +430,8 @@ async def _write_loop(self): while self.state == 'connected': packets = None try: - print(1) packets = [await asyncio.wait_for(self.queue.get(), self.ping_timeout)] - print(2) except self.queue_empty: self.logger.error('packet queue is empty, aborting') self._reset() @@ -444,9 +442,7 @@ async def _write_loop(self): else: while True: try: - print(3) packets.append(self.queue.get_nowait()) - print(4, packets[-1]) except self.queue_empty: break if packets[-1] is None: diff --git a/examples/README.rst b/examples/README.rst index 54bffea2..06bf2252 100644 --- a/examples/README.rst +++ b/examples/README.rst @@ -1,26 +1,6 @@ Engine.IO Examples ================== -This directory contains several example Engine.IO applications, organized by -directory: - -wsgi ----- - -Examples that are compatible with the WSGI protocol and frameworks. - -aiohttp -------- - -Examples that are compatible with the aiohttp framework for asyncio. - -sanic ------ - -Examples that are compatible with the sanic framework for asyncio. - - -tornado -------- - -Examples that are compatible with the Tornado framework. +This directory contains several example Engine.IO applications. Look in the +`server` directory for Engine.IO servers, and in the `client` directory for +Engine.IO clients. diff --git a/examples/client/README.rst b/examples/client/README.rst new file mode 100644 index 00000000..ca63fcdd --- /dev/null +++ b/examples/client/README.rst @@ -0,0 +1,15 @@ +Engine.IO Client Examples +========================= + +This directory contains several example Engine.IO client applications, +organized by directory: + +threads +------- + +Examples that use standard Python thread concurrency. + +asyncio +------- + +Examples that use Python's `asyncio` package for concurrency. diff --git a/examples/client/asyncio/README.rst b/examples/client/asyncio/README.rst new file mode 100644 index 00000000..d3b68b9f --- /dev/null +++ b/examples/client/asyncio/README.rst @@ -0,0 +1,35 @@ +Engine.IO Threading Examples +============================ + +This directory contains example Engine.IO clients that work with the +`threading` package of the Python standard library. + +simple_client.py +---------------- + +A basic application in which the client sends messages to the server and the +server responds. + +latency_client.py +----------------- + +In this application the client sends *ping* messages to the server, which are +responded by the server with a *pong*. The client measures the time it takes +for each of these exchanges. + +This is an ideal application to measure the performance of the different +asynchronous modes supported by the Engine.IO server. + +Running the Examples +-------------------- + +These examples work with the server examples of the same name. First run one +of the `simple.py` or `latency.py` versions from the `examples/server` +directory. On another terminal, then start the corresponding client with one +of the following commands:: + + $ python simple_client.py + +or:: + + $ python latency_client.py diff --git a/examples/client/asyncio/latency_client.py b/examples/client/asyncio/latency_client.py new file mode 100644 index 00000000..4eb8c83d --- /dev/null +++ b/examples/client/asyncio/latency_client.py @@ -0,0 +1,37 @@ +import asyncio +import time +import engineio + +loop = asyncio.get_event_loop() +eio = engineio.AsyncClient() +start_timer = None + + +async def send_ping(): + global start_timer + start_timer = time.time() + await eio.send('ping') + + +@eio.on('connect') +async def on_connect(): + print('connected to server') + await send_ping() + + +@eio.on('message') +async def on_message(data): + global start_timer + latency = time.time() - start_timer + print('latency is {0:.2f} ms'.format(latency * 1000)) + await eio.sleep(1) + await send_ping() + + +async def start_server(): + await eio.connect('http://localhost:5000') + await eio.wait() + + +if __name__ == '__main__': + loop.run_until_complete(start_server()) diff --git a/examples/client/asyncio/simple_client.py b/examples/client/asyncio/simple_client.py new file mode 100644 index 00000000..13149a96 --- /dev/null +++ b/examples/client/asyncio/simple_client.py @@ -0,0 +1,45 @@ +import asyncio +import signal +import engineio + +loop = asyncio.get_event_loop() +eio = engineio.AsyncClient() +exit_event = asyncio.Event() + + +async def send_hello(): + message = 'Hello from client side!' + while not exit_event.is_set(): + print('sending: ' + 'Hello from client side!') + await eio.send(message) + try: + await asyncio.wait_for(exit_event.wait(), timeout=5) + except asyncio.TimeoutError: + pass + await eio.disconnect() + + +@eio.on('connect') +def on_connect(): + print('connected to server') + eio.start_background_task(send_hello) + + +@eio.on('message') +def on_message(data): + print('received: ' + str(data)) + + +def signal_handler(sig, frame): + exit_event.set() + print('exiting') + + +async def start_server(): + await eio.connect('http://localhost:5000') + await eio.wait() + + +if __name__ == '__main__': + signal.signal(signal.SIGINT, signal_handler) + loop.run_until_complete(start_server()) diff --git a/examples/client/threads/README.rst b/examples/client/threads/README.rst new file mode 100644 index 00000000..d3b68b9f --- /dev/null +++ b/examples/client/threads/README.rst @@ -0,0 +1,35 @@ +Engine.IO Threading Examples +============================ + +This directory contains example Engine.IO clients that work with the +`threading` package of the Python standard library. + +simple_client.py +---------------- + +A basic application in which the client sends messages to the server and the +server responds. + +latency_client.py +----------------- + +In this application the client sends *ping* messages to the server, which are +responded by the server with a *pong*. The client measures the time it takes +for each of these exchanges. + +This is an ideal application to measure the performance of the different +asynchronous modes supported by the Engine.IO server. + +Running the Examples +-------------------- + +These examples work with the server examples of the same name. First run one +of the `simple.py` or `latency.py` versions from the `examples/server` +directory. On another terminal, then start the corresponding client with one +of the following commands:: + + $ python simple_client.py + +or:: + + $ python latency_client.py diff --git a/examples/client/threads/latency_client.py b/examples/client/threads/latency_client.py new file mode 100644 index 00000000..a1649ee6 --- /dev/null +++ b/examples/client/threads/latency_client.py @@ -0,0 +1,31 @@ +import time +import engineio + +eio = engineio.Client() +start_timer = None + + +def send_ping(): + global start_timer + start_timer = time.time() + eio.send('ping') + + +@eio.on('connect') +def on_connect(): + print('connected to server') + send_ping() + + +@eio.on('message') +def on_message(data): + global start_timer + latency = time.time() - start_timer + print('latency is {0:.2f} ms'.format(latency * 1000)) + eio.sleep(1) + send_ping() + + +if __name__ == '__main__': + eio.connect('http://localhost:5000') + eio.wait() diff --git a/examples/client/threads/simple_client.py b/examples/client/threads/simple_client.py new file mode 100644 index 00000000..b95c23af --- /dev/null +++ b/examples/client/threads/simple_client.py @@ -0,0 +1,37 @@ +import signal +import threading +import engineio + +eio = engineio.Client() +exit_event = threading.Event() + + +def send_hello(): + message = 'Hello from client side!' + while not exit_event.is_set(): + print('sending: ' + 'Hello from client side!') + eio.send(message) + exit_event.wait(5) + eio.disconnect() + + +@eio.on('connect') +def on_connect(): + print('connected to server') + eio.start_background_task(send_hello) + + +@eio.on('message') +def on_message(data): + print('received: ' + str(data)) + + +def signal_handler(sig, frame): + exit_event.set() + print('exiting') + + +if __name__ == '__main__': + signal.signal(signal.SIGINT, signal_handler) + eio.connect('http://localhost:5000') + eio.wait() diff --git a/examples/server/README.rst b/examples/server/README.rst new file mode 100644 index 00000000..c553c219 --- /dev/null +++ b/examples/server/README.rst @@ -0,0 +1,31 @@ +Engine.IO Server Examples +========================= + +This directory contains several example Engine.IO server applications, +organized by directory: + +wsgi +---- + +Examples that are compatible with the WSGI specification. + +asgi +---- + +Examples that are compatible with the ASGI specification. + +aiohttp +------- + +Examples that are compatible with the aiohttp framework for asyncio. + +sanic +----- + +Examples that are compatible with the sanic framework for asyncio. + + +tornado +------- + +Examples that are compatible with the Tornado framework. diff --git a/examples/aiohttp/README.rst b/examples/server/aiohttp/README.rst similarity index 100% rename from examples/aiohttp/README.rst rename to examples/server/aiohttp/README.rst diff --git a/examples/aiohttp/latency.html b/examples/server/aiohttp/latency.html similarity index 100% rename from examples/aiohttp/latency.html rename to examples/server/aiohttp/latency.html diff --git a/examples/aiohttp/latency.py b/examples/server/aiohttp/latency.py similarity index 100% rename from examples/aiohttp/latency.py rename to examples/server/aiohttp/latency.py diff --git a/examples/aiohttp/requirements.txt b/examples/server/aiohttp/requirements.txt similarity index 100% rename from examples/aiohttp/requirements.txt rename to examples/server/aiohttp/requirements.txt diff --git a/examples/aiohttp/simple.html b/examples/server/aiohttp/simple.html similarity index 100% rename from examples/aiohttp/simple.html rename to examples/server/aiohttp/simple.html diff --git a/examples/aiohttp/simple.py b/examples/server/aiohttp/simple.py similarity index 100% rename from examples/aiohttp/simple.py rename to examples/server/aiohttp/simple.py diff --git a/examples/aiohttp/static/engine.io.js b/examples/server/aiohttp/static/engine.io.js similarity index 100% rename from examples/aiohttp/static/engine.io.js rename to examples/server/aiohttp/static/engine.io.js diff --git a/examples/aiohttp/static/style.css b/examples/server/aiohttp/static/style.css similarity index 100% rename from examples/aiohttp/static/style.css rename to examples/server/aiohttp/static/style.css diff --git a/examples/asgi/README.rst b/examples/server/asgi/README.rst similarity index 100% rename from examples/asgi/README.rst rename to examples/server/asgi/README.rst diff --git a/examples/asgi/latency.html b/examples/server/asgi/latency.html similarity index 100% rename from examples/asgi/latency.html rename to examples/server/asgi/latency.html diff --git a/examples/asgi/latency.py b/examples/server/asgi/latency.py similarity index 100% rename from examples/asgi/latency.py rename to examples/server/asgi/latency.py diff --git a/examples/asgi/requirements.txt b/examples/server/asgi/requirements.txt similarity index 100% rename from examples/asgi/requirements.txt rename to examples/server/asgi/requirements.txt diff --git a/examples/asgi/simple.html b/examples/server/asgi/simple.html similarity index 100% rename from examples/asgi/simple.html rename to examples/server/asgi/simple.html diff --git a/examples/asgi/simple.py b/examples/server/asgi/simple.py similarity index 100% rename from examples/asgi/simple.py rename to examples/server/asgi/simple.py diff --git a/examples/asgi/static/engine.io.js b/examples/server/asgi/static/engine.io.js similarity index 100% rename from examples/asgi/static/engine.io.js rename to examples/server/asgi/static/engine.io.js diff --git a/examples/asgi/static/style.css b/examples/server/asgi/static/style.css similarity index 100% rename from examples/asgi/static/style.css rename to examples/server/asgi/static/style.css diff --git a/examples/sanic/README.rst b/examples/server/sanic/README.rst similarity index 100% rename from examples/sanic/README.rst rename to examples/server/sanic/README.rst diff --git a/examples/sanic/latency.html b/examples/server/sanic/latency.html similarity index 100% rename from examples/sanic/latency.html rename to examples/server/sanic/latency.html diff --git a/examples/sanic/latency.py b/examples/server/sanic/latency.py similarity index 100% rename from examples/sanic/latency.py rename to examples/server/sanic/latency.py diff --git a/examples/sanic/requirements.txt b/examples/server/sanic/requirements.txt similarity index 100% rename from examples/sanic/requirements.txt rename to examples/server/sanic/requirements.txt diff --git a/examples/sanic/simple.html b/examples/server/sanic/simple.html similarity index 100% rename from examples/sanic/simple.html rename to examples/server/sanic/simple.html diff --git a/examples/sanic/simple.py b/examples/server/sanic/simple.py similarity index 100% rename from examples/sanic/simple.py rename to examples/server/sanic/simple.py diff --git a/examples/sanic/static/engine.io.js b/examples/server/sanic/static/engine.io.js similarity index 100% rename from examples/sanic/static/engine.io.js rename to examples/server/sanic/static/engine.io.js diff --git a/examples/sanic/static/style.css b/examples/server/sanic/static/style.css similarity index 100% rename from examples/sanic/static/style.css rename to examples/server/sanic/static/style.css diff --git a/examples/tornado/README.rst b/examples/server/tornado/README.rst similarity index 100% rename from examples/tornado/README.rst rename to examples/server/tornado/README.rst diff --git a/examples/tornado/latency.py b/examples/server/tornado/latency.py similarity index 100% rename from examples/tornado/latency.py rename to examples/server/tornado/latency.py diff --git a/examples/tornado/requirements.txt b/examples/server/tornado/requirements.txt similarity index 100% rename from examples/tornado/requirements.txt rename to examples/server/tornado/requirements.txt diff --git a/examples/tornado/simple.py b/examples/server/tornado/simple.py similarity index 100% rename from examples/tornado/simple.py rename to examples/server/tornado/simple.py diff --git a/examples/tornado/static/engine.io.js b/examples/server/tornado/static/engine.io.js similarity index 100% rename from examples/tornado/static/engine.io.js rename to examples/server/tornado/static/engine.io.js diff --git a/examples/tornado/static/style.css b/examples/server/tornado/static/style.css similarity index 100% rename from examples/tornado/static/style.css rename to examples/server/tornado/static/style.css diff --git a/examples/tornado/templates/latency.html b/examples/server/tornado/templates/latency.html similarity index 100% rename from examples/tornado/templates/latency.html rename to examples/server/tornado/templates/latency.html diff --git a/examples/tornado/templates/simple.html b/examples/server/tornado/templates/simple.html similarity index 100% rename from examples/tornado/templates/simple.html rename to examples/server/tornado/templates/simple.html diff --git a/examples/wsgi/README.rst b/examples/server/wsgi/README.rst similarity index 100% rename from examples/wsgi/README.rst rename to examples/server/wsgi/README.rst diff --git a/examples/wsgi/latency.py b/examples/server/wsgi/latency.py similarity index 100% rename from examples/wsgi/latency.py rename to examples/server/wsgi/latency.py diff --git a/examples/wsgi/requirements.txt b/examples/server/wsgi/requirements.txt similarity index 100% rename from examples/wsgi/requirements.txt rename to examples/server/wsgi/requirements.txt diff --git a/examples/wsgi/simple.py b/examples/server/wsgi/simple.py similarity index 100% rename from examples/wsgi/simple.py rename to examples/server/wsgi/simple.py diff --git a/examples/wsgi/static/engine.io.js b/examples/server/wsgi/static/engine.io.js similarity index 100% rename from examples/wsgi/static/engine.io.js rename to examples/server/wsgi/static/engine.io.js diff --git a/examples/wsgi/static/style.css b/examples/server/wsgi/static/style.css similarity index 100% rename from examples/wsgi/static/style.css rename to examples/server/wsgi/static/style.css diff --git a/examples/wsgi/templates/latency.html b/examples/server/wsgi/templates/latency.html similarity index 100% rename from examples/wsgi/templates/latency.html rename to examples/server/wsgi/templates/latency.html diff --git a/examples/wsgi/templates/simple.html b/examples/server/wsgi/templates/simple.html similarity index 100% rename from examples/wsgi/templates/simple.html rename to examples/server/wsgi/templates/simple.html