当前位置:网站首页>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>
边栏推荐
- 记一次COOKIE的伪造登录
- 30 day question brushing plan (IV)
- leetcode(442)数组中重复的数据
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的边框颜色
- 【LVGL事件(Events)】事件代码
- Record a fake login of cookie
- .net for subtraction, intersection and union of complex type sets
- 7. Dependency injection
- Analyzing the principle of DNS resolution in kubernetes cluster
- Several efficient APIs commonly used in inventory operation URL
猜你喜欢

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

在centos中安装mysql5.7.36

Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function

What is the reason why the words behind word disappear when typing? How to solve it?

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response

【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)

IP黑白名单

产品经理:岗位职责表

7.依赖注入

什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
随机推荐
Security assurance is based on software life cycle -istio authentication mechanism
Holes in [apue] files
DDoS protection with iptables
P1797重型运输 题解
30 day question brushing plan (II)
MySQL开发技巧——视图
leetcode(442)数组中重复的数据
R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
Generation of tables and contingency tables (cross tables) of R language factor data: use the summary function to analyze the list, view the chi square test results, and judge whether the two factor v
Qt5开发从入门到精通——第一篇概述
Graph traversal (BFS & DFS basis)
SLAM论文合集
协同办公工具:在线白板初起步,在线设计已红海
Deploy application delivery services in kubernetes (Part 1)
Poj3268 shortest path solution
修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
Operator3 - design an operator
MySql5.5之后的默认存储引擎为InnoDB。
盘点操作URL中常用的几个高效API
To build agile teams, these methods are indispensable