0

在这里试用教程

设置.py

    DRAGON_URL = 'http://localhost:9999/'

    TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'notifications.context_processors.dragon_url',
)

沼泽龙设置

SWAMP_DRAGON_CONNECTION = ('swampdragon.connections.sockjs_connection.DjangoSubscriberConnection', '/data')

context_processors.py

from django.conf import settings

def dragon_url(request):
    return {'DRAGON_URL': settings.DRAGON_URL}

主页.html

{% load staticfiles %}
{% load swampdragon_tags %}

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>

<h1>Notifications demo</h1>

<!-- This is our list of notifications -->
<ul id="notifications">
{% for notification in object_list %}
<li>{{ notification.message }}</li>
{% endfor %}
</ul>


<!-- SwampDragon -->
{% swampdragon_settings %}
<script type="text/javascript" src="{% static 'swampdragon/js/dist/swampdragon.js' %}"></script>


<script type="text/javascript" src="{% static 'swampdragon/js/vendor/sockjs-0.3.4.min.js' %}"></script>

<script type="text/javascript" src="{% static 'swampdragon/js/legacy/swampdragon-vanilla.js' %}"></script>
<script type="text/javascript" src="{% static 'notifications.js' %}"></script>
<script type="text/javascript" src="{% static 'swampdragon/js/dist/datamapper.js' %}"></script>

<script type="text/javascript" src="{{ DRAGON_URL }}settings.js"></script>

<!-- notifications -->
<script type="text/javascript" src="{% static 'notifications.js' %}"></script>

</body>
</html>

控制台中的错误

ReferenceError: SwampDragon is not defined

var sdInstance = new SwampDragon(options);

还尝试在控制台中显示设置

<script>
console.log(window.swampdragon_settings);
</script>

我明白了

endpoint        "/data"

不知道出了什么问题。如果需要更多信息,请告诉我。

如果需要点冻结

backports.ssl-match-hostname==3.4.0.2
certifi==2015.4.28
Django==1.7
django-filter==0.10.0
djangorestframework==3.1.3
Markdown==2.6.2
python-dateutil==2.4.2
redis==2.10.3
six==1.9.0
sockjs-tornado==1.0.1
SwampDragon==0.4.2.2
tornado==4.2
tornado-redis==2.4.18
wheel==0.24.0
4

1 回答 1

0

似乎问题是我遵循的教程有一些问题。请参阅问题以获取教程的链接。

我将notification.js更改为:

// Ask the browser for permission to show notifications
// Taken from https://developer.mozilla.org/en-US/docs/Web   /API/Notification/Using_Web_Notifications
window.addEventListener('load', function () {
    Notification.requestPermission(function (status) {
        // This allows to use Notification.permission with Chrome/Safari
        if (Notification.permission !== status) {
            Notification.permission = status;
        }
    });
});


// Create an instance of vanilla dragon
//var dragon = swampdragon.open({onopen: onOpen, onchannelmessage:    onChannelMessage});

// This is the list of notifications
var notificationsList = document.getElementById("notifications");

// New channel message received
swampdragon.onChannelMessage(function(channels, message){
// Add the notification
addNotification((message.data));
});

// SwampDragon connection open
swampdragon.open(function() {
// Once the connection is open, subscribe to notifications
swampdragon.subscribe('notifications', 'notifications');
});


// Add new notifications
function addNotification(notification) {
// If we have permission to show browser notifications
// we can show the notifiaction
if (window.Notification && Notification.permission === "granted") {
    new Notification(notification.message);
}

// Add the new notification
var li = document.createElement("li");
notificationsList.insertBefore(li, notificationsList.firstChild);
li.innerHTML = notification.message;

// Remove excess notifications
while (notificationsList.getElementsByTagName("li").length > 5) {
    notificationsList.getElementsByTagName("li")[5].remove();
}
}

而不是使用

var dragon = new VanillaDragon(...)

我改为使用

swampdragon.<function name> 

它有效。

于 2015-07-15T15:15:46.597 回答