当前位置:网站首页>webSocket聊天
webSocket聊天
2022-07-28 13:08:00 【cc&】
1.启动后端程序(在上传的资源中)
2.前端页面引入js
3.编写页面
<!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>
// 得到元素
const ul=document.querySelector('#messages')
const form=document.querySelector('#form')
const input=document.querySelector('#input')
// 与服务器建立链接
let socket=io('ws://localhost:3000')
// 发送消息
form.addEventListener('submit',(e)=>{
e.preventDefault()
const value=input.value
if(value){
// 提交数据到服务器
socket.emit('chat message',value)}
})
// 接收消息
socket.on('chat message',(data)=>{
console.log(data);
ul.innerHTML+=`<li>${data}</li>`
// 页面自动滚动
window.scrollTo(0,document.scrollHeight)
})
</script>
</html>
边栏推荐
- R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize violin diagrams, set the palette parameter, and customize the border colors of violin diagrams at different l
- SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
- SQL daily practice (Niuke new question bank) - day 4: advanced operators
- 111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server
- 产品经理:岗位职责表
- POJ3275 Ranking the Cows题解
- JWT login authentication + token automatic renewal scheme, well written!
- The domestic API management tool eolink is very easy to use, creating an efficient research and development tool
- R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
- 你真的了解esModule吗
猜你喜欢

Security assurance is based on software life cycle -psp application

【LVGL事件(Events)】事件在不同组件上的应用(一)

Record a fake login of cookie

DXF读写:对齐尺寸标注文字居中、上方的位置计算

IP黑白名单

Operator3 - design an operator

Multithreading and high concurrency (III) -- source code analysis AQS principle

多线程与高并发(三)—— 源码解析 AQS 原理

30天刷题训练(一)

基于NoneBot2的qq机器人配置记录
随机推荐
TS扫盲大法-基础篇
目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
Product Manager: job responsibility table
Dojnoip201708 cheese solution
Using fail2ban to protect web servers from DDoS Attacks
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
Security assurance is based on software life cycle -istio authentication mechanism
在 Kubernetes 中部署应用交付服务(第 1 部分)
URL related knowledge points
Algorithm --- different paths (kotlin)
安全保障基于软件全生命周期-Istio的认证机制
leetcode-深度优先与广度优先遍历
【LVGL事件(Events)】事件代码
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
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
TS literacy method - Basic chapter
30天刷题训练(一)
IntersectionObserver交叉观察器
redis哨兵机制