Springboot 集成 WebSocket

一、加入依赖

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


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、自定义 Handler

package com.iyushu.websocket.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Slf4j
public class MyHandler extends TextWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {

        String payload = message.getPayload();
        log.info("我接收到的消息:{}", payload);
        String rtnMsg = "我回复了";
        for(int i = 0; i < 10; i ++){
            Thread.sleep(2000);
            session.sendMessage(new TextMessage(rtnMsg + i));
        }
        super.handleTextMessage(session, message);
    }
}

三、新建配置类,并将第二步新增的handler 注册到websocket

package com.iyushu.websocket.config;

import com.iyushu.websocket.handler.MyHandler;
import org.springframework.context.annotation.Bean;
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 {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(myHandler(), "/websocket");
    }


    @Bean
    public MyHandler myHandler() {
        return new MyHandler();
    }
}

四、新建一个index.html页面,其中使用js发送和接收websocket数据

<body>
<div id="msg"></div>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
    var socket = new WebSocket("ws://localhost:8080/websocket");
    socket.onopen = function() {
        socket.send("发送数据");
        console.log("数据发送中...");
    };
    socket.onmessage = function (evt){
        var received_msg = evt.data;
        $("#msg").append(received_msg+'
');
        console.log("数据已接收..."+received_msg);
    };


    socket.onclose = function(){
        // 关闭 websocket
        console.log("连接已关闭...");
    };
</script>
</body>

五、新增一个访问地址,映射到index.html

@GetMapping("/index")
public String index(){
    return "index";
}

六、呈现效果

Springboot 集成 WebSocket

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i余数

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值