我想要与此 XML 配置等效的内容(在此处获取),但使用 Java 配置:
<bean id="customHandler" class="app.wsock.CustomHandler"/>
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/foo">
<websocket:handshake-handler ref="customHandler"/>
</websocket:stomp-endpoint>
<websocket:simpl-broker prefix="/topic,/queue" />
</websocket:message-broker>
Run Code Online (Sandbox Code Playgroud)
我的目标是构建一个类,根据某些标准限制与我的 STOMP 端点(即:他的 websocket)的连接。
我不想使用 XML 来配置我的端点,如何将该代码段转换为 Java Config?
我正在使用 Spring 4 + Websockets + Stomp JS 库。我找不到任何方法来设置 websocket ping/pong 机制(心跳)。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...">
<websocket:message-broker>
<websocket:stomp-endpoint path="/cors/auth/clientEndpoint">
<websocket:handshake-handler ref="myHandshakeHandler" />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/queue, /topic" />
<websocket:client-inbound-channel>
<websocket:interceptors>
<bean class="com.mycompany.myproject.utils.messaging.MyInboundChannelInterception"></bean>
</websocket:interceptors>
</websocket:client-inbound-channel>
</websocket:message-broker>
<bean id="myHandshakeHandler" class="com.mycompany.myproject.utils.security.MyHandshakeHandler" />
<bean class="org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean">
<property name="maxSessionIdleTimeout" value="120000" />
</bean>
Run Code Online (Sandbox Code Playgroud)
因此,我正在实现我自己的 ping/pong 消息机制。
这里的任务之一 - 实现 websocket 的服务器端关闭,以防客户端在超过 10 秒内没有 ping 消息。
并且无法使用 Spring Websockets 来做到这一点!
也许有人可以告诉我如何访问用户的 Session 对象或通过 Spring Websockets 关闭这些 Session?
看来这里的春天很有限。
我想使用spring security. 从spring 官方文档 中 23.2 WebSocket Authentication,WebSocket 将重用在建立 WebSocket 连接时在 HTTP 请求中找到的相同身份验证信息。所以我设置了 spring security 来验证rest service. 如果用户通过了其余的认证,则拥有WebSocket连接的权限,否则无法建立WebSocket连接。以下是代码:
用于登录的休息服务:WssAuthService.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Authentication service.
*/
@RestController
@RequestMapping(path = "/hpdm")
public class WssAuthService {
@RequestMapping(path = "/login", method = RequestMethod.GET)
public String login(){
return "Login success to WssBroker...";
}
}
Run Code Online (Sandbox Code Playgroud)
spring 安全配置:WebSecurityConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy; …Run Code Online (Sandbox Code Playgroud) 我正在尝试连接到网络套接字 wss://ws-feed.gdax.com
我已经在 JavaScript 中使用了这个(见这里),但我试图将连接服务器端移动到我的 Spring Boot 应用程序中。
到目前为止,我有:
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private Logger LOG = LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.setDefaultMaxTextMessageBufferSize(64*1024);
WebSocketClient simpleWebSocketClient = new StandardWebSocketClient(container);
List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(simpleWebSocketClient));
SockJsClient sockJsClient = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
LOG.info("Connecting To [wss://ws-feed.gdax.com]");
StompSession session = stompClient.connect("wss://ws-feed.gdax.com", new GDAXHandler()).get();
}
private …Run Code Online (Sandbox Code Playgroud) 我想创建一个带有连接到 Web 套接字服务器的 Web 套接字客户端的 Spring Boot 应用程序。
例如,我使用了 Spring Boot 中的入门指南。
https://spring.io/guides/gs/messaging-stomp-websocket/
在此示例中,您使用 Spring Boot 创建一个 Web 套接字服务器,并使用 JavaScript 连接到它。
我想运行该服务器并使用另一个创建 WebSocketClient 对象的 Spring Boot 应用程序连接到它。
这是我在 Spring Boot 客户端 App 中创建的 WebSocketClientConfiguration 类
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketClientConfig {
@Bean
public WebSocketClient webSocketClient() {
final WebSocketClient client = new StandardWebSocketClient();
final WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
final StompSessionHandler sessionHandler = new MyStompSessionHandler();
stompClient.connect("ws://localhost:8080", sessionHandler);
return client;
}
}
Run Code Online (Sandbox Code Playgroud)
但是在我的类 MyStompSessionHandler 中,在 handleTransportError 方法中我可以看到异常是
javax.websocket.DeploymentException: The HTTP …Run Code Online (Sandbox Code Playgroud) 我尝试为我的应用程序添加 websocket 授权。
我有以下授权相关类:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private static final String SECURE_ADMIN_PASSWORD = "rockandroll";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.formLogin()
.loginPage("/index.html")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/sender.html")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/index.html")
.permitAll()
.and()
.authorizeRequests()
.antMatchers("/js/**", "/lib/**", "/images/**", "/css/**", "/index.html", "/","/*.css","/webjars/**", "/*.js").permitAll()
.antMatchers("/websocket").hasRole("ADMIN")
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ADMIN")
.anyRequest().authenticated();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthenticationProvider() {
@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException …Run Code Online (Sandbox Code Playgroud) java spring-security spring-boot spring-messaging spring-websocket
我有一个在 java spring 服务器和浏览器之间使用 WebSockets 的实时应用程序。是否有一种方法可以在页面刷新后保持 Websocket 连接处于活动状态?
这是我的 javascript 代码:
consumerWebSocket = new WebSocket("wss://" + window.location.host +
"/myWebSocketConnection");
consumerWebSocket.onopen = function () {
sendThroughWS(consumerWebSocket, "Send this message from browser to server");
};
Run Code Online (Sandbox Code Playgroud) 亲爱的,我正在尝试在我的 WebSocketHandler 中获取 HTTPSession。当我使用“javax.websocket-api”时我可以成功完成此操作,但我现在使用“Spring-Websocket”。
配置:
@ConditionalOnWebApplication
@Configuration
@EnableWebSocket
public class WebSocketConfigurator implements WebSocketConfigurer {
@Autowired
private ApplicationContext context;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
MyEndpoint endpoint = context.getBean(MyEndpoint.class);
registry.addHandler(endpoint, "/signaling");
}
}
Run Code Online (Sandbox Code Playgroud)
当连接建立时:
@Component
public class MyEndpoint implements WebSocketHandler {
private WebSocketSession wsSession;
@Override
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
this.wsSession = webSocketSession;
// need to get the HTTP SESSION HERE
log.info("Opening: " + webSocketSession.getId());
}
}
Run Code Online (Sandbox Code Playgroud)
现在这是我如何使用 'javax.websocket-api' 执行此操作的示例:
配置:
@ServerEndpoint(value = "/signaling", //
decoders = MessageDecoder.class, // …Run Code Online (Sandbox Code Playgroud) 在 Spring Boot 中使用 websockets 时,我见过使用以下示例:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic/");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/greeting");;
}
}
Run Code Online (Sandbox Code Playgroud)
指定 config.setApplicationDestinationPrefixes("/app") 并在控制器中使用 @MessageMapping 注释。
而且我还看到了仅使用 enableSimpleBroker() 并在控制器中使用 @SubscribeMapping 的示例。
据我了解,@MessageMapping 负责将收到的消息路由到正确的方法。并且只有当目的地包含在 setApplicationDestinationPrefixes 中声明的前缀之一时,才会触发带有此注释的方法。
但是@SubscribeMapping 也将消息路由到正确的方法,我们不需要在配置类中调用 setApplicationDestinationPrefixes()。
有什么不同?
我已经用 spring 消息构建了 WebSocket 聊天室。
现在我想建立像 Twitch 这样的流媒体网站。我在直播上搜索了很多方法。他们总是提供Andriod的框架。
首先,我想使用二进制来传输我的流。
但我担心如果连接太多我的流会崩溃。
所以我去了 youtube 和 twitch.tv 我看到 twitch 使用 m3u8 碎片并使用 WebSocket 获取信息并使用 get 或 option 获取 m3u8 文件来制作视频。
springMVC可以像DFS一样构建吗?
或者我也需要阅读一些API手册?
有人可以直接告诉我如何建立一个流媒体网站吗?
spring-websocket ×10
java ×6
stomp ×5
websocket ×4
spring ×3
spring-boot ×3
httpsession ×1
javascript ×1
spring-mvc ×1
twitch ×1
youtube ×1