I’m going to preface this by saying, I’m not that great at coding, so sorry if my code is bad.
What I’m trying to do is make an socket.io server, but I’m having trouble with the server code not running for some reason. Everything else runs, but not index.js.
Index.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Thingie</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="title" id="title">
Idk
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="script.js"></script>
</body>
</html>
script.js:
const socket = io();
io.on("Hello", (msg) => {
alert(msg)
});
index.js:
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
io.on('connection', (socket) => {
socket.emit("Hello", "Hi")
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
I have tried following the socket.io tutorial on their website, but that didn’t help me very much. I tried adding to the html hoping that it would run index.js, but that didn’t do anything either.
Hue is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.