SockJS如何创建iframe

时间:2018-09-22 13:51:28

标签: javascript sockjs

我在网站上使用SockJS,并且使用端点/ msg

当我转到mysitename.com/msg/iframe.html时,它显示此页面:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <script>
    document.domain = document.domain;
    _sockjs_onload = function(){SockJS.bootstrap_iframe();};
  </script>
  <script src="https://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js"></script>
</head>
<body>
  <h2>Don't panic!</h2>
  <p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
</body>
</html>

SockJS如何做到这一点?

1 个答案:

答案 0 :(得分:0)

它位于后端spring websocket的org.springframework.web.socket.sockjs.support.AbstractSockJsService中。

例如:

public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

private static final String SOCKJS_VERSION = "https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.3.0/sockjs.min.js";

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/handler").setAllowedOrigins("*");
    registry
        .addEndpoint("/handler")
        .setAllowedOrigins("*")
        .withSockJS()
        .setClientLibraryUrl(SOCKJS_VERSION);
}

}

如果您遵循setClientLibraryUrl,则会知道它的计算位置。

选中https://github.com/spring-projects/spring-framework/blob/master/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java

并搜索“ IFRAME_CONTENT”