Skip to content

Commit

Permalink
add: raw connection example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash authored Dec 7, 2024
1 parent 7cae290 commit f7e3b27
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,44 @@ pip install kcp
```

## Examples
### Just the raw connection
While kcp.py features a diverse set of pre-implemented uses of KCP (see below), it also allows you to directly manage your KCP connections.
Here is an example using two independent connections locally.
```py
from kcp import KCP

# Create two connections using the same conversation ID.
kcp1 = KCP(
conv_id=1,
)

kcp2 = KCP(
conv_id=1,
)

# Update their timing information.
kcp1.update()
kcp2.update()


# Set each connection to send data to the other one (usually this would go through some network layer, but
# for the purpose of the example we do this).
@kcp1.outbound_handler
def send_kcp1(_, data: bytes) -> None:
kcp2.receive(data)


@kcp2.outbound_handler
def send_kcp2(_, data: bytes) -> None:
kcp1.receive(data)

# Enqueue data to be sent and send it off.
kcp1.enqueue(b"Hello, world!")
kcp1.flush()

print(kcp2.get_received()) # b"Hello, world!"
```

### Asynchronous Server
kcp.py features an implementation of an asynchronous server using the event loop protocol API.
```py
Expand Down

0 comments on commit f7e3b27

Please sign in to comment.