Javascript syntax highlighting for documentation (#636)

This commit is contained in:
Steindór 2017-05-19 21:49:14 +00:00 committed by Andrew Godwin
parent 50f0b98d43
commit 4c2b8da463
2 changed files with 24 additions and 8 deletions

View File

@ -132,7 +132,9 @@ Let's test it! Run ``runserver``, open a browser, navigate to a page on the serv
(you can't use any page's console because of origin restrictions), and put the (you can't use any page's console because of origin restrictions), and put the
following into the JavaScript console to open a WebSocket and send some data following into the JavaScript console to open a WebSocket and send some data
down it (you might need to change the socket address if you're using a down it (you might need to change the socket address if you're using a
development VM or similar):: development VM or similar)
.. code-block:: javascript
// Note that the path doesn't matter for routing; any WebSocket // Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers // connection gets bumped over to WebSocket consumers
@ -246,7 +248,9 @@ views.
With all that code, you now have a working set of a logic for a chat server. With all that code, you now have a working set of a logic for a chat server.
Test time! Run ``runserver``, open a browser and use that same JavaScript Test time! Run ``runserver``, open a browser and use that same JavaScript
code in the developer console as before:: code in the developer console as before
.. code-block:: javascript
// Note that the path doesn't matter right now; any WebSocket // Note that the path doesn't matter right now; any WebSocket
// connection gets bumped over to WebSocket consumers // connection gets bumped over to WebSocket consumers
@ -508,7 +512,9 @@ chat to people with the same first letter of their username::
If you're just using ``runserver`` (and so Daphne), you can just connect If you're just using ``runserver`` (and so Daphne), you can just connect
and your cookies should transfer your auth over. If you were running WebSockets and your cookies should transfer your auth over. If you were running WebSockets
on a separate domain, you'd have to remember to provide the on a separate domain, you'd have to remember to provide the
Django session ID as part of the URL, like this:: Django session ID as part of the URL, like this
.. code-block:: javascript
socket = new WebSocket("ws://127.0.0.1:9000/?session_key=abcdefg"); socket = new WebSocket("ws://127.0.0.1:9000/?session_key=abcdefg");

View File

@ -20,7 +20,9 @@ The library is deliberately quite low-level and generic; it's designed to
be compatible with any JavaScript code or framework, so you can build more be compatible with any JavaScript code or framework, so you can build more
specific integration on top of it. specific integration on top of it.
To process messages:: To process messages
.. code-block:: javascript
const webSocketBridge = new channels.WebSocketBridge(); const webSocketBridge = new channels.WebSocketBridge();
webSocketBridge.connect('/ws/'); webSocketBridge.connect('/ws/');
@ -28,11 +30,15 @@ To process messages::
console.log(action, stream); console.log(action, stream);
}); });
To send messages, use the `send` method:: To send messages, use the `send` method
.. code-block:: javascript
webSocketBridge.send({prop1: 'value1', prop2: 'value1'}); webSocketBridge.send({prop1: 'value1', prop2: 'value1'});
To demultiplex specific streams:: To demultiplex specific streams
.. code-block:: javascript
webSocketBridge.connect(); webSocketBridge.connect();
webSocketBridge.listen('/ws/'); webSocketBridge.listen('/ws/');
@ -43,11 +49,15 @@ To demultiplex specific streams::
console.info(action, stream); console.info(action, stream);
}); });
To send a message to a specific stream:: To send a message to a specific stream
.. code-block:: javascript
webSocketBridge.stream('mystream').send({prop1: 'value1', prop2: 'value1'}) webSocketBridge.stream('mystream').send({prop1: 'value1', prop2: 'value1'})
The `WebSocketBridge` instance exposes the underlaying `ReconnectingWebSocket` as the `socket` property. You can use this property to add any custom behavior. For example:: The `WebSocketBridge` instance exposes the underlaying `ReconnectingWebSocket` as the `socket` property. You can use this property to add any custom behavior. For example
.. code-block:: javascript
webSocketBridge.socket.addEventListener('open', function() { webSocketBridge.socket.addEventListener('open', function() {
console.log("Connected to WebSocket"); console.log("Connected to WebSocket");