Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
Working socket.io demo getting error message with

I've been looking for a working demo of socket.io that does not use Express.js, why doesn't this example work:

server:

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);

client:

<script src="node_modules/socket.io/client-dist/socket.io.js"></script>
<script>
  const socket = io("http://localhost");
</script>

This gives the error: polling-xhr.js:157 GET http://localhost/socket.io/?EIO=4&transport=polling&t=NwWVGV9 net::ERR_CONNECTION_REFUSED

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Found something that works:

server:

var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler(req, res) {
    var url;
    if (req.url == "/") {
        url = "/index.html";
    } else {
        url = req.url;
    }
    fs.readFile(__dirname + url, function (err, data) {
        if (err) {
            res.writeHead(404);
            res.end(JSON.stringify(err));
            return;
        }
        res.writeHead(200);
        res.end(data);
    });
}

io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
    socket.on('disconnect', function () {
        console.log(socket.id + ' disconnect ('+ socket.client.conn.transport.constructor.name +')');
    });
});

client:

<script src="node_modules/socket.io/client-dist/socket.io.min.js"></script>
<script>
  var socket = io();
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda