Skip to content

Commit

Permalink
Hey look it's an echo server.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobian committed Mar 2, 2016
1 parent f1c7c75 commit 9f97b94
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 26 deletions.
8 changes: 7 additions & 1 deletion NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ Random notes and stuff, likely to become feedback to Andrew:

- static files is weird. how should it work in prod? WhiteNoise-ish would be
ideal; integrate with Daphe?
- channel backend - parse from URL?
- channel backend - parse from URL?
- "routing" in websockets is weird - there's a path, but realy only a single
consumer. some sort of url-ish routing perhaps?

websocket.connect -> path dispatch -> websocket.connect.{path} channel

- debugging errors in websocket consumers SUCKS
5 changes: 4 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
might make a good flow for the article, start with old-style and then add WSS
- channels backend shouild use config var (URL-ish?
- test deploy to heroku

- give rooms a name
- and let people change them -- would demonstrate different kinds of messages
- rename Message.username to Message.handle?
- reorganize code - ideally the chat app should be semi-reusable
6 changes: 5 additions & 1 deletion channels_example/routing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from channels.staticfiles import StaticFilesConsumer
from chat.consumers import ws_connect, ws_receive

channel_routing = {
# This makes Django serve static files from settings.STATIC_URL, similar
# to django.views.static.serve. This isn't ideal (not exactly production
# quality) but it works for a minimal example.
'http.request': StaticFilesConsumer()
'http.request': StaticFilesConsumer(),

'websocket.connect': ws_connect,
'websocket.receive': ws_receive,
}
26 changes: 22 additions & 4 deletions channels_example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@
os.path.join(BASE_DIR, 'static'),
]

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

# Channels
# FIXME: read from env
CHANNEL_LAYERS = {
Expand All @@ -134,4 +130,26 @@
},
"ROUTING": "channels_example.routing.channel_routing",
},
}

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'propagate': True,
'level': 'DEBUG'
},
'chat': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG',
},
},
}
10 changes: 10 additions & 0 deletions chat/consumers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import logging

log = logging.getLogger(__name__)

def ws_connect(message):
log.debug('ws_connect path={path}'.format(**message.content))

def ws_receive(message):
log.debug('ws_receive text={text}'.format(**message.content))
message.reply_channel.send({'text': message['text']})
13 changes: 13 additions & 0 deletions static/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$(function() {
// When we're using HTTPS, use WSS too.
var ws_scheme = window.location.protocol == "https:" ? "wss://" : "ws://";

var chatsock = new ReconnectingWebSocket(ws_scheme + location.host + "/chat");
chatsock.onmessage = function(message) {
alert(message.data);
};

$("#go").on("click", function(event) {
chatsock.send($("#message")[0].value);
});
});
1 change: 0 additions & 1 deletion static/humans.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
hi
5 changes: 5 additions & 0 deletions static/jquery-1.12.1.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/reconnecting-websocket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{% block content %}{% endblock content %}
{% block content %}{% endblock content %}

{% block afterbody %}{% endblock afterbody %}
41 changes: 24 additions & 17 deletions templates/room.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
{% extends "base.html" %}

{% block content %}
<h1>{{ room.label }}</h1>
<table>
{% for message in messages %}
<tr>
<td>{{ message.timestamp }}</td>
<td>{{ message.username }}</td>
<td>{{ message.message }}</td>
</tr>
{% endfor %}
{% load staticfiles %}
<h1>{{ room.label }}</h1>
<table>
{% for message in messages %}
<tr>
<td>Say something:</td>
<td><input id="username" type="text" placeholder="your name"></td>
<td>
<input id="message" type="text" placeholder="message">
<button id="go">Say it</button>
</td>
</table>
{% endblock content %}
<td>{{ message.timestamp }}</td>
<td>{{ message.username }}</td>
<td>{{ message.message }}</td>
</tr>
{% endfor %}
<tr>
<td>Say something:</td>
<td><input id="username" type="text" placeholder="your name"></td>
<td>
<input id="message" type="text" placeholder="message">
<button id="go">Say it</button>
</td>
</table>
{% endblock content %}

{% block afterbody %}
<script type="text/javascript" src='{% static "jquery-1.12.1.min.js" %}'></script>
<script type="text/javascript" src='{% static "reconnecting-websocket.min.js" %}'></script>
<script type="text/javascript" src='{% static "chat.js" %}'></script>
{% endblock afterbody %}

0 comments on commit 9f97b94

Please sign in to comment.