-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from xconnio/docs
add basic usage instructions
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |