使用Django和Socket.io进行实时通知

时间:2016-03-22 10:22:13

标签: python django socket.io

目前我正在为我的Django项目实施实时通知。

我按照this tutorial的说明操作。问题是,我使用的是Socket.io 1.4.5,而教程是针对1.0之前的版本编写的。所以我必须在Socket.io网站上的'Migrating from 0.9'指南之后调整一些代码。我得到的是:

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']);
     });
});

当我运行此脚本时:

node notifications.js

沉默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)

有人能指出我做错了吗?

1 个答案:

答案 0 :(得分:2)

刚刚发现了我的错误。

要访问Cookie,而不是socket.handshake,我应该使用socket.request。所以我现在的代码现在看起来像这样:

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']);
    });
});