miroslavpejic85 / mirotalksfu

🏆 WebRTC - SFU - Simple, Secure, Scalable Real-Time Video Conferences Up to 8k, compatible with all browsers and platforms.
https://sfu.mirotalk.com
GNU Affero General Public License v3.0
2.15k stars 340 forks source link

Keep websocket connection alive in browsers on ios while running background #96

Closed qiangweihewu closed 1 year ago

qiangweihewu commented 1 year ago

Hi there! I just wonder if it is possible to keep websocket connection alive in browsers on IOS while running background, below is some codes I tried on Room.js, but throws many errors. However, would you mind to take a look and see if there is any possible solutions to make it happen? Thank you very much! Blessings!

const socket = io({ transports: ['websocket'], upgrade: false // 确保在WebSocket连接建立之前不会尝试使用HTTP升级 });

let heartbeatId = null;

// 启动心跳 function startHeartbeat() { function sendHeartbeat() { socket.emit('ping'); heartbeatId = window.requestAnimationFrame(sendHeartbeat); } heartbeatId = window.requestAnimationFrame(sendHeartbeat); }

// 停止心跳 function stopHeartbeat() { window.cancelAnimationFrame(heartbeatId); }

// 处理可见性变化和网络状态变化 function handleVisibilityChange() { if (document.visibilityState === 'hidden') { // 如果浏览器进入后台,停止心跳并断开WebSocket连接 stopHeartbeat(); socket.disconnect(); } else { // 如果浏览器恢复可见性,并且网络状态在线,则尝试重连WebSocket if (navigator.onLine) { socket.connect(); startHeartbeat(); } } }

// 在页面加载完成后启动WebSocket连接和心跳 window.addEventListener('load', () => { socket.connect(); startHeartbeat(); });

// 监听可见性和网络状态变化 document.addEventListener('visibilitychange', handleVisibilityChange); window.addEventListener('online', handleVisibilityChange); window.addEventListener('offline', handleVisibilityChange);

miroslavpejic85 commented 1 year ago

Hello @qiangweihewu,

Since Mirotalk is a PWA, remains active, until you close the browser or press the button to leave the room.

Join us on Discord - Ideas and suggestions channel where we can also discuss all together and you can also interact with the community as well.

Thank you!