package com.red.circle.game.fruit.infrastructure.websocket.config;

import java.security.Principal;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

/**
 * 握手监听处理.
 *
 * @author pengliang on 2021/7/29
 */
public class PrincipalHandshakeHandler extends DefaultHandshakeHandler {

  @Override
  protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler,
      Map<String, Object> attributes) {
    return () -> Optional.ofNullable(attributes.get(SocketConstant.USER_ID))
        .map(Objects::toString)
        .orElse("0");
  }

}
