Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
iPhone can't seem to keep connection to server using socket.io

I'm creating a browser-based app using node.js and socket.io. It allows users to create a game, and join a room using a room code (sort of like Jackbox Games, if you've seen that). Here's the problem I'm having. On my laptop, the connection stays open, but if I connect with my phone, the connection randomly closes. I've confirmed this as follows:

On the client side, I have this code:

const socket = io();

socket.on('ping', (data) => {
  showMessage('info', 'ping', 500);
  socket.emit('pong', data);
});

socket.on('error', (data) => {
  showMessage('error', data.message, 2000);
});

We initialize the socket, and then just immediately emit a 'pong' whenever the server sends us a 'ping'. ShowMessage is a function that flashes a message on screen - here's it's merely diagnostic.

On the server side, I have:

const socket = (http, server) => {
  const io = require('socket.io')(http, {
    pingInterval: 1000,
    pingTimeout: 1000,
  });
  io.listen(server);

  setInterval(() => {
    pingTime = Date.now();
    io.emit('ping', null);
  }, pingInterval);

  io.on('connection', (socket) => {
    console.log(`A user has connected from ${socket.handshake.address}`);
    console.log(`\tID: ${socket.id}`);
  }
}

Here's the problem. On my computer, I can go to localhost:3000 and see the "ping" message blinking on the screen. It keeps going until I kill the server. When I do it on my iPhone 11 (using Chrome), the "ping" message blinks for a bit, and then stops, presumably as the socket is closed. The pingInterval and pingTimeout settings on the server side mean that the socket will reconnect pretty quickly. Unfortunately, as the app I'm trying to build is a buzzer for a speed-based game (think Jeopardy - first person to hit the button gets to answer the question), just reconnecting alone isn't good enough - I would really prefer it if the socket would stay open. Is there a way to force it to stay open?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda