Skip to main content

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

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data