Skip to content

Commit 4822d81

Browse files
reorganization of examples, plus some new ones for the client
1 parent fc5fbcf commit 4822d81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+170
-24
lines changed

examples/README.rst

+3-22

examples/client/README.rst

+15

examples/client/asyncio/README.rst

+24
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import asyncio
2+
import time
3+
import socketio
4+
5+
loop = asyncio.get_event_loop()
6+
sio = socketio.AsyncClient()
7+
start_timer = None
8+
9+
10+
async def send_ping():
11+
global start_timer
12+
start_timer = time.time()
13+
await sio.emit('ping_from_client')
14+
15+
16+
@sio.on('connect')
17+
async def on_connect():
18+
print('connected to server')
19+
await send_ping()
20+
21+
22+
@sio.on('pong_from_server')
23+
async def on_pong(data):
24+
global start_timer
25+
latency = time.time() - start_timer
26+
print('latency is {0:.2f} ms'.format(latency * 1000))
27+
await sio.sleep(1)
28+
await send_ping()
29+
30+
31+
async def start_server():
32+
await sio.connect('http://localhost:5000')
33+
await sio.wait()
34+
35+
36+
if __name__ == '__main__':
37+
loop.run_until_complete(start_server())

examples/client/threads/README.rst

+24
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
import socketio
3+
4+
sio = socketio.Client()
5+
start_timer = None
6+
7+
8+
def send_ping():
9+
global start_timer
10+
start_timer = time.time()
11+
sio.emit('ping_from_client')
12+
13+
14+
@sio.on('connect')
15+
def on_connect():
16+
print('connected to server')
17+
send_ping()
18+
19+
20+
@sio.on('pong_from_server')
21+
def on_pong(data):
22+
global start_timer
23+
latency = time.time() - start_timer
24+
print('latency is {0:.2f} ms'.format(latency * 1000))
25+
sio.sleep(1)
26+
send_ping()
27+
28+
29+
if __name__ == '__main__':
30+
sio.connect('http://localhost:5000')
31+
sio.wait()

examples/server/README.rst

+30
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.

socketio/asyncio_client.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ async def _emit_internal(self, event, data, namespace=None, id=None):
218218
# as a single argument
219219
if isinstance(data, tuple):
220220
data = list(data)
221-
else:
221+
elif data is not None:
222222
data = [data]
223+
else:
224+
data = []
223225
await self._send_packet(packet.Packet(
224226
packet.EVENT, namespace=namespace, data=[event] + data, id=id,
225227
binary=binary))

socketio/client.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,10 @@ def _emit_internal(self, event, data, namespace=None, id=None):
320320
# as a single argument
321321
if isinstance(data, tuple):
322322
data = list(data)
323-
else:
323+
elif data is not None:
324324
data = [data]
325+
else:
326+
data = []
325327
self._send_packet(packet.Packet(packet.EVENT, namespace=namespace,
326328
data=[event] + data, id=id,
327329
binary=binary))

0 commit comments

Comments
 (0)