Tommy Pritchett
Tommy Pritchett

Reputation: 1

React and socket io mobile disconnection instantly when switching apps

I am developing a game to be played most likely on a phone via the internet. I have it working as a beta but whenever I am on my phone and change apps it instantly exits and removes my player from the lobby. Ideally I would have a 2-3 minute pause before removing the player. Based on my research it is saying to edit my pingintervals and pingtimeout but that is doing nothing anyone have any ideas???

the game itself is working as expected to this point. But it constantly disconnected will be a huge issue.

I've tried a ton of different things such as doing this on the backend:

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path'); // Import the path module
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
  cors: {
    origin: '*',  // Adjust this for security in production
    methods: ['GET', 'POST'],
    allowedHeaders: ['Content-Type', 'Authorization'],
    credentials: true,
  },
  pingInterval: 5000,  // Send a ping every 5 seconds
  pingTimeout: 60000,   // Allow up to 60 seconds without a pong before disconnecting
});
const PORT = process.env.PORT || 3001; // Default to 3001 if not on Heroku
const cors = require('cors');
const rooms = {};  // Store rooms and players
const playerStats = {};  // Store drink and shotgun counts for each player
const roundResults = {};  // Store drink assignments for each round
const formerPlayers = {};

And this on the front end:

const socket = io(process.env.REACT_APP_API_URL || 'url', {  
  reconnection: true,            // Enable reconnection
  reconnectionAttempts: 5,        // Try to reconnect up to 5 times
  reconnectionDelay: 5000,        // Wait 5 seconds between each reconnection attempt
  timeout: 60000,                 // Wait 60 seconds before failing the connection
  pingInterval: 5000,  // Send a ping every 5 seconds
  pingTimeout: 60000,   // Allow up to 60 seconds without a pong before disconnecting
});

I have tried making the intervals larger and smaller and still haven't seen any success.

Upvotes: 0

Views: 23

Answers (0)

Related Questions