我目前正在尝试在Glassfish中实现Spring Websocket。到目前为止,我已经完成了此操作,但是当我尝试连接时,服务器未返回Sec-WebSocket协议。在Google Chrome浏览器中,出现以下错误:
Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received.
请求和响应,如Google Chrome中显示的图像:
作为Stomp客户端,我在Angular中使用ng2-stompjs。
我尝试了Tomcat 9.0.17和WildFly 16,一切都按预期工作。
WebSocket配置:
@Configuration
@EnableWebSocketMessageBroker
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry
.addEndpoint("/ws")
.setAllowedOrigins("*");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic");
}
}
glassfish-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/monitoring</context-root>
<classloading-delegate>false</classloading-delegate>
<parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>
build.gradle:
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
id 'war'
}
apply plugin: 'io.spring.dependency-management'
group = 'de.conting'
version = '0.0.1'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
configurations {
compile.exclude module: 'spring-boot-starter-tomcat'
compile.exclude group: 'org.apache.tomcat'
}
war {
enabled = true
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jersey'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-quartz'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.hibernate:hibernate-core:5.4.2.Final'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
runtimeOnly 'org.postgresql:postgresql'
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
}