Cannot get / socket.io /? EIO = 3 & transport = polling & t = LdmmKYz

I have a problem with my node.js server and my ionic 2 with socket.io (websocket) communication.

My ionic application is sending this error:

Cannot GET /socket.io/?EIO=3&transport=polling&t=LdmmKYz

and this is my code, I did not find my error.

my node.js code (using express):

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use( (req, res, next) => {
   res.header("Access-Control-Allow-Origin", "http://localhost:8100"); //The ionic server
   res.header("Access-Control-Allow-Credentials", "true");
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   next();
});
var port = Number(process.env.PORT || 8810);

io.on('connection', function (socket) {
    console.log('ping-pong started');
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

and this is the ionic 2 application code (inside the constructor):

this.connect = () => {
    this.socket = io('http://localhost:8810');
    console.log('socket started');

    this.socket.emit('connect', {data: 'data'});
        this.socket.on('news', (data)=>{
        console.log(data);
       this.socket.emit('my other event', { my: 'data' });
    });
}
this.connect();

What am I missing?

+8
source share
2 answers

I found my problem!

my problem was in server code:

var server = app.listen(8810)
var io = require('socket.io').listen(server);

it was a problem.

I needed to determine where the .io socket is listening without the socket.

change it and the error will disappear.

+16
source

Try this in the exact order if you are using express 4.

var express = require('express');
var app = express();
var server = app.listen(8810);
var io = require('socket.io').listen(server);

. API http://expressjs.com/en/4x/api.html.

+7

Source: https://habr.com/ru/post/1668303/


All Articles