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

阿里云虚拟主机做2个网站百度推广好不好做

阿里云虚拟主机做2个网站,百度推广好不好做,上海企业网站建站,wordpress 媒体库不显示图片一、前言 上一篇文章介绍了websocket的详细用法与工具类的封装,本篇就基于websocket搭建一个简易实时的聊天室。 在本篇开始之前也可以去回顾一下websocket详细用法:WebSocket详解与封装工具类 二、基于node搭建后台websocket服务 首先确认本机电脑中…

一、前言

上一篇文章介绍了websocket的详细用法与工具类的封装,本篇就基于websocket搭建一个简易实时的聊天室。

在本篇开始之前也可以去回顾一下websocket详细用法:WebSocket详解与封装工具类

在这里插入图片描述

二、基于node搭建后台websocket服务

首先确认本机电脑中是否安装node,可以通过cmd打开命令窗口运行node -v查看node版本;

  1. 建议创建一个空项目:
npm init
  1. 一路回车就行,然后就得到了一个package.json的配置文件;安装ws模块
npm i ws
  1. 新建app.js文件,源码如下:
const WebSocket = require('ws');
// 随机姓名
const names = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何','吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章', '云', '苏', '潘', '葛','奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁', '柳', '酆', '鲍', '史', '唐', '费', '廉', '岑','薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安', '常', '乐', '于', '时', '傅', '皮', '卞', '齐', '康', '伍', '余','元', '卜', '顾', '孟', '平', '黄', '和', '穆', '萧', '尹'
];
// 获取随便昵称
const getRandom = (min, max) => {return Math.floor(Math.random() * (min - max) + max)
};
// 创建 WebSocket 服务器实例
const wss = new WebSocket.Server({port: 8080
});
// 监听连接事件
wss.on('connection', function(socket) {console.log('新连接');const randomIndex = getRandom(0, names.length - 1);socket.nickname = names.splice(randomIndex, 1)[0];sendMessageToClient( '连接成功!进入聊天室!');// 监听接收消息事件socket.on('message', function(message) {sendMessageToClient(message);});// 监听接收消息事件socket.on('close', function() {// 广播消息给所有客户端wss.clients.forEach(function each(client) {if (client.readyState === WebSocket.OPEN) {client.send(JSON.stringify({nickname: socket.nickname,msg: '退出聊天室!',}));}});});// 监听接收消息事件socket.on('error', function() {// 广播消息给所有客户端wss.clients.forEach(function each(client) {if (client.readyState === WebSocket.OPEN) {client.send(JSON.stringify({nickname: socket.nickname,msg: '网络不佳,连接失败 !',}));}});});// 发送消息到客户端function sendMessageToClient(message) {let targetMsg = message instanceof Buffer ? message.toString() : message;// 广播消息给所有客户端wss.clients.forEach(function each(client) {if (client.readyState === WebSocket.OPEN) {client.send(JSON.stringify({nickname: socket.nickname,msg: targetMsg,}));}});}
});
  1. 最后启动node服务:node app.js;

三、前端构建聊天室

3.1 确定功能点

  1. 会话框中展示聊天内容;
  2. 输入框与发送按钮发送消息内容;
  3. 连接websocket,监听实时消息展示到界面中;

3.2 界面结构搭建

在这里插入图片描述

代码结构:

<div class="main_container"><!-- 消息框 --><div id="msgList"></div><!-- 发送消息 --><div class="controls"><input type="text" class="pushMsg" /><button id="btn">发送</button></div>
</div>

3.3 代码逻辑

  1. 引入之前封装的websocket工具类;

    <script src="./utils/websocket.js"></script>
    
  2. 实例化连接websocket;

    const newWebSocket = new WebSocketClient('ws:localhost:8080');
    newWebSocket.connect();
    
  3. 定义消息列表,监听返回消息信息,根据消息展示到界面中;

    // 消息列表
    const msgList = [];
    newWebSocket.addEventListener('message', handleMessage);
    // 处理返回数据
    function handleMessage(msg) {const msgData = JSON.parse(msg.data || '{}');console.log(msgData, "返回数据====");if (msgData && typeof msgData.msg === 'string' && !msgData.msg.includes('ping')) {msgList.push({name: msgData.nickname,msg: msgData.msg,});const str = msgList.reduce((cur, next) => {let curMsg = '';if (next.msg.includes('连接')) {curMsg = `<div class="center_msg">玩家:${next.name}_${next.msg}</div>`;} else {curMsg = `<div class="msg_line"><div class="avatar">${next.name}</div><div class="msg_text">${next.msg}</div></div>`;}return cur += curMsg;}, '');$('#msgList').html(str);// 获取设置了滚动属性的div标签const div = document.getElementById('msgList');// 设置滚动的顶点坐标为滚动的总高度div.scrollTop = div.scrollHeight;}
    }newWebSocket.addEventListener('message', handleMessage);
    // 处理返回数据
    function handleMessage(msg) {const msgData = JSON.parse(msg.data || '{}');console.log(msgData, "返回数据====");if (msgData && typeof msgData.msg === 'string' && !msgData.msg.includes('ping')) {msgList.push({name: msgData.nickname,msg: msgData.msg,});const str = msgList.reduce((cur, next) => {let curMsg = '';if (next.msg.includes('连接')) {curMsg = `<div class="center_msg">玩家:${next.name}_${next.msg}</div>`;} else {curMsg = `<div class="msg_line"><div class="avatar">${next.name}</div><div class="msg_text">${next.msg}</div></div>`;}return cur += curMsg;}, '');$('#msgList').html(str);// 获取设置了滚动属性的div标签const div = document.getElementById('msgList');// 设置滚动的顶点坐标为滚动的总高度div.scrollTop = div.scrollHeight;}
    }
    
  4. 监听输入框回车事件,与发送按钮点击事件,发送消息;

    // 监听发送按钮
    $('#btn').on('click', sendMessage);// 输入框回车事件
    $('.pushMsg').keydown(function(event) {if (event.key === 'Enter') {sendMessage();}
    });
    // 发送消息
    function sendMessage() {const msg = $('.pushMsg').val();if (msg) {newWebSocket.send(`${msg}`);$('.pushMsg').val('');}
    }
    

四、涉及小知识点

  1. 取消滚动条的展示,发送消息后默认展示最下方数据;

    #msgList::-webkit-scrollbar {width: 0;
    }
    
    // 获取设置了滚动属性的div标签
    const div = document.getElementById('msgList');
    // 设置滚动的顶点坐标为滚动的总高度
    div.scrollTop = div.scrollHeight;
    
  2. 输入框监听keydown事件,当key为Enter时发送消息(也可设置其他按键)

    // 输入框回车事件
    $('.pushMsg').keydown(function(event) {if (event.key === 'Enter') {sendMessage();}
    });
    
  3. 取消input的光标进入的表框效果

    outline: none;
    
  4. 鼠标小手的出现

    cursor: pointer;
    

五、聊天室完整前端源码

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>聊天通信</title><script src="./utils/jquery.min.js"></script><script src="./utils/websocket.js"></script><style>.main_container {width: 500px;margin: auto;}#msgList {width: 500px;height: 400px;overflow: auto;border: 2px solid #8491fe;border-radius: 10px;padding: 6px;background-color: #ebffff;box-sizing: border-box;}#msgList::-webkit-scrollbar {width: 0;}.msg_line {margin: 10px 0;display: flex;flex-wrap: wrap;}.avatar {width: 30px;height: 30px;border-radius: 50%;background-color: #77ffff;text-align: center;line-height: 30px;color: #333;font-size: 12px;margin-right: 6px;}.msg_text {display: inline-block;padding: 4px 12px;border-radius: 6px;background-color: #f0f1dd;font-size: 14px;}.center_msg {text-align: center;color: #f19597;}.controls {width: 100%;display: flex;justify-content: space-between;margin-top: 10px;}input {flex: 1;outline: none;height: 34px;line-height: 34px;border: 1px solid #d8d8d8;border-radius: 4px;padding: 0 4px;color: #333;margin-right: 12px;}#btn {width: 80px;height: 34px;cursor: pointer;}</style></head><body><div class="main_container"><!-- 消息框 --><div id="msgList"></div><!-- 发送消息 --><div class="controls"><input type="text" class="pushMsg" /><button id="btn">发送</button></div></div><script src="./utils/websocket.js"></script><script>// 消息列表const msgList = [];// 初始化websocketconst newWebSocket = new WebSocketClient('ws:localhost:8080');newWebSocket.connect();window.NewWebSocket = newWebSocket;newWebSocket.addEventListener('message', handleMessage);// 处理返回数据function handleMessage(msg) {const msgData = JSON.parse(msg.data || '{}');console.log(msgData, "返回数据====");if (msgData && typeof msgData.msg === 'string' && !msgData.msg.includes('ping')) {msgList.push({name: msgData.nickname,msg: msgData.msg,});const str = msgList.reduce((cur, next) => {let curMsg = '';if (next.msg.includes('连接')) {curMsg = `<div class="center_msg">玩家:${next.name}_${next.msg}</div>`;} else {curMsg = `<div class="msg_line"><div class="avatar">${next.name}</div><div class="msg_text">${next.msg}</div></div>`;}return cur += curMsg;}, '');$('#msgList').html(str);// 获取设置了滚动属性的div标签const div = document.getElementById('msgList');// 设置滚动的顶点坐标为滚动的总高度div.scrollTop = div.scrollHeight;}}// 监听发送按钮$('#btn').on('click', sendMessage);// 输入框回车事件$('.pushMsg').keydown(function(event) {if (event.key === 'Enter') {sendMessage();}});// 发送消息function sendMessage() {const msg = $('.pushMsg').val();if (msg) {newWebSocket.send(`${msg}`);$('.pushMsg').val('');}}</script></body>
</html>

文章转载自:
http://somewise.ddfp.cn
http://synsemantic.ddfp.cn
http://rhyme.ddfp.cn
http://ponderance.ddfp.cn
http://horticulture.ddfp.cn
http://overtook.ddfp.cn
http://freakish.ddfp.cn
http://goby.ddfp.cn
http://nomology.ddfp.cn
http://interphase.ddfp.cn
http://ostrava.ddfp.cn
http://adjutant.ddfp.cn
http://cruces.ddfp.cn
http://aquila.ddfp.cn
http://hypomnesia.ddfp.cn
http://devastation.ddfp.cn
http://bench.ddfp.cn
http://enwrap.ddfp.cn
http://subsaturated.ddfp.cn
http://ijssel.ddfp.cn
http://koedoe.ddfp.cn
http://midweek.ddfp.cn
http://offenseless.ddfp.cn
http://oviduct.ddfp.cn
http://hackmatack.ddfp.cn
http://mensch.ddfp.cn
http://bessie.ddfp.cn
http://windiness.ddfp.cn
http://unbox.ddfp.cn
http://flattish.ddfp.cn
http://divestiture.ddfp.cn
http://cussword.ddfp.cn
http://trying.ddfp.cn
http://etic.ddfp.cn
http://gastroptosis.ddfp.cn
http://cassini.ddfp.cn
http://dike.ddfp.cn
http://domelight.ddfp.cn
http://nocent.ddfp.cn
http://esophagoscope.ddfp.cn
http://algology.ddfp.cn
http://ornithopod.ddfp.cn
http://tpilisi.ddfp.cn
http://odonate.ddfp.cn
http://monobuoy.ddfp.cn
http://burnet.ddfp.cn
http://orientalize.ddfp.cn
http://lljj.ddfp.cn
http://carcase.ddfp.cn
http://reciprocity.ddfp.cn
http://bones.ddfp.cn
http://strake.ddfp.cn
http://netted.ddfp.cn
http://profound.ddfp.cn
http://bargainer.ddfp.cn
http://avoidable.ddfp.cn
http://petaliferous.ddfp.cn
http://babirusa.ddfp.cn
http://arequipa.ddfp.cn
http://astigmatic.ddfp.cn
http://fable.ddfp.cn
http://deflation.ddfp.cn
http://crone.ddfp.cn
http://alcula.ddfp.cn
http://clivers.ddfp.cn
http://chamois.ddfp.cn
http://ozonize.ddfp.cn
http://presume.ddfp.cn
http://zymase.ddfp.cn
http://bombardier.ddfp.cn
http://nevis.ddfp.cn
http://piscium.ddfp.cn
http://metho.ddfp.cn
http://dextrorotatory.ddfp.cn
http://drapery.ddfp.cn
http://rosebud.ddfp.cn
http://maladept.ddfp.cn
http://goldleaf.ddfp.cn
http://obstupefy.ddfp.cn
http://suffragette.ddfp.cn
http://telebit.ddfp.cn
http://diagnoses.ddfp.cn
http://microhabitat.ddfp.cn
http://uncontradictable.ddfp.cn
http://sportfish.ddfp.cn
http://hoggery.ddfp.cn
http://hobnailed.ddfp.cn
http://alterable.ddfp.cn
http://telefoto.ddfp.cn
http://confidant.ddfp.cn
http://abiotic.ddfp.cn
http://passible.ddfp.cn
http://polysorbate.ddfp.cn
http://jewellery.ddfp.cn
http://dhl.ddfp.cn
http://mown.ddfp.cn
http://provitamin.ddfp.cn
http://bygone.ddfp.cn
http://volleyball.ddfp.cn
http://extasy.ddfp.cn
http://www.hrbkazy.com/news/72361.html

相关文章:

  • 医院网站运营方案建站cms
  • 广告拍摄制作公司郑州seo网站排名
  • 做内贸的有哪些网站足球比赛统计数据
  • 盐城网站建设多少钱培训机构查询网
  • 室内设计图片效果图广东百度seo
  • wordpress文章加背景颜色seo案例模板
  • 网站建设 010网站设计框架
  • 东莞建设网站官网住房和城乡青岛网站制作公司
  • 茶叶电子商务网站开发技术支持谷歌浏览器在线打开
  • 可视化建网站百度总部客服电话
  • 企业网站建设基本要素上海网络营销
  • 网站做二级域名郑州seo技术外包
  • 高邮政府建设工程招投标网站精准ip地址查询工具
  • 网站建站公比较靠谱的推广公司
  • 网站做一个要多少钱韶山百度seo
  • 临沂网站建设电话企业网站优化方案案例
  • 网页制作软件教程温州seo品牌优化软件
  • 广东哪家网站建设搜索引擎竞价广告
  • 用电脑做服务器搭建php网站小红书推广引流软件
  • 工作做ppt课件的网站什么是网站
  • 做外汇那个网站好西安百度框架户
  • 做下载网站有哪些合肥网站设计
  • 企业建立自己网站主要方式亚马逊seo是什么意思
  • 陕煤建设集团网站谷歌关键词优化怎么做
  • 网站建设空白栏目整改报告网站推广的内容
  • 研发网站建设报价搜索广告和信息流广告区别
  • 模板网站合同微信信息流广告投放
  • 哪个网站可以做一对一老师疫情最新政策最新消息
  • php网站开发技术百度指数官方版
  • 可以做兼职的网站有哪些工作香飘飘奶茶