简体   繁体   English

Django 和 Socket.io 的实时通知

[英]Real time notifications with Django and Socket.io

currently i'm implementing real time notifications for my Django project.目前我正在为我的 Django 项目实现实时通知。

I'm following instructions from this tutorial .我正在按照本教程中的说明进行操作。 Problem is, i'm using Socket.io 1.4.5 and tutorial is written for pre-1.0 versions.问题是,我使用的是 Socket.io 1.4.5,教程是为 1.0 之前的版本编写的。 So i had to adapt some code following 'Migrating from 0.9' guideline on Socket.io site.因此,我必须按照 Socket.io 站点上的“从 0.9 迁移”指南修改一些代码。 What i got is:我得到的是:

var http = require('http');
var server = http.createServer().listen(8002);
var io = require('socket.io')(server);
var cookie_reader = require('cookie');
var querystring = require('querystring');
var redis = require('redis');

// Supposedly this should store cookie set by Django
io.use(function(socket,accept){
     var data = socket.request;
     if(data.headers.cookie){
     data.cookie = cookie_reader.parse(data.headers.cookie);
     return accept(null, true);
     }
     return accept('error', false);
});

io.sockets.on('connection', function (socket) {

     // Redis client
     client = redis.createClient();

     // Subscribe to notification channel
     client.subscribe('notifications.' + socket.handshake.cookie['sessionid']);
     console.log('subscribed');

     //Grab message from Redis and send to client
     client.on('message', function(channel, message){
         console.log('on message', message);
         socket.send(message);
     });

     // Unsubscribe
     socket.on('disconnect', function() {
         client.unsubscribe('notifications.' + socket.handshake.cookie['sessionid']);
     });
});

When i'm running this script:当我运行这个脚本时:

node notifications.js

After 2 seconds of silence i get this error:沉默 2 秒后,我收到此错误:

client.subscribe('notifications.' + socket.handshake.cookie['sessionid']);
                                                           ^
TypeError: Cannot read property 'sessionid' of undefined
at Namespace.<anonymous> (path/to/notifications.js)
at Namespace.emit (events.js:107:17)
at Namespace.emit (/path/to/node_modules/socket.io/lib/namespace.js:206:10)
at /path/to/node_modules/socket.io/lib/namespace.js:174:14
at process._tickCallback (node.js:355:11)

Can somebody point me to what i did wrong?有人可以指出我做错了什么吗?

Just found what my mistake was.刚刚发现我的错误是什么。

To access cookie, instead of socket.handshake i should be using socket.request .要访问 cookie,我应该使用socket.request而不是socket.handshake So my current code looks like this now:所以我当前的代码现在看起来像这样:

var http = require('http');
var server = http.createServer().listen(8002);
var io = require('socket.io')(server);
var cookie_reader = require('cookie');
var querystring = require('querystring');
var redis = require('redis');

io.use(function(socket,accept){
    var data = socket.request;
    if(data.headers.cookie){
        data.cookie = cookie_reader.parse(data.headers.cookie);
        return accept(null, true);
    }
    return accept('error', false);
});

io.on('connection', function (socket) {

    // Redis client
    client = redis.createClient();

    // Subscribe to notification channel
    client.subscribe('notifications.' + socket.request.cookie['sessionid']);
    console.log('subscribed');

    //Grab message from Redis and send to client
    client.on('message', function(channel, message){
        console.log('on message', message);
        socket.send(message);
    });

    // Unsubscribe
    socket.on('disconnect', function() {
        client.unsubscribe('notifications.' + socket.request.cookie['sessionid']);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM