EJBWebSocket握手错误:响应代码不是101:404

yr9zkbsy  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(303)

如果你没有时间阅读细节,我的问题是:

当一个简单的websocket请求发送到ejb模块中的ejbwebsocket端点服务器时,我得到了http错误代码404

(maven用java8、ejb3和jbosseap7.2创建的项目)

注意,ejb模块不能两者都有 web.xml 也不是 maven contextRoot tag ###详情:
我用ejb3开发了一个企业项目,并使用intellijidea中的maven通过jboss-eap7.2进行了部署。我需要提供一个websocket服务器和客户端与第三方进行通信。部分代码如下:
服务器端:

@ServerEndpoint("/chat")
public class ChatServer {
    private Session session;

    @OnOpen
    public void connect(Session session){
        this.session=session;
    }

    @OnClose
    public void close(){
        this.session=null;
    }

    @OnMessage
    public void processMessage(String msg){
        System.out.println("msg = " + msg);;
        if(this.session!=null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText("this is from server");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

同样在服务器上,我们有一个类来声明根路径(并启用jax-rs):

@ApplicationPath("gateway")
public class RestApplication extends Application {
    // without any implementation
}

此外,我在服务器端有一个过滤器,如下所示,它对websocket请求不做任何处理,在调试时,它显示请求是在过滤器处收到的,但在该服务器生成400之后,请求就永远不会转到 ServerEndpoint !
http筛选器:

@Provider
@PreMatching
public class SecurityFilter implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    //some snipet to evaluate the JAX-RS and nothing to do with websocket requests
    //websocket request received here but it did not go toward EndpointServer
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("response status: "+ containerResponseContext.getStatus());
        //here I get Http status code 404 
    }
}

我删除了过滤器,但错误仍然存在。
客户端:

public class SocketSmokeTest {
    private WebSocketContainer container;
    private HelloEndpoint endpoint;

    @Test
    public void communicate() {
        try {
            Session connectToServer = container.connectToServer
            (this.endpoint, new URI("ws://127.0.0.1:8080/gateway/chat"));
            //I have tried other path like "ws://127.0.0.1:8080/rootsource/gateway/chat"
            //or "ws://127.0.0.1:8080/rootsource/chat"
            this.endpoint.sendMessage("hello from client");
            Thread.sleep(3000);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
``` `HelloEndPoint` 是一个简单的类,如下所示:

public class HelloEndpoint extends Endpoint {
private Session session;

@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
    this.session = session;

    this.session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override
        public void onMessage(String message) {
            System.out.println("!!!!!!  retrieved message: " + message);
        }
    });
}

public void sendMessage(String message){
    try {
        this.session.getBasicRemote().sendText(message);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

错误:运行smoketest会得到以下错误:
javax.websocket.deploymentexception:握手错误。
原因:org.glassfish.tyrus.core.handshakeexception:响应代码不是101:404。
wireshark捕获的数据:

GET /resources/websocket/chatserver/ HTTP/1.1
Connection: Upgrade
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Sec-WebSocket-Key: K3mTSnyiVbi5/yeyIwFR2Q==
Sec-WebSocket-Version: 13
Upgrade: websocket

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 0
Date: Sat, 05 Sep 2020 05:02:43 GMT

关于项目结构的更多信息:项目由多层方法中的一些模块组成,例如聚合在ear中的数据(ejb模块)、业务(ejb模块)、网关(ejb模块)、gui(war模块)。所有jax-rs和websocket都驻留在网关模块中。
ear pom文件:

...

...





org.apache.maven.plugins
maven-ear-plugin
2.6

lib


true




ir.co.isc
BusinessLayer


ir.co.isc
DataAccessLayer


ir.co.isc
PresentationLayerGateway


ir.co.isc
PresentationLayerGUI
/rootsource





org.apache.maven.plugins
maven-war-plugin


org.apache.maven.plugins
maven-ejb-plugin


org.apache.maven.plugins
maven-compiler-plugin




...

网关ejb模块pom文件:



Azmoonyar
ir.co.isc
1.0

4.0.0

<artifactId>PresentationLayerGateway</artifactId>
<packaging>ejb</packaging>

<name>PresentationLayerGateway</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>ir.co.isc</groupId>
        <artifactId>BusinessLayer</artifactId>
        <version>1.0</version>
        <type>ejb</type>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>ejb-api</artifactId>
        <version>3.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-client-api</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-client</artifactId>
        <version>1.17</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus.bundles</groupId>
        <artifactId>tyrus-standalone-client</artifactId>
        <version>1.17</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version   >
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <skipTests>${skipTests}</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题