Bootstrap

SpringBoot 基于注解实现的WebSocketServer

Maven 依赖

在线WebScoket客户端

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
<!--        引用这个依赖是当项目在开发环境运行时才需要或者运行项目是依赖SpringBoot内置的tomcat时才需要的
            @Configuration
            @EnableWebSocket
            public class WebSocketConfig {
                @Bean
                public ServerEndpointExporter serverEndpoint() {
                    return new ServerEndpointExporter();
                }
            }
            通过这个配置注册websocket.
            如果项目是在外置tomcat上运行的该依赖可以不需要导入 上面的那个配置Bean也就不需要了
-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <!--        打包 排除掉该依赖-->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除掉SpringBoot内置的tomcat-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

<!--        导入SpringBoot内置的tomcat. 但设置只有在开发阶段生效 。 maven-package将排除掉该依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.hwl.socket.SocketApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

一个三个类

启动类

@SpringBootApplication
public class SocketApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(SocketApplication.class, args);
	}

//	外置tomcat的启动实现
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(SocketApplication.class);
	}

}

配置类

// 判断只有在开发阶段 底下的所有配置才会生效
@ConditionalOnClass(ServerEndpointExporter.class)
@Configuration
@EnableWebSocket
public class WebSocketConfig {
	@Bean
		public ServerEndpointExporter serverEndpoint() {
		return new ServerEndpointExporter();
	}
}

websocket的处理类

package com.hwl.socket.server;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

// Handler 这个类是多例的. 每一个连接都会导致类实例的创建
// 原生API无法拥有拦截器
@Slf4j
@ServerEndpoint("/websocket/{key}")
@Component
public class Handler {

	//静态变量,用来记录当前在线连接数。
	private static final AtomicInteger onlineCount = new AtomicInteger(0);
	//concurrent包的线程安全
	private static final ConcurrentHashMap<String, Handler> map = new ConcurrentHashMap<String, Handler>();

	//与某个客户端的连接会话,需要通过它来给客户端发送数据
	private Session session;

	private String key;

	public static synchronized int getOnlineCount() {
		return onlineCount.get();
	}

	public static synchronized void addOnlineCount() {
		onlineCount.getAndIncrement();

	}

	public static synchronized void subOnlineCount() {
		onlineCount.getAndDecrement();
	}

	//  发生错误时调用
	@OnError
	public void onError(Session session, Throwable error) {
		log.error("[WebSocket] 异常:{}", error.getMessage());
	}

	//	连接建立成功调用的方法
	@OnOpen
	public void afterConnectionEstablished(Session session, @PathParam("key") String key) throws Exception {
		this.session = session;
		this.key = key+session.getId(); // 不然可能出现重复的key 导致map的存储出现问题
		addOnlineCount();
		map.put(key, this);
		log.info("[WebSocket] 新的连接加入:{}\t当前有{}个连接",  key, getOnlineCount());
	}

	//	收到客户端消息后调用的方法
	@OnMessage
	public void handleTextMessage(String message, Session session) throws Exception {
		log.info("[WebSocket] 来自 {} {} 的消息:{}", this.hashCode(),key, message);
		try {
			sendMessage(new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//	连接关闭调用的方法
	@OnClose
	public void afterConnectionClosed() throws Exception {
		map.remove(key);
		subOnlineCount();           //在线数减1
		log.info("[WebSocket] 连接关闭:{}\t当前有{}个连接", key, getOnlineCount());
	}

	/**
	 * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
	 */
	public void sendMessage(String message) throws IOException {
		this.session.getBasicRemote().sendText(message);
//		this.session.sendMessage(new TextMessage(message));
		//this.session.getAsyncRemote().sendText(message);
	}
}

;