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
from channels import Group | |
def ws_connect(message): | |
print("Someone connected.") | |
path = message['path'] # i.e. /sensor/ | |
if path == b’/sensor/': | |
print("Adding new user to sensor group") | |
Group(“sensor").add(message.reply_channel) # Adds user to group for broadcast | |
message.reply_channel.send({ # Reply to individual directly |
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
""" | |
Django settings for sensor project. | |
Generated by 'django-admin startproject' using Django 1.9.2. | |
For more information on this file, see | |
https://docs.djangoproject.com/en/1.9/topics/settings/ | |
For the full list of settings and their values, see | |
https://docs.djangoproject.com/en/1.9/ref/settings/ |
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
import os | |
import channels.asgi | |
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “sensor.settings”) | |
channel_layer = channels.asgi.get_channel_layer() |
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
from sensor.consumers import ws_message, ws_connect, ws_disconnect | |
channel_routing = { | |
'websocket.connect': ws_connect, | |
'websocket.receive': ws_message, | |
'websocket.disconnect': ws_disconnect, | |
} |
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
def ws_message(message): | |
# ASGI WebSocket packet-received and send-packet message types | |
# both have a "text" key for their textual data. | |
print("Received!!" + message['text']) |
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
def ws_disconnect(message): | |
print("Someone left us...") | |
Group("sensor").discard(message.reply_channel) |
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
from django.shortcuts import render | |
def sensor(request): | |
return render(request, "sensor.html", { | |
'sensor': '99', | |
}) |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>{% block title %}Sensor Example{% endblock %}</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
{% load staticfiles%} |
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
$(function() { | |
// When we're using HTTPS, use WSS too. | |
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws"; | |
var chatsock = new ReconnectingWebSocket(ws_scheme + '://' + window.location.host + "/sensor/"); | |
chatsock.onopen = function() { | |
console.log("Connected!"); | |
$('#sensor').text("Connected!"); | |
chatsock.send("Connected!"); | |
}; |
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
from channels import Group | |
from django.core.management import BaseCommand | |
import time | |
#The class must be named Command, and subclass BaseCommand | |
class Command(BaseCommand): | |
# Show this when the user types help | |
help = "Simulates reading sensor and sending over Channel." | |
# A command must define handle() |
OlderNewer