0

I created a simple test refer to this tutorial: https://youtu.be/HZWmrt3Jy10 but there is an error: Failed to load resource: :3000/socket.io/?EIO=4&transport=polling&t=Nt3f74j:1 the server responded with a status of 404 (Not Found)

this is server.js:

var express = require('express');
var app = express();

var server = app.listen(3000);

app.use(express.static('public'));

var socket = require('socket.io');
var io = socket(server);

io.sockets.on('connection', newConnection);

function newConnection(socket) {
  console.log(socket);
  console.log('new connection');
}

this is sketch.js:

socket = io.connect('http://127.0.0.1:3000');

this is index.html:

<script src="https://cdn.socket.io/4.4.0/socket.io.js"></script>

I've tried socket.io: Failed to load resource Failed to load resource socket.io Failed to load resource - Socket.IO but they all don't work, it seems the socket client does not find socket.io

#update of the question:

  1. According to the Troubleshooting document of socket.io Troubleshooting connection issues, I check URL https://localhost:3001/socket.io/?EIO=4&transport=polling and the result is entry not found: /socket.io, while it should return something like 0{"sid":"Lbo5JLzTotvW3g2LAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}

  2. I try to check whether socket.io is connected and the console output is connect failedError: xhr poll error. After I check Consistent "xhr poll error" is caused by hanging instances of XHRPolling, there should be some problems with socket's connection to the server, but I've no idea how to fix it

console.log(socket.connected);

socket.on('error', function()
{
    console.log("Sorry, there seems to be an issue with the connection!");
});

socket.on('connect_error', function(err)
{
    console.log("connect failed"+err);
});

socket.on('connection', function ()
{
    console.log("connected");
    socket.on('newPhoto',function()
    {
        load_posts();
    });
});
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Try `socket = io()` instead of `io.connect` – Ameer Dec 16 '21 at 21:57
  • yes, I've tried, but it doesn't work. Could you have a look at my update, I found some new clues but don't know how to fix it – Haoyue Yang Dec 17 '21 at 00:09
  • In your update you referenced checking `https://localhost:3001/socket.io/?EIO=4&transport=polling` while troubleshooting? Did you mean to specify port 3000 there? It is not clear why you would be testing that on port 3001 instead. – Paul Wheeler Dec 17 '21 at 03:43
  • Also is it fair to assume that you have the same version of socket.io installed on the backend (4.4.0)? I'm not able to reproduce your problem. Here's a working example: https://replit.com/@KumuPaul/Web-Sockets-Test#index.js – Paul Wheeler Dec 17 '21 at 03:49
  • try moving `app.use(express.static('public'));` to after the socket initialization – Samathingamajig Dec 17 '21 at 06:26
  • I'm sorry to cause a misunderstanding. I changed the port later to 3001 as I tried to find if it's a problem – Haoyue Yang Dec 17 '21 at 11:49
  • @Paul Wheeler https://replit.com/@HaoyueYang/Socket-Test-3#public/sketch.js could you have a look at my sketch, it's very simple but doesn't work at all – Haoyue Yang Dec 18 '21 at 00:34
  • @Samathingamajig could you have a look at my example https://replit.com/@HaoyueYang/Socket-Test-3#public/sketch.js it's simple but I still doesn't know what's the problem – Haoyue Yang Dec 18 '21 at 00:36
  • In order for this project to run on Replit.com the Repl will need to be a Node.js repl not an HTML repl because you need your backend express server to be running and HTML repls don't have a backend running (they only serve static content). Here's an example: https://replit.com/@KumuPaul/Socket-Test-3 It appears that this is successfully connecting to your WebSocket server. The only change I had to make was to move all of your client side files (index.html and sketch.js) into the public folder. I also added nodemon and customized the Repl's run command. – Paul Wheeler Dec 18 '21 at 02:55

0 Answers0