wip litle front end for chat
This commit is contained in:
6
tests_hugo/chat_node/README.md
Normal file
6
tests_hugo/chat_node/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
https://www.youtube.com/watch?v=o8Fn9BgqoQM
|
||||
|
||||
npm install --save express
|
||||
npm install --save socket.io
|
||||
sudo npm install -g nodemon
|
||||
|
||||
34
tests_hugo/chat_node/index.html
Normal file
34
tests_hugo/chat_node/index.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
|
||||
let socket = io();
|
||||
|
||||
let send = function () {
|
||||
let text = document.getElementById('message').value;
|
||||
socket.emit('chat_message', text);
|
||||
}
|
||||
|
||||
let receive = function (msg) {
|
||||
let li = document.createElement('li');
|
||||
li.innerText = msg;
|
||||
document.getElementById('messages').appendChild(li);
|
||||
}
|
||||
socket.on('chat_message', receive);
|
||||
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<ul id="messages"></ul>
|
||||
<input id="message" type="text"/>
|
||||
<button onclick="send()">send</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
1931
tests_hugo/chat_node/package-lock.json
generated
Normal file
1931
tests_hugo/chat_node/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
7
tests_hugo/chat_node/package.json
Normal file
7
tests_hugo/chat_node/package.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"nodemon": "^2.0.20",
|
||||
"socket.io": "^4.5.3"
|
||||
}
|
||||
}
|
||||
28
tests_hugo/chat_node/server.js
Normal file
28
tests_hugo/chat_node/server.js
Normal file
@@ -0,0 +1,28 @@
|
||||
let app = require('express')();
|
||||
let http = require('http').Server(app);
|
||||
let io = require('socket.io')(http);
|
||||
|
||||
app.get("/", function (req, res) {
|
||||
console.log("req.headers: ");
|
||||
console.log(req.headers);
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
})
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
|
||||
console.log('a user is connected');
|
||||
|
||||
socket.on('disconnect', function () {
|
||||
console.log('a user is disconnected');
|
||||
})
|
||||
|
||||
socket.on('chat_message', function (msg) {
|
||||
console.log('message received: ' + msg);
|
||||
io.emit('chat_message', msg);
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
http.listen(3000, function () {
|
||||
console.log('server running on 3000');
|
||||
})
|
||||
Reference in New Issue
Block a user