From acca42e693a55e5f0ef6846cbc2246925aa0bcb2 Mon Sep 17 00:00:00 2001 From: saad Date: Fri, 13 Oct 2023 00:58:11 -0400 Subject: [PATCH] basic app --- index.html | 46 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 27 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 index.html create mode 100644 index.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..444043d --- /dev/null +++ b/index.html @@ -0,0 +1,46 @@ + + + + + Socket.IO chat + + + + +
+ +
+ + + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..cf18681 --- /dev/null +++ b/index.js @@ -0,0 +1,27 @@ +const express = require('express'); +const { createServer } = require('node:http'); +const { join } = require('node:path'); +const { Server } = require('socket.io'); + +const app = express(); +const server = createServer(app); +const io = new Server(server); + +app.get('/', (req, res) => { + res.sendFile(join(__dirname, 'index.html')); +}); + +io.on('connection', (socket) => { + console.log('a user connected'); + socket.on('chat message', (msg) => { + io.emit('chat message', msg); + console.log('message: ' + msg); + }); + socket.on('disconnect', () => { + console.log('user disconnected'); + }); +}); + +server.listen(3000, () => { + console.log('server running at http://localhost:3000'); +}); \ No newline at end of file