Can't establish connection between client and server using node js with socket.io and express and a html file
I was following a Youtube tutorial (https://www.youtube.com/watch?v=PXBy6pB4mTU&list=PLo6lBZn6hgcZ4-xQFjDPfWnONyq9vFnh9) for a simple web game with lobbies/rooms, until I stumbled upon this problem: when trying to get the client to connect to the server, I didn't get the expected: "a user connected" text outputted to the console. I'm using a html file and Node js with express and socket.io installed. I also tried searching the internet (stack overflow, expressjs.com and https://socket.io/docs/v4/troubleshooting-connection-issues/)for solutions but couldn't really find any useful solutions, perhaps because of my bad understanding of code.
I start my server.js via inputting "node server.js" in the Terminal of VScode and after that I start a live server of my Index.html file. The tutorial showed the expected "a user connected" (7:24 in the video) but on my side nothing happend.
node js version: 9.6.7 express version: 4.18.2 socket.io version: 4.7.2
This is my Index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.js"></script>
<script src="client.js"></script>
</body>
</html>
This is the client.js file: const socket = io('http://localhost:3000');
Last the server.js file:
const express = require('express')
const app = express()
const port = 3000
const server = app.listen(port)
const io = require('socket.io')(server)
app.get('/', (req, res) => {
res.send('Hello World!');
});
io.on('connection', (socket) => {
console.log('a user connected');
});
Via Active questions tagged javascript - Stack Overflow https://ift.tt/HdSxJQE
Comments
Post a Comment