Skip to content

Commit e419a1e

Browse files
author
Mark Gibbs
committed
Added synchronous websocket consumer and simple echo use from index page
1 parent 21fd23a commit e419a1e

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

demo/demo/consumers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from channels.generic.websocket import WebsocketConsumer
2+
3+
class MessageConsumer(WebsocketConsumer):
4+
def connect(self):
5+
self.accept()
6+
def disconnect(self, close_code):
7+
pass
8+
def receive(self, text_data):
9+
print("Got incoming")
10+
print(text_data)
11+
self.send("Thanks for [%s]"%text_data)

demo/demo/routing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from channels.routing import ProtocolTypeRouter
1+
from channels.routing import ProtocolTypeRouter, URLRouter
2+
from channels.auth import AuthMiddlewareStack
3+
4+
from django.conf.urls import url
5+
6+
from .consumers import MessageConsumer
27

38
application = ProtocolTypeRouter({
9+
'websocket': AuthMiddlewareStack(URLRouter([url('ws/channel', MessageConsumer),])),
410
})

demo/demo/templates/index.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE HTML>
22
<html>
33
{%load plotly_dash%}
4-
<title>Simple stuff</title>
4+
<title>Simple stuff</title>
55
<body>
66
<div>
77
Content here
@@ -15,5 +15,12 @@
1515
Content here
1616
{%plotly_app name="Ex2"%}
1717
</div>
18-
</body>
18+
</body>
19+
<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+
};
25+
</script>
1926
</html>

0 commit comments

Comments
 (0)