当前位置:网站首页>Websocket chat
Websocket chat
2022-07-28 14:06:00 【cc&】
1. Start the backend program ( In the uploaded resources )
2. Front page introduction js
3. Write page
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO chat</title>
<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>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button>
</form>
</body>
<script src="./socket.io.min.js"></script>
<script>
// Get the element
const ul=document.querySelector('#messages')
const form=document.querySelector('#form')
const input=document.querySelector('#input')
// Link to the server
let socket=io('ws://localhost:3000')
// Send a message
form.addEventListener('submit',(e)=>{
e.preventDefault()
const value=input.value
if(value){
// Submit data to server
socket.emit('chat message',value)}
})
// receive messages
socket.on('chat message',(data)=>{
console.log(data);
ul.innerHTML+=`<li>${data}</li>`
// The page scrolls automatically
window.scrollTo(0,document.scrollHeight)
})
</script>
</html>
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置draw_quantiles参数添加指定分位数横线(例如,50%分位数、中位数)
- Poj1860 currency exchange solution
- Leetcode depth first and breadth first traversal
- 对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
- 30 day question brushing plan (III)
- Poj3268 shortest path solution
- 30 day question brushing plan (II)
- DXF读写:标注样式组码中文说明
- RSA用私钥加密数据公钥解密数据(不是签名验证过程)
- Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
猜你喜欢
随机推荐
如何有效进行回顾会议(上)?
盘点操作URL中常用的几个高效API
R language ggplot2 visualization: visualize the scatter diagram and add text labels to the data points in the scatter diagram, using geom of ggrep package_ text_ The rep function avoids overlapping da
redis哨兵机制
第六章 支持向量机
浅谈WebSocket
Security assurance is based on software life cycle -psp application
30天刷题计划(四)
.net for subtraction, intersection and union of complex type sets
【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复
a标签_文件下载(download属性)
数据库系统概论(第5版)补充习题——第一章 绪论
Istio四之故障注入和链路追踪
Long closed period private placement products reappearance industry insiders have different views
P1797 heavy transportation problem solution
TS扫盲大法-基础篇
DXF reading and writing: align the calculation of the position of the dimension text in the middle and above
在 Kubernetes 中部署应用交付服务(第 1 部分)
Poj3268 shortest path solution
Understanding of stack and practical application scenarios







