Skip to content

Commit

Permalink
reorganization of examples, plus some new ones for the client
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 16, 2018
1 parent fc5fbcf commit 4822d81
Show file tree
Hide file tree
Showing 62 changed files with 170 additions and 24 deletions.
25 changes: 3 additions & 22 deletions examples/README.rst
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
Socket.IO Examples
==================

This directory contains several example Socket.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 Socket.IO applications. Look in the
`server` directory for Socket.IO servers, and in the `client` directory for
Socket.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 @@
Socket.IO Client Examples
=========================

This directory contains several example Socket.IO client applications,
organized by directory:

threads
-------

Examples that use standard Python thread concurrency.

asyncio
-------

Examples that use Python's `asyncio` package for concurrency.
24 changes: 24 additions & 0 deletions examples/client/asyncio/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Socket.IO Threading Examples
============================

This directory contains example Socket.IO clients that work with the
`threading` package of the Python standard library.

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 Socket.IO server.

Running the Examples
--------------------

These examples work with the server examples of the same name. First run one
of the `latency.py` versions from the `examples/server/wsgi` directory. On
another terminal, then start the corresponding client::

$ 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 socketio

loop = asyncio.get_event_loop()
sio = socketio.AsyncClient()
start_timer = None


async def send_ping():
global start_timer
start_timer = time.time()
await sio.emit('ping_from_client')


@sio.on('connect')
async def on_connect():
print('connected to server')
await send_ping()


@sio.on('pong_from_server')
async def on_pong(data):
global start_timer
latency = time.time() - start_timer
print('latency is {0:.2f} ms'.format(latency * 1000))
await sio.sleep(1)
await send_ping()


async def start_server():
await sio.connect('http://localhost:5000')
await sio.wait()


if __name__ == '__main__':
loop.run_until_complete(start_server())
24 changes: 24 additions & 0 deletions examples/client/threads/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Socket.IO Threading Examples
============================

This directory contains example Socket.IO clients that work with the
`threading` package of the Python standard library.

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 Socket.IO server.

Running the Examples
--------------------

These examples work with the server examples of the same name. First run one
of the `latency.py` versions from the `examples/server/wsgi` directory. On
another terminal, then start the corresponding client::

$ 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 socketio

sio = socketio.Client()
start_timer = None


def send_ping():
global start_timer
start_timer = time.time()
sio.emit('ping_from_client')


@sio.on('connect')
def on_connect():
print('connected to server')
send_ping()


@sio.on('pong_from_server')
def on_pong(data):
global start_timer
latency = time.time() - start_timer
print('latency is {0:.2f} ms'.format(latency * 1000))
sio.sleep(1)
send_ping()


if __name__ == '__main__':
sio.connect('http://localhost:5000')
sio.wait()
30 changes: 30 additions & 0 deletions examples/server/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Socket.IO Server Examples
=========================

This directory contains several example Socket.IO applications, organized by
directory:

wsgi
----

Examples that are compatible with the WSGI protocol and frameworks.

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.
4 changes: 3 additions & 1 deletion socketio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ async def _emit_internal(self, event, data, namespace=None, id=None):
# as a single argument
if isinstance(data, tuple):
data = list(data)
else:
elif data is not None:
data = [data]
else:
data = []
await self._send_packet(packet.Packet(
packet.EVENT, namespace=namespace, data=[event] + data, id=id,
binary=binary))
Expand Down
4 changes: 3 additions & 1 deletion socketio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,10 @@ def _emit_internal(self, event, data, namespace=None, id=None):
# as a single argument
if isinstance(data, tuple):
data = list(data)
else:
elif data is not None:
data = [data]
else:
data = []
self._send_packet(packet.Packet(packet.EVENT, namespace=namespace,
data=[event] + data, id=id,
binary=binary))
Expand Down

0 comments on commit 4822d81

Please sign in to comment.