-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
895 additions
and
180 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from channels.generic.websocket import WebsocketConsumer | ||
|
||
|
||
class HelloConsumer(WebsocketConsumer): | ||
groups = ["broadcast"] | ||
|
||
def connect(self): | ||
self.user = self.scope["user"] | ||
self.accept() | ||
|
||
def receive(self, text_data=None, bytes_data=None): | ||
if self.user.is_authenticated: | ||
self.send(text_data=f"Hello {self.user.first_name:} {self.user.last_name}!") | ||
else: | ||
self.send(text_data="Hello anonymous!") | ||
|
||
def disconnect(self, close_code): | ||
... |
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{% extends '_base.html' %} | ||
{% load i18n %} | ||
{% block full_title %}{{ PROJECT_NAME }}{% endblock full_title %} | ||
{% block content %} | ||
<section class="page page--wide"> | ||
<h1> | ||
Websockets demo | ||
</h1> | ||
<p> | ||
If websocket connection is successful you should see an alert with a message. | ||
</p> | ||
</section> | ||
<script> | ||
const websocket = new WebSocket( | ||
'ws://' | ||
+ window.location.host | ||
+ '/ws/hello/' | ||
) | ||
console.log('ws://' | ||
+ window.location.host | ||
+ '/ws/hello/') | ||
websocket.onopen = function(e) { | ||
websocket.send('Sending message from the browser') | ||
} | ||
|
||
websocket.onmessage = function(e) { | ||
alert(e.data) | ||
} | ||
|
||
websocket.onclose = function(e) { | ||
console.error('Websocket connection closed') | ||
} | ||
</script> | ||
{% endblock content %} |