Skip to main content
 首页 » 编程设计

python之使用 Django 和 Socket.io 的实时通知

2024年10月24日35sharpest

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

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

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) 

谁能指出我做错了什么?

请您参考如下方法:

刚刚发现我的错误所在。

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

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