Skip to content

Commit 798ab6a

Browse files
v5 protocol: add JavaScript client and server examples for testing
1 parent 4940fc1 commit 798ab6a

20 files changed

Lines changed: 3598 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ venv*
4343
tags
4444
htmlcov
4545
*.swp
46+
47+
node_modules

examples/client/README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ asyncio
1313
-------
1414

1515
Examples that use Python's `asyncio` package for concurrency.
16+
17+
javascript
18+
----------
19+
20+
Examples that use the JavaScript version of Socket.IO for compatiblity testing.

examples/client/asyncio/latency_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ async def pong_from_server():
2525
latency = time.time() - start_timer
2626
print('latency is {0:.2f} ms'.format(latency * 1000))
2727
await sio.sleep(1)
28-
await send_ping()
28+
if sio.connected:
29+
await send_ping()
2930

3031

3132
async def start_server():
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# Socket.IO Fiddle
3+
4+
```
5+
$ npm install
6+
$ node fiddle-client.js # to run the fiddle example
7+
$ node latency-client.js # to run the latency example
8+
```
9+
10+
Optionally, specify a port by supplying the `PORT` env variable.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const io = require('socket.io-client')
2+
const port = process.env.PORT || 5000;
3+
4+
const socket = io('http://localhost:' + port);
5+
6+
socket.on('connect', () => {
7+
console.log(`connect ${socket.id}`);
8+
});
9+
10+
socket.on('disconnect', () => {
11+
console.log(`disconnect ${socket.id}`);
12+
});
13+
14+
socket.on('hello', (a, b, c) => {
15+
console.log(a, b, c);
16+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const io = require('socket.io-client')
2+
const port = process.env.PORT || 5000;
3+
4+
const socket = io('http://localhost:' + port);
5+
let last;
6+
function send () {
7+
last = new Date();
8+
socket.emit('ping_from_client');
9+
}
10+
11+
socket.on('connect', () => {
12+
console.log(`connect ${socket.id}`);
13+
send();
14+
});
15+
16+
socket.on('disconnect', () => {
17+
console.log(`disconnect ${socket.id}`);
18+
});
19+
20+
socket.on('pong_from_server', () => {
21+
const latency = new Date() - last;
22+
console.log('latency is ' + latency + ' ms');
23+
setTimeout(send, 1000);
24+
});

0 commit comments

Comments
 (0)