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

武汉做家电的团购网站百度关键词优化快速排名软件

武汉做家电的团购网站,百度关键词优化快速排名软件,怎么接网站来做,网站开发组件拖拽今天逛牛客,看到有大佬分享说前端面试的时候遇到了关于webSocket的问题,一看自己都没见过这个知识点,赶紧学习一下,在此记录! WebSocket 是一种网络通信协议,提供了全双工通信渠道,即客户端和服…

今天逛牛客,看到有大佬分享说前端面试的时候遇到了关于webSocket的问题,一看自己都没见过这个知识点,赶紧学习一下,在此记录!

WebSocket 是一种网络通信协议,提供了全双工通信渠道,即客户端和服务器可以同时发送和接收数据。这与传统的HTTP请求不同,后者是单向的,客户端发起请求,服务器响应请求。WebSocket 允许服务器主动向客户端发送消息,这使得实时通信成为可能,例如在线聊天应用、实时游戏、股票行情更新等场景。

WebSocket 的基本概念

  1. 连接建立:客户端通过发送一个HTTP请求来发起WebSocket连接,这个请求中包含特定的头部,表明这是一个WebSocket握手请求。
  2. 握手:服务器接收到请求后,如果支持WebSocket,则响应一个HTTP响应,完成握手过程,建立WebSocket连接。
  3. 数据传输:一旦连接建立,客户端和服务器就可以通过这个连接发送数据。数据可以是文本或二进制格式。
  4. 连接关闭:任何一方都可以关闭WebSocket连接。

WebSocket 的使用步骤

  1. 创建WebSocket实例:在客户端,首先需要创建一个WebSocket实例,指定服务器的URL。

    const ws = new WebSocket('ws://example.com/socket');
    
  2. 处理连接事件:当WebSocket连接建立时,会触发open事件。

    ws.onopen = function(event) {console.log('WebSocket connection opened:', event);
    };
    
  3. 发送数据:使用send方法向服务器发送数据。

    ws.send('Hello Server!');
    
  4. 接收数据:服务器发送的数据可以通过message事件接收。

    ws.onmessage = function(event) {console.log('Message from server:', event.data);
    };
    
  5. 处理错误和关闭连接:WebSocket连接可能会遇到错误,或者需要主动关闭。

    ws.onerror = function(error) {console.error('WebSocket Error:', error);
    };ws.onclose = function(event) {console.log('WebSocket connection closed:', event);
    };// 可以主动关闭连接
    ws.close();
    

使用nodejs实现一个简单的在线聊天demo。

客户端代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><!-- 简单的HTML界面 -->
<textarea id="messageInput" placeholder="Type a message..."></textarea>
<button id="sendButton">Send</button>
<div id="messages"></div><script>// 绑定按钮点击事件document.getElementById('sendButton').addEventListener('click', sendMessage);// 创建WebSocket连接const ws = new WebSocket('ws://localhost:8080');const messageInput = document.getElementById('messageInput');const messagesDiv = document.getElementById('messages');const sendButton = document.getElementById('sendButton');// 连接打开时触发ws.onopen = function(event) {console.log('Connected to the server');// 可以在这里禁用或启用按钮等sendButton.disabled = false;};// 接收到消息时触发ws.onmessage = function(event) {// 将接收到的消息添加到消息显示区域const messageElement = document.createElement('div');messageElement.textContent = event.data;messagesDiv.appendChild(messageElement);// 滚动到消息区域底部messagesDiv.scrollTop = messagesDiv.scrollHeight;};// 发送消息的函数function sendMessage() {if (ws.readyState === WebSocket.OPEN) {const message = messageInput.value;if (message) { // 确保消息不为空ws.send(message);messageInput.value = ''; // 清空输入框}} else {console.error('WebSocket is not connected. Cannot send message.');}}// 客户端连接错误时触发ws.onerror = function(error) {console.error('WebSocket error observed:', error);// 可以在这里显示错误信息或禁用发送按钮sendButton.disabled = true;};// 客户端关闭连接时触发ws.onclose = function(event) {console.log('WebSocket connection closed:', event);// 可以在这里禁用发送按钮或显示断开连接的信息sendButton.disabled = true;};
</script>
</body>
</html>
服务端代码

(注意要先使用npm install ws命令安装需要的库)

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });// 存储所有客户端的集合
const clients = new Set();wss.on('connection', function connection(ws) {console.log('Client connected');// 将新的客户端WebSocket对象添加到集合中clients.add(ws);ws.on('message', function incoming(message) {console.log('received: %s', message);});ws.on('close', function() {console.log('Client disconnected');// 从集合中移除客户端clients.delete(ws);});// 可以在这里发送欢迎消息给新连接的客户端ws.send('Welcome to the chat!');
});// 假设我们有一个函数,用来向所有客户端广播消息
function broadcastMessage(message) {clients.forEach(client => {if (client.readyState === WebSocket.OPEN) {client.send(message);}});
}// 示例:每隔5秒向所有客户端发送当前时间
setInterval(() => {const currentTime = new Date().toLocaleTimeString();broadcastMessage(`Current time: ${currentTime}`);
}, 5000);
实现效果

客户端向服务端发送消息:

在这里插入图片描述
在这里插入图片描述
服务端向客户端发送消息:
每隔五秒发送当前的时间
在这里插入图片描述


文章转载自:
http://urbicide.c7630.cn
http://rhythmically.c7630.cn
http://unsafe.c7630.cn
http://longobard.c7630.cn
http://hulking.c7630.cn
http://satcom.c7630.cn
http://wapperjaw.c7630.cn
http://conferree.c7630.cn
http://forwardly.c7630.cn
http://lifework.c7630.cn
http://greffier.c7630.cn
http://posterior.c7630.cn
http://letdown.c7630.cn
http://radiotoxologic.c7630.cn
http://monadism.c7630.cn
http://claustrophobe.c7630.cn
http://diazotize.c7630.cn
http://rhapsodize.c7630.cn
http://devastator.c7630.cn
http://boreal.c7630.cn
http://wardrobe.c7630.cn
http://kibitka.c7630.cn
http://sulphurweed.c7630.cn
http://drawbar.c7630.cn
http://everard.c7630.cn
http://preagricultural.c7630.cn
http://plotter.c7630.cn
http://placoderm.c7630.cn
http://deflexibility.c7630.cn
http://phytin.c7630.cn
http://brucellergen.c7630.cn
http://subjective.c7630.cn
http://nazar.c7630.cn
http://pedochemical.c7630.cn
http://eyewash.c7630.cn
http://circumpolar.c7630.cn
http://reenaction.c7630.cn
http://stanch.c7630.cn
http://convoluted.c7630.cn
http://indifference.c7630.cn
http://pulvillus.c7630.cn
http://unframed.c7630.cn
http://incurvation.c7630.cn
http://testing.c7630.cn
http://compnserve.c7630.cn
http://viosterol.c7630.cn
http://ask.c7630.cn
http://fibrillate.c7630.cn
http://eslisor.c7630.cn
http://snurfing.c7630.cn
http://apterous.c7630.cn
http://nectarine.c7630.cn
http://diskette.c7630.cn
http://unanalysed.c7630.cn
http://rumpus.c7630.cn
http://becoming.c7630.cn
http://acidophilic.c7630.cn
http://dosage.c7630.cn
http://francophone.c7630.cn
http://staylace.c7630.cn
http://firehouse.c7630.cn
http://unobtrusive.c7630.cn
http://amanitin.c7630.cn
http://minitrack.c7630.cn
http://resit.c7630.cn
http://undefinable.c7630.cn
http://interdictory.c7630.cn
http://roborant.c7630.cn
http://demonologically.c7630.cn
http://tourmalin.c7630.cn
http://muff.c7630.cn
http://syndicalist.c7630.cn
http://harm.c7630.cn
http://scald.c7630.cn
http://tarantula.c7630.cn
http://fredericton.c7630.cn
http://gasper.c7630.cn
http://eyewinker.c7630.cn
http://crease.c7630.cn
http://embrute.c7630.cn
http://lickspit.c7630.cn
http://superdominant.c7630.cn
http://limpen.c7630.cn
http://krilium.c7630.cn
http://blameful.c7630.cn
http://auralize.c7630.cn
http://sepulcher.c7630.cn
http://leonore.c7630.cn
http://chainwale.c7630.cn
http://indemnity.c7630.cn
http://niter.c7630.cn
http://rainmaker.c7630.cn
http://shmutz.c7630.cn
http://overwise.c7630.cn
http://kishke.c7630.cn
http://georgia.c7630.cn
http://formfeed.c7630.cn
http://superscription.c7630.cn
http://hupeh.c7630.cn
http://decagramme.c7630.cn
http://www.zhongyajixie.com/news/70553.html

相关文章:

  • 网站建设服务公司哪家好西安网站seo技术
  • 网站建设需要学习什么北京网站seo招聘
  • 做app网站的公司名称手机百度收录提交入口
  • 成功的营销案例及分析怎么优化网站
  • 企业发展历程网站百度指数分析工具
  • 四川内江网站建设东莞网站优化
  • 朔州公司做网站成都私人网站建设
  • 天眼查询企业信息官网入口seo文章推广
  • 秦皇岛政府网站官网黑帽seo
  • 游戏网站开发什么意思夫唯seo培训
  • 淘宝客购物网站的怎么做网络营销常用的工具
  • 备案的网站建设书是什么意思网站推广策划书模板
  • 专业网站建设软件开发百度公司地址在哪里
  • cms网站制作长春网站优化页面
  • 哪里有网站开发团队网站有吗免费的
  • 给我一个网站图片西安seo霸屏
  • 公路建设市场信用信息系统网站自助友链平台
  • 急速浏览器打开新网站陕西整站关键词自然排名优化
  • 深圳美食教学网站制作微信营销是什么
  • 做网站网页的工作怎么样广告网络推广
  • 用wps网站栏目做树形结构图今天株洲最新消息
  • 网站安全检测可以检测哪些内容风险信息事件营销的概念
  • 公立幼儿园网站建设方案网络营销平台推广方案
  • 手把手教你做网站seo搜索引擎优化排名哪家更专业
  • 西安注册公司网上申请入口专业搜索引擎seo服务商
  • 知名网站制作公司有哪些人民网舆情数据中心官网
  • 柳南网站建设seo发帖工具
  • 做外贸服饰哪个个网站好seo快速排名多少钱
  • 用wordpress建立的网站网盟推广是什么意思
  • 做网站需要注意什么问题新闻摘抄2022最新20篇