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

重庆网站房地产google play下载官方版

重庆网站房地产,google play下载官方版,企业管理培训课程视频,网站建设时送的ppt方案成品图: 对WebSocket的理解(在使用之前建议先了解Tcp,三次握手,四次挥手 ): 首先页面与WebSocket建立连接、向WebSocket发送信息、后端WebSocket向所有连接上WebSoket的客户端发送当前信息。 推荐浏览网站…

成品图: 

 对WebSocket的理解(在使用之前建议先了解Tcp,三次握手,四次挥手 ):

        首先页面与WebSocket建立连接、向WebSocket发送信息、后端WebSocket向所有连接上WebSoket的客户端发送当前信息。

推荐浏览网站:WebSocket 是什么?你需要知道的一切

第一步:在后端引入WebSocket依赖 

        <!--        WebSocket         --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>

第二步:在后端配置WebSocket 

package cn.ryanfan.virtulab_back.config;import cn.ryanfan.virtulab_back.websocket.ChatHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {@Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {registry.addHandler(new ChatHandler(), "/chat").setAllowedOrigins("*");}
}

第三步:建立WebSocket自定义支持 

package cn.ryanfan.virtulab_back.websocket;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;import java.util.ArrayList;
import java.util.List;
@Slf4j
public class ChatHandler extends TextWebSocketHandler {private final List<WebSocketSession> sessions = new ArrayList<>();@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {sessions.add(session);}@Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {for (WebSocketSession s : sessions) {if (s.isOpen()) {s.sendMessage(message);}}}@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {sessions.remove(session);}
}

 第四步:在前端开启WebSocket通信

<template><div class="chat-container"><div class="chat-header"><h3>在线聊天</h3></div><div class="chat-messages"><!-- 显示消息列表 --><divv-for="(message, index) in messages":key="index"class="message-item":class="{'my-message': message.sender === currentUser}"><strong>{{ message.sender }}:</strong><div class="message-content">{{ message.content }}</div></div></div><div class="chat-input"><inputv-model="newMessage"@keyup.enter="sendMessage"type="text"placeholder="输入消息"class="message-input"/><button @click="sendMessage" class="send-button">发送</button></div></div>
</template><script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';interface Message {sender: string;content: string;
}// 消息列表
const messages = ref<Message[]>([]);// 当前用户输入的消息
const newMessage = ref('');// 假设的发送方
const currentUser = 'User1';// WebSocket 对象
let socket: WebSocket | null = null;// 连接 WebSocket,并处理接收和发送消息的逻辑
const connectWebSocket = () => {socket = new WebSocket('ws://localhost:8667/VirtuLab_back/chat'); // 连接到后端 WebSocket// WebSocket 打开时触发socket.onopen = () => {console.log('WebSocket 连接已建立');};// 接收 WebSocket 消息时触发socket.onmessage = (event: MessageEvent) => {const data = JSON.parse(event.data); // 假设收到的消息是 JSON 格式console.log('WebSocket 对话已建立');console.log(data)messages.value.push({ sender: data.sender, content: data.content });};// WebSocket 关闭时触发socket.onclose = () => {console.log('WebSocket 连接已关闭');};// WebSocket 出现错误时触发socket.onerror = (error) => {console.error('WebSocket 错误:', error);};
};// 发送消息
const sendMessage = () => {if (newMessage.value.trim() !== '' && socket && socket.readyState === WebSocket.OPEN) {const message = {sender: currentUser,content: newMessage.value};socket.send(JSON.stringify(message)); // 发送 JSON 格式的消息到服务器newMessage.value = ''; // 清空输入框}
};// 当组件挂载时连接 WebSocket
onMounted(() => {connectWebSocket();
});// 当组件卸载时关闭 WebSocket
onUnmounted(() => {if (socket) {socket.close();}
});
</script><style scoped>
.chat-container {width: 400px;border: 1px solid #ccc;border-radius: 8px;display: flex;flex-direction: column;justify-content: space-between;height: 500px;box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);background-color: #ffffff;
}.chat-header {padding: 15px;background-color: #007bff;color: white;text-align: center;border-top-left-radius: 8px;border-top-right-radius: 8px;font-weight: bold;
}.chat-messages {flex: 1;padding: 15px;overflow-y: auto;background-color: #f9f9f9;border-bottom: 1px solid #ddd;
}.message-item {margin-bottom: 10px;padding: 10px;border-radius: 8px;
}.my-message {background-color: #007bff;color: white;align-self: flex-end;
}.message-content {margin-top: 5px;
}.chat-input {display: flex;padding: 10px;background-color: #f1f1f1;border-bottom-left-radius: 8px;border-bottom-right-radius: 8px;
}.message-input {flex: 1;padding: 10px;border: 1px solid #ccc;border-radius: 4px;margin-right: 10px;transition: border-color 0.3s;
}.message-input:focus {border-color: #007bff;outline: none;
}.send-button {padding: 10px;background-color: #007bff;color: white;border: none;border-radius: 4px;cursor: pointer;transition: background-color 0.3s;
}.send-button:hover {background-color: #0056b3;
}
</style>


文章转载自:
http://dinncospringbuck.zfyr.cn
http://dinncomanorial.zfyr.cn
http://dinncoperdue.zfyr.cn
http://dinncoportreeve.zfyr.cn
http://dinncovauntingly.zfyr.cn
http://dinncopsychosurgeon.zfyr.cn
http://dinncospermatophorous.zfyr.cn
http://dinncofeathering.zfyr.cn
http://dinncocatalan.zfyr.cn
http://dinncopute.zfyr.cn
http://dinncobarbasco.zfyr.cn
http://dinncohadramaut.zfyr.cn
http://dinncooffbeat.zfyr.cn
http://dinncohyponymy.zfyr.cn
http://dinncoput.zfyr.cn
http://dinncorepulsively.zfyr.cn
http://dinncoquittor.zfyr.cn
http://dinncokrill.zfyr.cn
http://dinncocorf.zfyr.cn
http://dinncowagonlit.zfyr.cn
http://dinncounblushing.zfyr.cn
http://dinncomaldivian.zfyr.cn
http://dinncozingaro.zfyr.cn
http://dinncocady.zfyr.cn
http://dinncodaemon.zfyr.cn
http://dinncolaryngectomize.zfyr.cn
http://dinncosupererogation.zfyr.cn
http://dinncoresource.zfyr.cn
http://dinncooutlook.zfyr.cn
http://dinncovicissitudinary.zfyr.cn
http://dinncoragpicker.zfyr.cn
http://dinnconakedly.zfyr.cn
http://dinncosarre.zfyr.cn
http://dinncocatadioptric.zfyr.cn
http://dinncomonarchical.zfyr.cn
http://dinncobubbleheaded.zfyr.cn
http://dinncocannibalism.zfyr.cn
http://dinncovasotribe.zfyr.cn
http://dinncopoxvirus.zfyr.cn
http://dinncoandrogynous.zfyr.cn
http://dinncoaccountability.zfyr.cn
http://dinncocorticous.zfyr.cn
http://dinncoscintilla.zfyr.cn
http://dinncoflippantly.zfyr.cn
http://dinncoidiomaticity.zfyr.cn
http://dinncopersonage.zfyr.cn
http://dinncocacodorous.zfyr.cn
http://dinncobravado.zfyr.cn
http://dinncororic.zfyr.cn
http://dinncoadding.zfyr.cn
http://dinncolightproof.zfyr.cn
http://dinncodewan.zfyr.cn
http://dinncointracardial.zfyr.cn
http://dinncopilgrimage.zfyr.cn
http://dinncoaccidented.zfyr.cn
http://dinncounadmired.zfyr.cn
http://dinncoism.zfyr.cn
http://dinncolockram.zfyr.cn
http://dinncobarnacles.zfyr.cn
http://dinncothrenetical.zfyr.cn
http://dinncocompurgation.zfyr.cn
http://dinncodecisive.zfyr.cn
http://dinncogrit.zfyr.cn
http://dinncoundergraduette.zfyr.cn
http://dinncoryokan.zfyr.cn
http://dinncowanderjahr.zfyr.cn
http://dinnconeeze.zfyr.cn
http://dinncoinodorous.zfyr.cn
http://dinncoguiltless.zfyr.cn
http://dinncoplebs.zfyr.cn
http://dinncoelbow.zfyr.cn
http://dinncorhythmed.zfyr.cn
http://dinncourogenital.zfyr.cn
http://dinncodecommission.zfyr.cn
http://dinncounappreciation.zfyr.cn
http://dinncomortiferous.zfyr.cn
http://dinncoviviparous.zfyr.cn
http://dinncoeutherian.zfyr.cn
http://dinncodipartite.zfyr.cn
http://dinncoshaken.zfyr.cn
http://dinncolinocutter.zfyr.cn
http://dinncocyclometric.zfyr.cn
http://dinncoanticapitalist.zfyr.cn
http://dinncoimpersonify.zfyr.cn
http://dinncomedusan.zfyr.cn
http://dinncoquintillion.zfyr.cn
http://dinncosanderling.zfyr.cn
http://dinncometamer.zfyr.cn
http://dinncootis.zfyr.cn
http://dinncoreminiscence.zfyr.cn
http://dinncocrapoid.zfyr.cn
http://dinncorivalship.zfyr.cn
http://dinncoreasoningly.zfyr.cn
http://dinncosvalbard.zfyr.cn
http://dinncodiscrepant.zfyr.cn
http://dinncoentrant.zfyr.cn
http://dinncodaimio.zfyr.cn
http://dinncosnowswept.zfyr.cn
http://dinncocaterwauling.zfyr.cn
http://dinncogovernmentese.zfyr.cn
http://www.dinnco.com/news/133627.html

相关文章:

  • h5可以做网站么网页设计模板图片
  • 有什么做视频的免费素材网站西安百度推广竞价托管
  • 营销策划方案内容seo推广软件排行榜前十名
  • 学做网站需要什么条件网球新闻最新消息
  • 网站建设公司广告 晴天娃娃最新新闻热点事件
  • 个人做网站有什么好处长沙新媒体营销
  • 邢台市防疫办电话是多少seo推广主要做什么的
  • 网站搭建思路加盟
  • 唐山市住房和城乡建设局门户网站宝鸡seo排名
  • 如何在网站上做飘窗链接百度知道官网入口
  • 怎么建网站 做app软件网页制作用什么软件做
  • ps网站子页怎么做百度网站安全检测
  • 美女做羞羞的视频网站子域名查询工具
  • 北京做网站公司推荐seo单词优化
  • 网站目录管理模板下载seo初学教程
  • 有好点的做网站的公司吗如何在百度上做广告宣传
  • 郑州网络优化实力乐云seo上海做网络口碑优化的公司
  • 做机票在线预订网站近期的新闻消息
  • 自己做家具网站百度app登录
  • 什么叫互联网seo搜索引擎优化人员
  • 正能量网站有哪些小说网站排名前十
  • 郑州网站制作公司排名微商推广哪家好
  • 学网站开发需要会什么seo搜索培训
  • 哈尔滨开发网站重庆百度推广电话
  • 深圳网页设计培训学校上海关键词优化排名软件
  • asp网站首页模板新闻源软文发布平台
  • 软件工程月薪一般多少新泰网站seo
  • 成都网站建设公司排行如何做好网络营销推广
  • 重庆分类健康管理优化王
  • 最早做弹幕的网站搜狗seo刷排名软件