admin管理员组文章数量:1391999
I'm trying to connect to server io.js+socket.io with socket.io client. It starts with xhr polling requests, the connect event and even first message are receiving throught xhr, then it upgrades to websocket. How can i detect when the switch of the transport happens to log it (on both sides)?
Simplified server code:
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");
})
Simplified client code:
var socket = io.connect();
socket.on("hello",function(data){
socket.emit("join",{userInfo: {name:"someName"}});
setTimeout(function(){
socket.emit("testMsg",{});
},8000)
});
I'm trying to connect to server io.js+socket.io with socket.io client. It starts with xhr polling requests, the connect event and even first message are receiving throught xhr, then it upgrades to websocket. How can i detect when the switch of the transport happens to log it (on both sides)?
Simplified server code:
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");
})
Simplified client code:
var socket = io.connect();
socket.on("hello",function(data){
socket.emit("join",{userInfo: {name:"someName"}});
setTimeout(function(){
socket.emit("testMsg",{});
},8000)
});
Share
Improve this question
edited Sep 19, 2016 at 8:10
ForceUser
asked Feb 19, 2015 at 16:14
ForceUserForceUser
1,1391 gold badge15 silver badges23 bronze badges
1
- I'm also interested to know. – ricardopereira Commented Mar 19, 2015 at 18:59
1 Answer
Reset to default 8Client Side
You can catch changes with:
<script>
var socket = io();
/* Transport */
socket.io.engine.on('upgrade', function(transport) {
console.log('transport changed');
});
</script>
Server Side
You can catch changes with:
io.on('connection', function(socket) {
console.log('User connected')
// Transport event
socket.conn.on('upgrade', function(transport) {
console.log('transport changed')
})
})
本文标签: javascriptHow to detect when socketio transport changes (event or something)Stack Overflow
版权声明:本文标题:javascript - How to detect when socket.io transport changes? (event or something) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744663575a2618396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论