vue3与SpringBoot的socket.io连接

vue3与SpringBoot的socket.io连接

相关依赖

前端Vue3使用 socket.io-client@2.2.0

npm install socket.io-client@2.2.0

后端SpringBoot使用 netty-socketio@1.7.18

<dependency>
	<groupId>com.corundumstudio.socketio</groupId>
	<artifactId>netty-socketio</artifactId>
	<version>1.7.18</version>
</dependency>

后端代码实现

Spring启动类

@SpringBootApplication
@Slf4j
public class MeetingApplication implements CommandLineRunner {
    // 注入 SocketIOServer
    @Autowired
    private SocketIOServer socketIOServer;

    public static void main(String[] args) {
        SpringApplication.run(MeetingApplication.class, args);
    }
	// 将在springboot启动后运行run
    @Override
    public void run(String... args) throws Exception {
        socketIOServer.start();
        log.info("socket.io启动");
    }
}

Socket.io配置类

@Configuration
class SocketIOConfig {
    /**
     * SocketIO配置
     * @return
     */
    @Bean
    public SocketIOServer socketIoServer() {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        return new SocketIOServer(config);
    }

    /**
     * 用于扫描netty-socketio的抓取事件注解
     */

    @Bean
    public SpringAnnotationScanner springAnnotationScanner() {
        return new SpringAnnotationScanner(socketIoServer());
    }

}

Socket.io事件处理类

@Component
@Slf4j
public class SocketHandler {
    // 连接成功就会调用此方法
    @OnConnect
    public void onConnect(SocketIOClient client){
        client.sendEvent("message", "conn ok");
        log.info(String.format("连接成功 %s",client.getSessionId()));
    }
}

前端代码实现

<script setup>
    import io from "socket.io-client";
    var socket = io.connect("ws://localhost:9092", {
      transports: ["websocket", "polling"], // 优先使用websocket连接方式
      timeout: 5000,
    });
    // 监听message事件
    socket.on("message", (message) => {
      console.log(message);
    });
</script>

参考链接

netty-socketio概述 https://blog.csdn.net/u014635374/article/details/115893751
vue3使用socketio的简单例子
https://blog.csdn.net/autumn_later/article/details/123045541

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建Java应用程序的开源框架,它能够简化Java开发过程并提供开箱即用的特性。它可以与xterm.js、Vue.js和Socket.IO等技术一起使用。 xterm.js是一个在网页上实现终端功能的JavaScript库。它可以通过WebSocket通信协议与后端进行实时通信,并将前端输入的命令传递给后端进行处理。xterm.js集成了WebSocket的功能,因此在使用xterm.js时不需要编写太多的WebSocket代码。 Vue.js是一个用于构建用户界面的JavaScript框架。它可以与xterm.js一起使用,来创建一个具有终端功能的前端界面。 Socket.IO是一个实现了WebSocket协议的JavaScript库。它可以用于在前端和后端之间实现实时通信。在Spring Boot应用程序中,可以使用Socket.IO来与xterm.js和Vue.js进行通信,实现终端功能的交互。 综上所述,使用Spring Boot、xterm.js、Vue.js和Socket.IO等技术可以实现一个具有终端功能的应用程序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [基于 xterm + websocket + vue 实现网页版终端 terminal](https://blog.csdn.net/weekdawn/article/details/125202972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值