springboot整合websocket


小編在開發測試中發現前台刷狀態用ajax請求太惡心

只好使用websocket,小編也就開始了研究websocket的道路

廢話不多說,直接上碼

首先需要引入spring整合websocket的依賴

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

再來寫 spring 配置

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
@ConditionalOnWebApplication
public class WebSocketStompConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Bean
    public MySpringConfigurator mySpringConfigurator() {
        return new MySpringConfigurator();
    }
}

由於使用這種方式 websocket 的 bean 是由自己管理,所以需要將管理交給 spring

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.websocket.server.ServerEndpointConfig;

public class MySpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware {

    private static volatile BeanFactory context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MySpringConfigurator.context = applicationContext;
    }

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
        return context.getBean(clazz);
    }
}

接着,就要在 websocket 接口上寫注解

import cn.byzt.MySpringConfigurator;
import cn.byzt.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;


@Component
@ServerEndpoint(value = "/websocket", configurator = MySpringConfigurator.class)
public class TestController {

    @Autowired
    private TestService testService;

    @OnOpen
    public void open(Session session) {
        System.out.println("----open-----" + session.getId());
    }

    @OnMessage
    public void onMessage(Session session, String message) {
        sendMessage(session, testService.testMethod(message));
    }

    public void sendMessage(Session session, String message) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("發生錯誤");
        error.printStackTrace();
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println(session.getId() + "---走了...");
    }
}

這樣就可以在類中注入其他 bean

以上是小編使用 springboot 和 websocket  整合的代碼

如有不足之處,還請大神多多指教


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM