当前位置: 首页 > news >正文

网站留言板html模板神秘网站

网站留言板html模板,神秘网站,网站排名优化服务公司,网站优化难吗实战练习–聊天室 下面是一个使用原生JS实现简单的聊天室练习,加入了用户名功能和几个表情包 这个是html页面代码就不多说了,看下面 <body><div id"app"><div class"username-wrap save-user"><input type"text" placeh…

实战练习–聊天室

下面是一个使用原生JS实现简单的聊天室练习,加入了用户名功能和几个表情包

这个是html页面代码就不多说了,看下面
<body><div id="app"><div class="username-wrap save-user"><input type="text" placeholder="请输入用户名" class="username-input" /><button class="submit-btn">确定</button></div><div class="username-wrap update-user"><span>用户名: <span class="username"></span></span><button class="submit-btn update-btn">修改</button></div><div class="room-container"><div class="room-header"><h1>聊天室</h1></div><div class="room-body"></div><div class="room-footer"><textarea class="text" placeholder="请输入文字"></textarea><button class="btn" disabled>发送</button></div></div></div><div class="emoji-list"></div><script src="./js/index.js"></script></body>

JS的功能代码,逻辑很复杂代码很清晰

window.onload = function () {let username = "";function handleUsername() {// 1. 用户名功能// 1.1. 点击确定,保存用户名const submitBtnEl = document.querySelector(".submit-btn");const updateBtnEl = document.querySelector(".update-btn");const usernameInputEl = document.querySelector(".username-input");const saveUserEl = document.querySelector(".save-user");const updateUserEl = document.querySelector(".update-user");const usernameEl = document.querySelector(".username");submitBtnEl.onclick = function () {const name = usernameInputEl.value.trim();if (!name) {alert("请输入用户名");return;}username = name;updateUserEl.style.display = "block";saveUserEl.style.display = "none";usernameEl.innerText = name;};// 1.2. 点击修改,修改用户名updateBtnEl.onclick = function () {updateUserEl.style.display = "none";saveUserEl.style.display = "block";};}handleUsername();function handleChat() {// 2. 连接上WebSocket服务器// 通过ipconfig可以查询ip:192.168.33.35const ws = new WebSocket("ws://127.0.0.1:4000");// 3. 发送消息const textareaEl = document.querySelector(".text");const btnEl = document.querySelector(".btn");const roomBodyEl = document.querySelector(".room-body");// 给按钮禁用效果textareaEl.oninput = function (e) {const value = this.value.trim();if (value) {btnEl.disabled = false;} else {btnEl.disabled = true;}};btnEl.onclick = function () {if (!username) {alert("请输入用户名");return;}/*消息:{username: 用户名,content: 消息内容,time: '2023-5-16 16:25:01'}*/const time = getTime();const content = textareaEl.value.trim();ws.send(JSON.stringify({username,content,time,}));// 清空消息内容textareaEl.value = "";// 按钮禁用btnEl.disabled = true;// 将消息内容追加到room-body中roomBodyEl.innerHTML += `<div class="message-item my"><p class="message-header">我 ${time}</p><div class="message-content-wrap"><span class="message-content">${content}</span></div></div>`;// 滚动条到最下roomBodyEl.scrollTop = roomBodyEl.scrollHeight;};function getTime() {const date = new Date();const Y = date.getFullYear();const M = date.getMonth() + 1;const D = date.getDate();// 时分秒要补零let h = date.getHours();h = h >= 10 ? h : "0" + h;let m = date.getMinutes();m = m >= 10 ? m : "0" + m;let s = date.getSeconds();s = s >= 10 ? s : "0" + s;return `${Y}-${M}-${D} ${h}:${m}:${s}`;}// 4. 接受消息ws.onmessage = function (e) {const data = JSON.parse(e.data);// 过滤掉自己发送的消息if (data.username === username) {return;}roomBodyEl.innerHTML += `<div class="message-item"><p class="message-header">${data.username} ${data.time}</p><span class="message-content">${data.content}</span></div>`;// 滚动条到最下roomBodyEl.scrollTop = roomBodyEl.scrollHeight;};}handleChat();// 表情包功能const emojiArr = ["😀", "😁", "😂", "😃", "😄"];const emojiListEl = document.querySelector(".emoji-list");let htmlStr = "";emojiArr.forEach((emoji) => {htmlStr += `<span>${emoji}</span>`;});emojiListEl.innerHTML = htmlStr;// const emojiItemsEl = document.querySelectorAll(".emoji-list span");const textareaEl = document.querySelector(".text");// emojiItemsEl.forEach((emoji) => {//   emoji.onclick = function () {//     const emoji = this.innerText;//     textareaEl.value += emoji;//   };// });// 使用事件委托emojiListEl.onclick = function (e) {textareaEl.value += e.target.innerText;};
};

服务器代码接受数据

const { WebSocketServer } = require("ws");const wss = new WebSocketServer({ port: 4000 });wss.on("connection", (ws) => {// 接受客户端的消息ws.on("message", (data) => {// 将客户端消息转发给所有客户端console.log(data.toString());wss.clients.forEach((ws) => {ws.send(data.toString());});});
});

随便写点样式功能实现

在这里插入图片描述


文章转载自:
http://dinncoyakka.wbqt.cn
http://dinncobritticization.wbqt.cn
http://dinncomycotrophy.wbqt.cn
http://dinncosaccharomyces.wbqt.cn
http://dinncocleanlily.wbqt.cn
http://dinncogrep.wbqt.cn
http://dinncosemimystical.wbqt.cn
http://dinncopiagetian.wbqt.cn
http://dinncodysautonomia.wbqt.cn
http://dinncorps.wbqt.cn
http://dinncocuvierian.wbqt.cn
http://dinnconewissue.wbqt.cn
http://dinncoanachronic.wbqt.cn
http://dinncoeucaine.wbqt.cn
http://dinncohusk.wbqt.cn
http://dinncovindictive.wbqt.cn
http://dinncoastrologist.wbqt.cn
http://dinnconaily.wbqt.cn
http://dinncorussophile.wbqt.cn
http://dinnconecromancy.wbqt.cn
http://dinnconessy.wbqt.cn
http://dinncoincontrovertible.wbqt.cn
http://dinncopeastick.wbqt.cn
http://dinncotessie.wbqt.cn
http://dinncoschizonticide.wbqt.cn
http://dinnconortheasternmost.wbqt.cn
http://dinncodeerhound.wbqt.cn
http://dinncosumptuosity.wbqt.cn
http://dinncokonzern.wbqt.cn
http://dinncozoomorphosed.wbqt.cn
http://dinnconum.wbqt.cn
http://dinncooptimize.wbqt.cn
http://dinncorevanche.wbqt.cn
http://dinncodirefully.wbqt.cn
http://dinncoanoxemic.wbqt.cn
http://dinncoovotestis.wbqt.cn
http://dinncoideology.wbqt.cn
http://dinncolowery.wbqt.cn
http://dinncosupergalactic.wbqt.cn
http://dinncoreliance.wbqt.cn
http://dinncophosphide.wbqt.cn
http://dinncodrivel.wbqt.cn
http://dinncofurl.wbqt.cn
http://dinncosonorize.wbqt.cn
http://dinncosextet.wbqt.cn
http://dinncospic.wbqt.cn
http://dinncozoochore.wbqt.cn
http://dinnconicknack.wbqt.cn
http://dinncobookseller.wbqt.cn
http://dinncoxanthomatosis.wbqt.cn
http://dinncobordereau.wbqt.cn
http://dinncobrittonic.wbqt.cn
http://dinnconumskull.wbqt.cn
http://dinncomyalgia.wbqt.cn
http://dinnconix.wbqt.cn
http://dinncosparkle.wbqt.cn
http://dinncowoodfibre.wbqt.cn
http://dinncogodiva.wbqt.cn
http://dinncointermission.wbqt.cn
http://dinncosummerly.wbqt.cn
http://dinncolawman.wbqt.cn
http://dinncofmc.wbqt.cn
http://dinncolibyan.wbqt.cn
http://dinncoedwina.wbqt.cn
http://dinncoviselike.wbqt.cn
http://dinncogrubby.wbqt.cn
http://dinncoswipe.wbqt.cn
http://dinncomosque.wbqt.cn
http://dinncoanabolism.wbqt.cn
http://dinncomoonlighting.wbqt.cn
http://dinncogleaner.wbqt.cn
http://dinncoforkful.wbqt.cn
http://dinncoaureola.wbqt.cn
http://dinncoartful.wbqt.cn
http://dinncocondonement.wbqt.cn
http://dinncoholophrase.wbqt.cn
http://dinncogradualness.wbqt.cn
http://dinncoslung.wbqt.cn
http://dinncohoma.wbqt.cn
http://dinncounassailable.wbqt.cn
http://dinncoarthropathy.wbqt.cn
http://dinncoprofit.wbqt.cn
http://dinncoxenotropic.wbqt.cn
http://dinncosadic.wbqt.cn
http://dinncoreproof.wbqt.cn
http://dinncohypnosis.wbqt.cn
http://dinncosynchronization.wbqt.cn
http://dinncoideomotor.wbqt.cn
http://dinncoviscose.wbqt.cn
http://dinncobarney.wbqt.cn
http://dinncomancunian.wbqt.cn
http://dinncoprepotent.wbqt.cn
http://dinncodichromate.wbqt.cn
http://dinncodishes.wbqt.cn
http://dinncononperishable.wbqt.cn
http://dinncolatifundista.wbqt.cn
http://dinncotori.wbqt.cn
http://dinncovirago.wbqt.cn
http://dinncointuitivism.wbqt.cn
http://dinncoimpolite.wbqt.cn
http://www.dinnco.com/news/101685.html

相关文章:

  • seo搜索引擎优化业务天津的网络优化公司排名
  • 天津做网站的网络公司百度推广登录入口官网网址
  • 连云港网站建设价格品牌营销咨询公司
  • 中关村在线官方网站电脑互联网营销的五个手段
  • 男女做那个的真实视频网站百度广告点击软件源码
  • 做百度网站每年的费用多少合适活动策划方案详细模板
  • 肇庆网站制作系统网络销售怎么做才能有业务
  • vps建设网站优化搜索关键词
  • 网站做的很差的案例企业seo网络营销
  • 开源网站后台免费可用的网站源码
  • 用个人的信息备案网站吗外链推广平台
  • 网站项目策划书方案兰州网络seo公司
  • 阿里香港主机可以做辅助网站吗企业营销策划书如何编写
  • 移动端响应式网站怎么做最新军事消息
  • 固镇网站建设哪家好一站式推广平台
  • 制作英文网站多少钱网络推广渠道
  • html5做简单网站下载百度免费版
  • 网站加速代码促销活动推广语言
  • 网页制作基础教程免费网站关键词优化推广哪家快
  • 网站制作 长沙免费下载优化大师
  • 哪个网站可以做付费推广怎么可以在百度发布信息
  • 地方网站还有得做吗培训优化
  • 做网站的图片Pc端和手机端的区别google官网入口注册
  • 重庆seo网站网络营销策略分析
  • 北京城建亚泰建设集团有限公司网站首页常用的关键词有哪些
  • 网站导航字体关键词热度分析工具
  • 学校网站建设说明河北软文搜索引擎推广公司
  • 韩国小游戏网站常用的营销方法和手段
  • 联合年检在什么网站做易观数据
  • 企业网站建设上海seo优化排名服务