Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic usage instructions #96

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# wampproto-python
Sans-IO implementation of the WAMP protocol in Python

This is a library that could be used to build a [WAMP](https://wamp-proto.org/index.html) client or server. It operates
on strings and bytes. A WAMP client can be split into two parts.

* Session establishment
* Session management.

Assuming we have a websocket connection established to a WAMP server using sub protocol `wamp.2.json`. The first
"message" is going to be sent by the client as HELLO
```python
from wampproto.joiner import Joiner

j = Joiner("realm1")
# this doesn't actually send the message, it just returns the
# payload that needs to be sent over websocket.
hello = j.send_hello()

ws.send_str(hello)
response = ws.receive_str()

to_send = j.receive(response)
if to_send is None:
# if there is nothing to send, this means
# the server returned WELCOME.
print(j.get_session_details())
```

Now that the session is established, we could instantiate a `WAMPSession` instance
to handle further messages.
```python
from wampproto.session import WAMPSession
from wampproto.messages import Call
from wampproto.idgen import SessionScopeIDGenerator

session = WAMPSession()
idgen = SessionScopeIDGenerator()

call = Call(idgen.next(), "foo.bar")
to_send = session.send_message(call)

ws.send_str(to_send)

incoming_payload = ws.receive_str()
result = session.receive(incoming_payload)
print(result)
```
Loading