package com.emergency.avenue.admin.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Slf4j
@Configuration
@AllArgsConstructor
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.addEndpoint("/act/ws").setAllowedOriginPatterns("*")

				.withSockJS();
	}

	/**
	 * 配置消息代理，哪种路径的消息会进行代理处理
	 * @param registry
	 */
	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		/*
		 *  1. 配置一对一消息前缀， 客户端接收一对一消息需要配置的前缀 如“'/user/'+userid + '/message'”，
		 *     是客户端订阅一对一消息的地址 stompClient.subscribe js方法调用的地址
		 */
		registry.setUserDestinationPrefix("/user");

		//接受客户端发送给服务端地址的前缀
		registry.setApplicationDestinationPrefixes("/app");
		//用户订阅主题的前缀
		//topic 代表发布广播，即群发
		//queue 代表点对点，即发指定用户
        registry.enableSimpleBroker("/topic", "/topic");
		registry.enableSimpleBroker("/queue", "/user");
	}

	/**
	 * 设置输入消息通道的线程数，默认线程为1，可以自己自定义线程数，最大线程数，线程存活时间
	 * @param registration
	 */
	@Override
	public void configureClientInboundChannel(ChannelRegistration registration) {

	}

	@Bean
	public ServerEndpointExporter serverEndpointExporter() {
		return new ServerEndpointExporter();
	}
}
