【问题标题】:django saving messages to database via node server and socket.iodjango 通过节点服务器和 socket.io 将消息保存到数据库
【发布时间】:2015-03-11 07:26:45
【问题描述】:

我有节点服务器:

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

var redis = require('redis');
var sub = redis.createClient();

//Subscribe to the Redis chat channel
sub.subscribe('chat');

//Configure socket.io to store cookie set by Django
io.use(function(){
    io.set('authorization', function(data, accept){
        if(data.headers.cookie){
            data.cookie = cookie_reader.parse(data.headers.cookie);
           return accept(null, true);
        }
        return accept('error', false);
     });
    io.set('log level', 1);
 });

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

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

//Client is sending message through socket.io
socket.on('send_message', function (message) {
    values = querystring.stringify({
        comment: message,
        sessionid: socket.handshake.cookie['sessionid'],
    });

    var options = {
        host: 'localhost',
        port: 8000,
        path: '/node_api',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': values.length
        }
    };

    //Send message to Django server
    var req = http.request(options, function(res){
        res.setEncoding('utf8');

        //Print out error message
        res.on('data', function(message){
            if(message != 'Everything worked :)'){
                console.log('Message: ' + message);
            }
        });
    });

    req.write(values);
    req.end();
});

});

当我发送消息时,它不会保存在数据库中..

这是我对 django 的看法:

@csrf_exempt
def node_api(request):
    print "inside node"
    try:
        print "inside try"
        session = Session.objects.get(session_key=request.POST.get("sessionid"))
        print "first"
        user_id = session.get_decode().get("_auth_user_id")
        print user_id
        user = User.objects.get(id=user_id)

        Comments.objects.create(user=user, text=request.POST.get("comment"))
        r = redis.StrictRedis(host="localhost", port=6379, db=0)
        r.publish("chat", user.username + ": " + request.POST.get("comment"))

        return HttpResponse("Everything worked :")
    except Exception, e:

        return HttpResponseServerError(str(e))

这里有什么问题??

谁能指导我正确的方向? 当我转到“/node_api/”网址时,它说会话匹配查询不存在..

我的消息显示视图:

@login_required
def home(request):
    print "entered"
    comments = Comments.objects.select_related().all()[0:100]
    return render(request, "index.html", locals())

当我提交消息时,它不保存到数据库也不显示......

提前谢谢??

【问题讨论】:

    标签: python django node.js redis socket.io


    【解决方案1】:

    如果你使用的是python,那你为什么不试试python的实时方式。

    用 django 试试 tornado。它支持socket.io..

    您可以毫无困难地使用 python 编写代码。 这并不意味着 javascript 不好,而是尝试在 python 中执行并查看 tornado、redis、django。你会找到你的解决方案。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2020-02-05
      • 1970-01-01
      • 2020-11-01
      相关资源
      最近更新 更多