Skip to content

Commit

Permalink
client examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 14, 2018
1 parent 5923e7d commit 916bd7a
Show file tree
Hide file tree
Showing 50 changed files with 269 additions and 27 deletions.
4 changes: 0 additions & 4 deletions engineio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down
26 changes: 3 additions & 23 deletions examples/README.rst
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions examples/client/README.rst
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 35 additions & 0 deletions examples/client/asyncio/README.rst
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions examples/client/asyncio/latency_client.py
Original file line number Diff line number Diff line change
@@ -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())
45 changes: 45 additions & 0 deletions examples/client/asyncio/simple_client.py
Original file line number Diff line number Diff line change
@@ -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())
35 changes: 35 additions & 0 deletions examples/client/threads/README.rst
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions examples/client/threads/latency_client.py
Original file line number Diff line number Diff line change
@@ -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()
37 changes: 37 additions & 0 deletions examples/client/threads/simple_client.py
Original file line number Diff line number Diff line change
@@ -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()
31 changes: 31 additions & 0 deletions examples/server/README.rst
Original file line number Diff line number Diff line change
@@ -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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 916bd7a

Please sign in to comment.