简体   繁体   English

如何使用node.js从Codeigniter获取会话

[英]how to get session from codeigniter with node.js

i'm writing application like social network where in my application can show status update and chat . 我正在编写类似社交网络的应用程序,其中我的应用程序中可以显示状态更新和聊天。 when i search on internet i found node.js for long polling technology and i think i can use that for chat and streaming page in my application. 当我在互联网上搜索时,我发现了用于长期轮询技术的node.js,我认为我可以将其用于应用程序中的聊天和流媒体页面。 but when i use node.js i have a stack 但是当我使用node.js时我有一个堆栈

this is a technology i want to my project: 1) i'm using codeigniter for framework and mysql database in address localhost:81/myproject 2) and using node.js in port 127.0.0.1:8080 to chat and streaming page 这是我想在我的项目中使用的一项技术:1)我在地址localhost:81 / myproject中将codeigniter用于框架和mysql数据库2)在端口127.0.0.1:8080中使用node.js进行聊天和流式处理页面

this is code javascript server with node.js name is server.js 这是代码JavaScript服务器,其node.js名称为server.js

var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
events = require("events");

function load_static_file(uri, response) {
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
    if(!exists) {
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.write("404 Not Found\n");
        response.end();
        return;
    }

    fs.readFile(filename, "binary", function(err, file) {
        if(err) {
            response.writeHead(500, {"Content-Type": "text/plain"});
            response.write(err + "\n");
            response.end();
            return;
        }

        response.writeHead(200);
        response.write(file, "binary");
        response.end();
    });
});
   } 
   var local_client = http.createClient(81, "localhost");

   var local_emitter = new events.EventEmitter();

   function get_users() {
      var request = local_client.request("GET", "/myproject/getUser", {"host": "localhost"});

request.addListener("response", function(response) {
    var body = "";
    response.addListener("data", function(data) {
        body += data;
    });

    response.addListener("end", function() {
        var users = JSON.parse(body);
        if(users.length > 0) {
            local_emitter.emit("users", users);
        }
    });
});

request.end();
 }

 setInterval(get_users, 5000);

 http.createServer(function(request, response) {
    var uri = url.parse(request.url).pathname;
    if(uri === "/stream") {

    var listener = local_emitter.addListener("users", function(users) {
        response.writeHead(200, { "Content-Type" : "text/plain" });
        response.write(JSON.stringify(users));
        response.end();

        clearTimeout(timeout);
    });

       var timeout = setTimeout(function() {
        response.writeHead(200, { "Content-Type" : "text/plain" });
        response.write(JSON.stringify([]));
        response.end();

        local_emitter.removeListener(listener);
       }, 10000);

   }
   else {
       load_static_file(uri, response);
   }
  }).listen(8383);

  sys.puts("Server running at http://localhost:8383/");

now in codeigniter side i making webservices on url http://localhost:81/myproject/getUser with response is json format and i access this with session auhtentication if not is redirect to login page. 现在在codeigniter端,我在url http:// localhost:81 / myproject / getUser上进行web服务,响应为json格式,如果没有重定向到登录页面,我将通过会话auhtentication访问它。

[{"user_id":"2a20f5b923ffaea7927303449b8e76daee7b9b771316488679","token":"3m5biVJMjkCNDk79pGSo","username":"rakhacs","password":"*******","name_first":"rakha","name_middle":"","name_last":"cs","email_id":"email@gmail.com","picture":"img\/default.png","active":"1","online":"0","created_at":"2011-09-20 11:14:43","access":"2","identifier":"ae70c3b56df19a303a7693cdc265f743af5b0a6e"},{"user_id":"9a6e55977e906873830018d95c31d2bf664c2f211316493932","token":"RoUvvPyVt7bGaFhiMVmj","username":"ferdian","password":"*****","name_first":"willy","name_middle":"","name_last":";f;w","email_id":"email1@gmail.com","picture":"img\/default.png","active":"1","online":"0","created_at":"2011-09-20 11:47:20","access":"2","identifier":"1ccd4193fa6b56b96b3889e59c5205cc531177c9"}]

this is the point problem when i execute node server.js i get error like this undefined:0 sysntaxerror:unexpected end of input at object.parse(native) 这是我执行node server.js时的关键问题,我得到这样的错误undefined:0 sysntaxerror:unexpected end of input at object.parse(native)

i don't know about that error i think because i using session ? 我不知道该错误,因为我正在使用会话? but i'm not sure. 但我不确定。 when i test using test.js for testing my webservices 当我使用test.js测试我的Web服务时

var http = require("http")
var options = {
    host: 'localhost',
    port: 81,
    path: '/myproject/getUser',
    method: 'GET'
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
   });
});
req.on('error', function(e) {
   console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

and this is the output 这是输出

problem with request: parse error

but when i using another webservice who has been response json format too and not using session that's work i can get the body response..if this problem is session how i can fix this?..this is can make me confused..thanks for your comment 但是当我使用另一个也已经响应json格式的Web服务并且不使用会话时,我可以得到主体响应..如果这个问题是会话,我该如何解决呢?..这会使我感到困惑..谢谢你的评论

hard to say but I would primary try to send an object in jSON and not directly an array. 很难说,但我会主要尝试在jSON中发送对象,而不是直接发送数组。 ie just encapsulate the array into an object: 即只是将数组封装到一个对象中:

{"data":[…]}

This "may" be causing the parse error. 此“可能”导致解析错误。

Something else, for this purpose, it won't be bad to add the following to your responding PHP method: 为此,可以在响应的PHP方法中添加以下内容:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

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

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