由于SpringBoot已经整合了WebSocket,使用起来非常方便。这篇博客的前提是已经搭建好SpringBoot项目,如果没有搭建好,请参考http://blog.csdn.net/u010889616/article/details/79561808这篇文章。
项目结构如下:
gradle添加依赖
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket
compile group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version: '2.0.0.RELEASE'
(1)开启WebSocket服务。
新建WebSocketConfig.java类,添加@Configuration注解
package com.wzj.demo.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* Created by wzj on 2018/3/14.
*/
@Configuration
public class WebSocketConfig
{
@