this is not working but i have to make commit

This commit is contained in:
slawk0
2024-08-21 22:38:54 +02:00
parent b233ee3fa3
commit 31cc5f7aa0
5 changed files with 66 additions and 21 deletions

View File

@@ -21,4 +21,6 @@ function chat(req, res) {
});
}
module.exports = chat;
module.exports = {
chat
};

View File

@@ -1,9 +1,8 @@
const socket = io();
const socket = io();
const form = document.getElementById('form');
const input = document.getElementById('input');
const cos = document.getElementById('cos');
let cos = jklsdfjklfsdjkldfsjkldsfklj
form.addEventListener('submit', (e) => {
e.preventDefault();
if (input.value) {

View File

@@ -1,21 +1,44 @@
<!doctype html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Chat</title>
<script src="../js/chat.js" defer></script>
</head>
<style>
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
#form { background: rgba(0, 0, 0, 0.15); padding: 0.25rem; position: fixed; bottom: 0; left: 0; right: 0; display: flex; height: 3rem; box-sizing: border-box; backdrop-filter: blur(10px); }
#input { border: none; padding: 0 1rem; flex-grow: 1; border-radius: 2rem; margin: 0.25rem; }
#input:focus { outline: none; }
#form > button { background: #333; border: none; padding: 0 1rem; margin: 0.25rem; border-radius: 3px; outline: none; color: #fff; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages > li { padding: 0.5rem 1rem; }
#messages > li:nth-child(odd) { background: #efefef; }
</style>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>)
<script>
const socket = io();
const form = document.getElementById('form');
const input = document.getElementById('input');
form.addEventListener('submit', (e) => {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});
</script>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" />
<button>Send</button>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" />
<button>Send</button>
</form>
<p id="cos"></p>
</body>
</html>

View File

@@ -5,7 +5,7 @@
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="../js/login.js" defer></script>
<script src="../js/js/login.js" defer></script>
<title>Login</title>
</head>
<body>

View File

@@ -6,7 +6,6 @@ const app = express()
const path = require('path');
const { insertUser, isUserExists, client } = require('./backend/db.js');
const { chat } = require('./backend/socket.js');
const bcrypt = require('bcrypt');
const saltRounds = 10;
@@ -14,6 +13,9 @@ const { Server } = require("socket.io");
const { createServer } = require('node:http');
const server = createServer(app);
const { join } = require('node:path');
const io = new Server(server);
require('dotenv').config()
//TODO change password option will be cool
@@ -22,8 +24,8 @@ require('dotenv').config()
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/static', express.static('public'))
app.use('/static', express.static('frontend'))
app.use('/socket', express.static('node_modules/socket.io/client-dist'));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
@@ -56,6 +58,25 @@ app.get('/', (req, res) => {
}
})
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('connection', (socket) => {
console.log('message: ', + msg);
})
});
io.on('connection', (socket) => {
socket.on('chat message', (msg) => {
console.log('message: ' + msg);
});
});
// run server
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
@@ -111,7 +132,7 @@ async function loginUser(req, res) {
<script>
setTimeout(() =>{
window.location.href = '/';
}, 3000);
}, 1500);
</script>
`);
} else {