void updateSimpMessageHeadersFromStompHeaders() {
   if (getNativeHeaders() == null) {
     return;
   }
   String value = getFirstNativeHeader(STOMP_DESTINATION_HEADER);
   if (value != null) {
     super.setDestination(value);
   }
   value = getFirstNativeHeader(STOMP_CONTENT_TYPE_HEADER);
   if (value != null) {
     super.setContentType(MimeTypeUtils.parseMimeType(value));
   }
   StompCommand command = getCommand();
   if (StompCommand.MESSAGE.equals(command)) {
     value = getFirstNativeHeader(STOMP_SUBSCRIPTION_HEADER);
     if (value != null) {
       super.setSubscriptionId(value);
     }
   } else if (StompCommand.SUBSCRIBE.equals(command) || StompCommand.UNSUBSCRIBE.equals(command)) {
     value = getFirstNativeHeader(STOMP_ID_HEADER);
     if (value != null) {
       super.setSubscriptionId(value);
     }
   } else if (StompCommand.CONNECT.equals(command)) {
     protectPasscode();
   }
 }
  @Test
  public void handleMessageToClientConnectAck() {

    StompHeaderAccessor connectHeaders = StompHeaderAccessor.create(StompCommand.CONNECT);
    connectHeaders.setHeartbeat(10000, 10000);
    connectHeaders.setNativeHeader(StompHeaderAccessor.STOMP_ACCEPT_VERSION_HEADER, "1.0,1.1");
    Message<?> connectMessage =
        MessageBuilder.withPayload(new byte[0]).setHeaders(connectHeaders).build();

    SimpMessageHeaderAccessor connectAckHeaders =
        SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
    connectAckHeaders.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
    Message<byte[]> connectAckMessage =
        MessageBuilder.withPayload(new byte[0]).setHeaders(connectAckHeaders).build();

    this.protocolHandler.handleMessageToClient(this.session, connectAckMessage);

    verifyNoMoreInteractions(this.channel);

    // Check CONNECTED reply

    assertEquals(1, this.session.getSentMessages().size());
    TextMessage textMessage = (TextMessage) this.session.getSentMessages().get(0);
    Message<?> message =
        new StompDecoder().decode(ByteBuffer.wrap(textMessage.getPayload().getBytes()));
    StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(message);

    assertEquals(StompCommand.CONNECTED, replyHeaders.getCommand());
    assertEquals("1.1", replyHeaders.getVersion());
    assertArrayEquals(new long[] {0, 0}, replyHeaders.getHeartbeat());
    assertEquals("joe", replyHeaders.getNativeHeader("user-name").get(0));
  }
  /**
   * Encodes the given payload and headers into a {@code byte[]}.
   *
   * @param headers the headers
   * @param payload the payload
   * @return the encoded message
   */
  public byte[] encode(Map<String, Object> headers, byte[] payload) {
    Assert.notNull(headers, "'headers' is required");
    Assert.notNull(payload, "'payload' is required");
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream(128 + payload.length);
      DataOutputStream output = new DataOutputStream(baos);

      if (SimpMessageType.HEARTBEAT.equals(SimpMessageHeaderAccessor.getMessageType(headers))) {
        logger.trace("Encoded heartbeat");
        output.write(StompDecoder.HEARTBEAT_PAYLOAD);
      } else {
        StompCommand command = StompHeaderAccessor.getCommand(headers);
        Assert.notNull(command, "Missing STOMP command: " + headers);
        output.write(command.toString().getBytes(StompDecoder.UTF8_CHARSET));
        output.write(LF);
        writeHeaders(command, headers, payload, output);
        output.write(LF);
        writeBody(payload, output);
        output.write((byte) 0);
      }

      return baos.toByteArray();
    } catch (IOException e) {
      throw new StompConversionException(
          "Failed to encode STOMP frame, headers=" + headers + ".", e);
    }
  }
  public void onApplicationEvent(SessionConnectEvent event) {
    MessageHeaders headers = event.getMessage().getHeaders();
    Principal user = SimpMessageHeaderAccessor.getUser(headers);
    if (user == null) {
      logger.info(event.getSource() + " Connect  user == null");
      user =
          new Principal() {

            @Override
            public String getName() {
              return "test zhxy 1";
            }
          };
    }
    String id = SimpMessageHeaderAccessor.getSessionId(headers);
    //		repository.save(new ActiveWebSocketUser(id, user.getName(), Calendar.getInstance()));
    messagingTemplate.convertAndSend("/topic/friends/signin", Arrays.asList(user.getName()));
  }
  @Override
  public void onApplicationEvent(AbstractSubProtocolEvent event) {
    MessageHeaders headers = event.getMessage().getHeaders();
    Principal user = SimpMessageHeaderAccessor.getUser(headers);

    if (event instanceof SessionConnectEvent) {
      String id = SimpMessageHeaderAccessor.getSessionId(headers);

      playerManager.addPlayer(id, user.getName());

      messagingTemplate.convertAndSend(
          "/topic/player/join", PlayerEntry.create(user.getName(), PlayerEntry.State.ACTIVE));
    } else if (event instanceof SessionDisconnectEvent) {
      SessionDisconnectEvent disconnectEvent = (SessionDisconnectEvent) event;

      playerManager.removePlayer(disconnectEvent.getSessionId());

      messagingTemplate.convertAndSend(
          "/topic/player/leave", PlayerEntry.create(user.getName(), PlayerEntry.State.DETACHED));
    }
  }
 @MessageMapping("/hello")
 public void greeting(SimpMessageHeaderAccessor headerAccessor, HelloMessage message)
     throws Exception {
   InetSocketAddress clientAddress =
       (InetSocketAddress) headerAccessor.getSessionAttributes().get("remoteAddress");
   String ip = clientAddress.getAddress().getHostAddress();
   String host = clientAddress.getHostName();
   LOG.info("Ping client");
   Thread.sleep(1000); // simulated delay
   String greetingMsg =
       String.format("Hello, %s! From ip: %s, hostname: %s", message.getName(), ip, host);
   messagingTemplate.convertAndSend("/cg/websockets/topic/greetings", new Greeting(greetingMsg));
 }
  @ServiceActivator(inputChannel = Sink.INPUT)
  public void websocketSink(Message<?> message) {
    if (logger.isTraceEnabled()) {
      logger.trace(String.format("Handling message: %s", message));
    }

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    headers.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
    String messagePayload = message.getPayload().toString();
    for (Channel channel : WebsocketSinkServer.channels) {
      if (logger.isTraceEnabled()) {
        logger.trace(
            String.format(
                "Writing message %s to channel %s", messagePayload, channel.localAddress()));
      }

      channel.write(new TextWebSocketFrame(messagePayload));
      channel.flush();
    }

    if (traceEndpointEnabled) {
      addMessageToTraceRepository(message);
    }
  }
 @Override
 public void setSubscriptionId(String subscriptionId) {
   super.setSubscriptionId(subscriptionId);
   trySetStompHeaderForSubscriptionId();
 }
 public void setContentType(MimeType contentType) {
   super.setContentType(contentType);
   setNativeHeader(STOMP_CONTENT_TYPE_HEADER, contentType.toString());
 }
 @Override
 public void setDestination(String destination) {
   super.setDestination(destination);
   setNativeHeader(STOMP_DESTINATION_HEADER, destination);
 }