Skip to content

Commit 81cf917

Browse files
author
Mark Gibbs
committed
Moved to channels websocket bridge
1 parent e419a1e commit 81cf917

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

demo/demo/consumers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from channels.generic.websocket import WebsocketConsumer
22

3+
import json
4+
35
class MessageConsumer(WebsocketConsumer):
46
def connect(self):
57
self.accept()
68
def disconnect(self, close_code):
79
pass
810
def receive(self, text_data):
911
print("Got incoming")
12+
message = json.loads(text_data)
1013
print(text_data)
11-
self.send("Thanks for [%s]"%text_data)
14+
self.send(json.dumps({'message':"Thanks for [%s]"%text_data}))
15+
self.send(json.dumps({'original_message':message}))

demo/demo/templates/index.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE HTML>
22
<html>
3+
{%load staticfiles%}
34
{%load plotly_dash%}
45
<title>Simple stuff</title>
56
<body>
@@ -16,11 +17,14 @@
1617
{%plotly_app name="Ex2"%}
1718
</div>
1819
</body>
20+
<script src="{%static "channels/js/websocketbridge.js"%}"></script>
1921
<script>
20-
var socket = new WebSocket("ws://"+window.location.host+"/ws/channel");
21-
socket.onmessage = function(e) {
22-
console.log("Got ws inbound message");
23-
console.log(e);
24-
};
22+
const wsb = new channels.WebSocketBridge();
23+
wsb.connect("/ws/channel");
24+
wsb.listen(function(action, stream) {
25+
console.log("WSB inbound");
26+
console.log(action);
27+
console.log(stream);
28+
});
2529
</script>
2630
</html>

0 commit comments

Comments
 (0)