简体   繁体   中英

Error: http://localhost:3001/socket.io/ 404 (Not Found)

I am trying to connect client-server using socket.io however at the moment of testing my code I am getting this error http://localhost:3001/socket.io/?EIO=3&transport=polling&t=MgUqscg 404 (Not Found)

I already tried two different ways to achieve this. One returns the error already specified the second one returns an error listen EADDRINUSE :::3001 can somebody please explain to me what am I doing wrong? all tutorials seem so easy but I am stuck at this point and I really want to learn how to use sockets to improve my skills.

This is my code for server

const express = require('express');
const http = require('http');
const app = express();
// Web socket config option #1
var server   = http.Server(app);
var io       = require('socket.io')(server);

// Web socket config option #2
var io       = require('socket.io').listen(server);
server.listen(PORT)

const PORT = process.env.PORT || 3001;
app.set('port', 3000)

io.on('connection', (socket) =>{
    console.log('a user is connected')
})

app.listen(PORT, function(){
    console.log(' ======= SERVER RUNNING =======');
})

This is my code for client

import * as io from 'socket.io-client';

private socket = io(`http://localhost:3001`)

I just want to connect client-server successfully without errors and print to console a success message. Thanks in advance.

two thing from my experience.

  1. remove app.listen official doc
  2. upgrade problem official doc

cheers, hope it can help you

I solved it by using a different port for sending secket.io events. server.listen(4444)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM