2011-09-22 44 views
0

我正在編寫像我的應用程序可以顯示狀態更新和聊天的社交網絡應用程序。當我在互聯網上搜索時,我發現node.js爲長輪詢技術,我想我可以在我的應用程序中使用聊天和流媒體頁面。但是當我使用Node.js的,我有一個堆棧如何通過node.js獲取codeigniter會話

這是一個技術,我想我的項目: 1)我是在本地主機地址使用笨的框架和MySQL數據庫:81/myproject的 2),並使用Node.js的港口127.0.0.1:8080聊天和流媒體頁面

這是編寫JavaScript代碼服務器的nod​​e.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/"); 

現在笨身邊,我的網址http://localhost:81/myproject/getUser使得web服務與響應是JSON格式,我訪問這與會話auhtentication如果不是重定向噸o登錄頁面。

[{"user_id":"2a20f5b923ffaea7927303449b8e76daee7b9b771316488679","token":"3m5biVJMjkCNDk79pGSo","username":"rakhacs","password":"*******","name_first":"rakha","name_middle":"","name_last":"cs","email_id":"[email protected]","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":"[email protected]","picture":"img\/default.png","active":"1","online":"0","created_at":"2011-09-20 11:47:20","access":"2","identifier":"1ccd4193fa6b56b96b3889e59c5205cc531177c9"}] 

這是一點問題,當我執行node server.js 我得到錯誤這樣undefined:0 sysntaxerror:unexpected end of input at object.parse(native)

我不知道這個錯誤我想是因爲我使用會話?但我不確定。 當我測試使用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(); 

,這是輸出

problem with request: parse error

但是當使用其他web服務誰一直響應JSON格式也和我沒有使用會話這是工作我可以得到身體的反應..如果這個問題是會議如何我可以解決這個問題?..這是可以讓我感到困惑..感謝您的意見

回答

0

很難說,但我會主要嘗試發送一個jSON對象而不是直接數組。即將數組封裝到一個對象中:

{"data":[…]} 

這個「可能」導致瞭解析錯誤。

別的東西,爲了這個目的,它不會壞到以下添加到您迴應PHP方法:

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

我想在我的JSON是沒有問題的。因爲我可以分析that.i嘗試你的建議添加頭緩存控制,過期,和content-type.but當我執行「節點test.js」是相同的錯誤...當我檢查頭響應我發現重定向位置登錄頁面。 – viyancs

+0

數組本身在語法上是正確的,但是在返回一個數組而不是一個對象時,我已經得到了和你一樣的問題,於是我不得不封裝它,因爲我建議在上面這樣做。 –

+0

我試着改變我的json格式,像你的suggent,但保持錯誤.. – viyancs