我正在编写类似社交网络的应用程序,在我的应用程序中可以显示状态更新和聊天。当我在互联网上搜索时,我发现node.js用于长轮询技术,我想我可以在我的应用程序中使用它进行聊天和流媒体页面。但是当我使用node.js时,我有一个堆栈
这是我想要的项目技术: 1)我在地址localhost:81 / myproject中使用codeigniter for framework和mysql数据库 2)并使用端口127.0.0.1:8080中的node.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/");
现在在codeigniter方面我在url http://localhost:81/myproject/getUser上创建webservices并且响应是json格式,如果没有重定向到登录页面,我使用session 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"}]
这是我执行node server.js
时的问题
我得到像undefined:0
sysntaxerror:unexpected end of input
at object.parse(native)
我不知道这个错误我认为因为我使用会话?但我不确定。 当我测试使用test.js测试我的webservices
时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
但是当我使用另一个已经响应json格式的webservice并且没有使用会话时,我可以得到身体响应..如果这个问题是会话我怎么能解决这个问题?这可能让我感到困惑。 。感谢您的评论
答案 0 :(得分: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');