我正在尝试使用socket.io客户端连接到io.js + socket.io服务器。 它从xhr轮询请求开始,连接事件甚至第一条消息都是通过xhr接收的,然后它升级到websocket。 如何检测传输切换发生的时间以记录它(在两侧)?
简化版服务器代码:
io.on("connection",function(socket){
console.log("transport",socket.conn.transport.name); //will print "polling"
socket.on("join",function(data){
console.log("transport",socket.conn.transport.name); //will print "polling" (usualy)
console.log("userjoined",data.userInfo);
});
socket.on("testMsg",function(data){
console.log("transport",socket.conn.transport.name); //will print "websocket" (if it supported and already switched)
});
socket.emit("hello","hello");
})
简化后的客户端代码:
var socket = io.connect();
socket.on("hello",function(data){
socket.emit("join",{userInfo: {name:"someName"}});
setTimeout(function(){
socket.emit("testMsg",{});
},8000)
});