Skip to content

Commit

Permalink
Merge f06a00d into a808e3f
Browse files Browse the repository at this point in the history
  • Loading branch information
alvassin committed May 8, 2019
2 parents a808e3f + f06a00d commit 6dc9335
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
114 changes: 58 additions & 56 deletions README.rst
Expand Up @@ -60,45 +60,56 @@ Add the backend side

.. code-block:: python
import logging
from time import time
## If you want write async tornado code import it
# from from wsrpc import WebSocketRoute, WebSocket, wsrpc_static
## else you should use thread-base handler
from wsrpc import WebSocketRoute, WebSocketThreaded as WebSocket, wsrpc_static
tornado.web.Application((
# js static files will available as "/js/wsrpc.min.js".
wsrpc_static(r'/js/(.*)'),
# WebSocket handler. Client will connect here.
(r"/ws/", WebSocket),
# Serve other static files
(r'/(.*)', tornado.web.StaticFileHandler, {
'path': os.path.join(project_root, 'static'),
'default_filename': 'index.html'
}),
))
# This class should be call by client.
# Connection object will be have the instance of this class when will call route-alias.
import aiohttp.web
from wsrpc_aiohttp import WebSocketAsync, STATIC_DIR, WebSocketRoute
log = logging.getLogger(__name__)
# This class can be called by client.
# Connection object will have this class instance after calling route-alias.
class TestRoute(WebSocketRoute):
# This method will be executed when client will call route-alias first time.
# This method will be executed when client calls route-alias
# for the first time.
def init(self, **kwargs):
# the python __init__ must be return "self". This method might return anything.
# Python __init__ must be return "self".
# This method might return anything.
return kwargs
def getEpoch(self):
# this method named by camelCase because the client can call it.
# This method named by camelCase because the client can call it.
async def getEpoch(self):
# You can execute functions on the client side
await self.do_notify()
return time()
# stateful request
# this is the route alias TestRoute as "test1"
WebSocket.ROUTES['test1'] = TestRoute
# This method calls function on the client side
async def do_notify(self):
awesome = 'Somebody executed test1.getEpoch method!'
await self.socket.call('notify', result=awesome)
app = aiohttp.web.Application()
app.router.add_route("*", "/ws/", WebSocketAsync) # Websocket route
app.router.add_static('/js', STATIC_DIR) # WSRPC js library
app.router.add_static('/', ".") # Your static files
# Stateful request
# This is the route alias TestRoute as "test1"
WebSocketAsync.add_route('test1', TestRoute)
# stateless request
WebSocket.ROUTES['test2'] = lambda *a, **kw: True
# Stateless request
WebSocketAsync.add_route('test2', lambda *a, **kw: True)
# initialize ThreadPool. Needed when using WebSocketThreaded.
WebSocket.init_pool()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
aiohttp.web.run_app(app, port=8000)
Expand All @@ -107,43 +118,34 @@ Add the frontend side

.. code-block:: HTML

<script type="text/javascript" src="/js/q.min.js"></script>
<script type="text/javascript" src="/js/wsrpc.min.js"></script>
<script>
var url = ((window.location.protocol==="https):"?"wss://":"ws://") + window.location.host + '/ws/';
RPC = WSRPC(url, 5000);
RPC.addRoute('test', function (data) { return "Test called"; });
var url = (window.location.protocol==="https):"?"wss://":"ws://") + window.location.host + '/ws/';
RPC = new WSRPC(url, 8000);
// Configure client API, that can be called from server
RPC.addRoute('notify', function (data) {
console.log('Server called client route "notify":', data);
return data.result;
});
RPC.connect();
// Call stateful route
// After you call that route, server would execute 'notify' route on the
// client, that is registered above.
RPC.call('test1.getEpoch').then(function (data) {
console.log(data);
console.log('Result for calling server route "test1.getEpoch": ', data);
}, function (error) {
alert(error);
}).done();
});
RPC.call('test2').then(function (data) { console.log(data); }).done();
// Call stateless method
RPC.call('test2').then(function (data) {
console.log('Result for calling server route "test2"', data);
});
</script>

Reverse call from Server to Client
----------------------------------
backend:
.. code-block:: python
def do_notify(self):
awesome = 'Notification for you!'
yield self.socket.call('notify', result=awesome)
frontend:

.. code-block:: HTML
<script>
var url = (window.location.protocol==="https:"?"wss://":"ws://") + window.location.host + '/ws/';
RPC = WSRPC(url, 5000);
RPC.addRoute('notify', function (data) { return data.result; });
RPC.connect();
</script>

Documentation
+++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion docs/source/01-introduction.rst
Expand Up @@ -100,7 +100,7 @@ Javascript client code:

.. code-block:: javascript
RPC = WSRPC("ws://127.0.0.1:8000/ws/");
RPC = new WSRPC("ws://127.0.0.1:8000/ws/");
RPC.connect();
RPC.call('kw.set', {'key': 'foo', 'value': 'bar'}).then(function (result) {
Expand Down

0 comments on commit 6dc9335

Please sign in to comment.