socket.io upgrade from 0.6 to 0.7 causes problems in socket.request
I had an application that uses node.js and socket.io. It was working fine three months ago but when I tried to run it now it showed me that the module socket.io is not defined (don't know why?). So I installed it again using npm install socket.io
. It turned out that socket.io new version was released with a different set of functions (or at least the way it is used). I fixed the changes but have still some problem. Here is what I do:
server:
var http = require('http');
...
var server = http.createServer ( function (request,response){
console.log("Client request: " + request.url);
...
});
server.listen(port);
var socketOptions = {
transportOptions: {
'xhr-polling': {
closeTimeout: 12000000, //20 mins
timeout: 1200000 //20 mins
}
}
}
var io = require('socket.io').listen(server,socketOptions);
io.sockets.on("connection", function(socket) {
console.log("WebSocket client connected");
if (!socket.request) { // xhr polling => get cookie using websocket message
socket.json.send(JSON.stringify({
message: "resendcookie",
why: "Missing cookie",
}));
} else {
console.dir(socket.request.headers.cookie);
... <use the cookie> ...
}
io.sockets.on ("message", function(data) {
...
});
io.sockets.on("close", function(){
...
});
io.sockets.on("disconnect", function(){
...
});
});
client:
var socket = io.connect("localhost:port", {
rememberTransport : false,
transportOptions : {
"xhr-polling": {
closeTimeout: 600000,
duration: 600000
}
开发者_如何学JAVA }
});
...
function onload() {
...
socket.on("connect", function(){
...
}
socket.on("mesasge", function(data) {
var m =JSON.parse(data);
if (m.message === "resendcookie"){
socket.send(JSON.stringify({
cookie: document.cookie,
}));
}
});
}
This is a slightly modified code of what was working fine with socket.io 0.6 to include the new way to connect and listen to the websocket. My problem is:
socket.request used to exist if using chrome since it allows for websockets and I could reach the cookies directly (the xhr-polling trick I used to reach cookies in other browsers that don't support websockets). Now socket.request is giving different results even with chrome and I can't find the cookies. Did I forget to modify anything else? If not, please me tell how to reach the cookies.
Thanks a lot,
Sabah
Did you read https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7 ? It contains allot of information on migrating from 0.6 to the latest 0.7
精彩评论