我正在尝试使用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");
})
Run Code Online (Sandbox Code Playgroud)
简化的客户端代码:
var socket = io.connect();
socket.on("hello",function(data){
socket.emit("join",{userInfo: {name:"someName"}});
setTimeout(function(){
socket.emit("testMsg",{});
},8000)
});
Run Code Online (Sandbox Code Playgroud) 我用这样的代码渲染 grecaptcha
let callback;
const p = new Promise((resolve) => callback = (result) => resolve(result));
grecaptcha.render(el, {
sitekey: window.settings.recaptchaKey,
size: "invisible",
type: "image",
callback: result => callback(result),
badge: "inline"
});
const key = await p;
Run Code Online (Sandbox Code Playgroud)
一切正常,但如果用户单击 recaptcha 模式的背景,recaptcha 会关闭并且我无法检测到它,所以我无限等待响应
我需要某种事件或回调来检测它何时关闭