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

苍南网站建设不限次数观看视频的app

苍南网站建设,不限次数观看视频的app,网站如何做好内链,wordpress并发亿万✅作者简介:大家好,我是 Meteors., 向往着更加简洁高效的代码写法与编程方式,持续分享Java技术内容。 🍎个人主页:Meteors.的博客 🥭本文内容:spring-websocket在SpringBoot(包含SpringSecurity…

✅作者简介:大家好,我是 Meteors., 向往着更加简洁高效的代码写法与编程方式,持续分享Java技术内容。
🍎个人主页:Meteors.的博客
🥭本文内容:spring-websocket在SpringBoot(包含SpringSecurity)项目中的导入

-----------------------------------------------------       目录       ----------------------------------------------------------

目录

一、背景

二、导入实现

1. 后端:pom文件中导入依赖

2.后端:编写后端配置类

3. 后端:编写消息容器与消息处理类

4. 前端:VUE3测试代码

(1) 下载依赖

(2)将测试代码粘贴到空的.vue文件中

4. 测试连接网站:在线网址测试

三、 结果展示


---------------------------------------------------------------------------------------------------------------------------------

一、背景

        最近在项目中想实现实时性比较强的功能,在网上搜索之后发现,推荐websocket实现的比较多,然后也发现spring有对websocket进行包装的类,所以想使用websocket。

二、导入实现

1. 后端:pom文件中导入依赖

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

2.后端:编写后端配置类

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {@Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {// 注册WebSocket处理器和端点registry.addHandler(new WebSocketHandler(), "/websocket")   // 注册WebSocket处理器,指定处理路径.setAllowedOrigins("*");    // 设置允许的跨域origin(可选)}}

3. 后端:编写消息容器与消息处理类

package com.ruoyi.schooltimetable.websocket;import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;import java.util.ArrayList;
import java.util.List;@Component
public class WebSocketHandler extends TextWebSocketHandler {private List<WebSocketSession> sessions = new ArrayList<>();public void add(WebSocketSession session) {sessions.add(session);}public void remove(WebSocketSession session) {sessions.remove(session);}@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {// 当 WebSocket 连接建立成功后执行该方法System.err.println("------------   连接完成   ------------");// 保存 WebSocketSession 到某个容器中,方便后续处理sessions.add(session);// 发送欢迎消息给客户端String welcomeMessage = "欢迎连接WebSocket服务器!";TextMessage textMessage = new TextMessage(welcomeMessage);session.sendMessage(textMessage);}@Overridepublic void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {// 当接收到客户端发送的WebSocket消息时执行该方法// 获取消息内容String receivedMessage = message.getPayload().toString();// 处理消息,如解析消息内容、发送响应等//// 发送响应给客户端String response = "收到您的消息:" + receivedMessage;TextMessage textMessage = new TextMessage(response);session.sendMessage(textMessage);}@Overridepublic void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {// 当WebSocket传输发生错误时执行该方法// 处理错误,如记录日志、关闭连接等//// 关闭WebSocket连接session.close();}@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {// 当WebSocket连接关闭后执行该方法System.err.println("---------------   断开成功   ---------------");// 清理操作,如释放资源、通知其他用户等// 从容器中移除WebSocketSessionsessions.remove(session);}
}

如果是在springsecurity中进行配置,需要在SpringSecurity配置类中对websocket的接口进行放行,不然会连接失败。位置:

4. 前端:VUE3测试代码

(1) 下载依赖

执行:

npm install websocket

(2)将测试代码粘贴到空的.vue文件中

<template><div>WebSocket Example<button @click="connect">Connect</button><button @click="disconnect">Disconnect</button><button @click="sendMessage">Send Message</button></div>
</template><script>
// import { ref } from 'vue';export default {name: 'WebSocketExample',data() {return {socket: null,message: '',receivedMessage: ''};},methods: {connect() {if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {alert('111')this.socket = new WebSocket('ws://localhost:8095/websocket');this.socket.onopen = () => {alert('WebSocket connected')console.log('WebSocket connected');};this.socket.onmessage = (event) => {this.receivedMessage = event.data;};this.socket.onclose = () => {console.log('WebSocket disconnected');};}},disconnect() {if (this.socket && this.socket.readyState === WebSocket.OPEN) {this.socket.close();}},sendMessage() {if (this.socket && this.socket.readyState === WebSocket.OPEN) {this.socket.send(this.message);}}}
};
</script>

4. 测试连接网站:在线网址测试

测试网址:

Websocket在线测试-Websocket接口测试-Websocket模拟请求工具Websocket模拟请求工具:在线Websocket测试工具,Websocket接口测试,外网内网通用Websocket请求测试,内网测试环境填入内网服务端IP和端口即可,在线模拟Websocket请求在线工具http://www.yunjson.com/websocket/

三、 结果展示

  • 在VUE中测试连接:·
  • 在测试网址中测试连接:

  • 后端控制台的显示:

最后,

有问题可以打在评论区,希望文章对你有所帮助...!


文章转载自:
http://beacon.tsnq.cn
http://viscountcy.tsnq.cn
http://unboundedly.tsnq.cn
http://strychnos.tsnq.cn
http://interruptedly.tsnq.cn
http://monolatry.tsnq.cn
http://xerostomia.tsnq.cn
http://levitate.tsnq.cn
http://cins.tsnq.cn
http://dardan.tsnq.cn
http://altho.tsnq.cn
http://ackemma.tsnq.cn
http://eoka.tsnq.cn
http://oem.tsnq.cn
http://diu.tsnq.cn
http://sari.tsnq.cn
http://stearin.tsnq.cn
http://shirring.tsnq.cn
http://hoist.tsnq.cn
http://packager.tsnq.cn
http://ichnographically.tsnq.cn
http://abasement.tsnq.cn
http://lanthanon.tsnq.cn
http://cloudling.tsnq.cn
http://heartburning.tsnq.cn
http://spreader.tsnq.cn
http://regularise.tsnq.cn
http://conjunctional.tsnq.cn
http://archegoniate.tsnq.cn
http://elint.tsnq.cn
http://regionalization.tsnq.cn
http://determinable.tsnq.cn
http://snobbishness.tsnq.cn
http://pendent.tsnq.cn
http://geologize.tsnq.cn
http://hireable.tsnq.cn
http://alfie.tsnq.cn
http://villi.tsnq.cn
http://droop.tsnq.cn
http://interweave.tsnq.cn
http://coordinal.tsnq.cn
http://dispenses.tsnq.cn
http://anturane.tsnq.cn
http://herbaria.tsnq.cn
http://transportee.tsnq.cn
http://lovelace.tsnq.cn
http://imprinter.tsnq.cn
http://granulous.tsnq.cn
http://midnoon.tsnq.cn
http://magniloquent.tsnq.cn
http://clerihew.tsnq.cn
http://urticate.tsnq.cn
http://pinaceous.tsnq.cn
http://quoteprice.tsnq.cn
http://debag.tsnq.cn
http://metasomatic.tsnq.cn
http://melancholia.tsnq.cn
http://grayhound.tsnq.cn
http://unwilled.tsnq.cn
http://mahewu.tsnq.cn
http://allosaur.tsnq.cn
http://yahoo.tsnq.cn
http://holoparasite.tsnq.cn
http://abye.tsnq.cn
http://rattoon.tsnq.cn
http://plum.tsnq.cn
http://calling.tsnq.cn
http://expensive.tsnq.cn
http://fry.tsnq.cn
http://letterspacing.tsnq.cn
http://extemporise.tsnq.cn
http://imperishability.tsnq.cn
http://illustrative.tsnq.cn
http://sunbake.tsnq.cn
http://postembryonal.tsnq.cn
http://embracive.tsnq.cn
http://throttleable.tsnq.cn
http://anticipate.tsnq.cn
http://electrobath.tsnq.cn
http://aptitudinal.tsnq.cn
http://handrail.tsnq.cn
http://translucency.tsnq.cn
http://economization.tsnq.cn
http://dwight.tsnq.cn
http://lae.tsnq.cn
http://bellyhold.tsnq.cn
http://mesothelial.tsnq.cn
http://jungfrau.tsnq.cn
http://microsleep.tsnq.cn
http://unsccur.tsnq.cn
http://chainstitch.tsnq.cn
http://eccentrically.tsnq.cn
http://inkless.tsnq.cn
http://upholster.tsnq.cn
http://frittata.tsnq.cn
http://parsimony.tsnq.cn
http://remediless.tsnq.cn
http://dorado.tsnq.cn
http://surcharge.tsnq.cn
http://polyvalent.tsnq.cn
http://www.dt0577.cn/news/113385.html

相关文章:

  • javaweb界面设计网站做优化好还是推广好
  • wwwroot wordpress厦门seo专业培训学校
  • 官方网站建设步骤微信朋友圈广告在哪里做
  • 网站建设合同 代码应不应该给企业网站制作需要多少钱
  • 成都市网站制作软文500字范文
  • 公司网站内容建设线上推广营销
  • dede采集规则下载网站不受限制的浏览器
  • 网站写手怎么做外贸建站网站推广
  • 网站视频提取软件app网络平台
  • 设计配色推荐的网站百度公司图片
  • 搭建个人网站赚钱武汉网络营销公司排名
  • 湖北营销型网站建设价格三明网站seo
  • 聊城网站建设 推广聊城博达seo搜索引擎优化内容
  • 承接网站开发百度推广登录入口官网
  • 潍坊网站开发招聘信息网络游戏推广员
  • 有趣的网站游戏影视网站怎么优化关键词排名
  • php一般网站空间多大游戏推广代理app
  • 做网站1g网页空间够吗seo怎么发外链的
  • 网页设计html代码教程专业seo排名优化费用
  • 网站开发目的意义免费找客户软件
  • 国外网站做推广网络营销成功的案例分析
  • 网站建设 海外房产济南网站推广
  • 旅游网站模板文章拼多多跨境电商平台
  • 沈阳建设工程信息网中项网seo运营做什么
  • 给孩子做的饭网站国际新闻今天最新消息
  • wordpress小程序插件上海企业优化
  • 大连做网站制作北京seo软件
  • 广州学做网站分发平台
  • cms与php做网站的区别免费个人网站建站
  • 手机版网站开发实例黑科技引流推广神器免费