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  整合的代码

如有不足之处,还请大神多多指教

posted @ 2018-10-12 17:55  chbyiming  阅读(2733)  评论(0编辑  收藏  举报