【问题标题】:WebSocket EndPoint not getting resolved: Error Code: 404WebSocket 端点未得到解决:错误代码:404
【发布时间】:2021-01-16 14:16:05
【问题描述】:

我正在尝试创建一个非常基本的网络套接字应用程序。后续步骤:

  1. 创建 EchoServer java 类
    import javax.websocket.OnMessage;
    import javax.websocket.server.ServerEndpoint;
    
    @ServerEndpoint("/echo")
    public class EchoServer {
    
        @OnMessage
        public String echo(String incomingMessage) {
            return "I got this (" + incomingMessage + ") so I am sending it back";
        }
    }
  1. 使用调用上述端点的 javascript 创建 html

     <html>
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title>Web Socket JavaScript Echo Client</title>
         <script language="javascript" type="text/javascript">
             var echo_websocket;
             function init() {
                 output = document.getElementById("output");
             }
             function send_echo() {
                 var wsUri = "ws://localhost:7003/wspractice/echo";
                 writeToScreen("Connecting to " + wsUri);
                 echo_websocket = new WebSocket(wsUri);
                 echo_websocket.onopen = function (evt) {
                     writeToScreen("Connected !");
                     doSend(textID.value);
                 };
                 echo_websocket.onmessage = function (evt) {
                     writeToScreen("Received message: " + evt.data);
                     echo_websocket.close();
                 };
                 echo_websocket.onerror = function (evt) {
                     writeToScreen('<span style="color: red;">ERROR:</span> '
                         + evt.data);
                     echo_websocket.close();
                 };
             }
             function doSend(message) {
                 echo_websocket.send(message);
                 writeToScreen("Sent message: " + message);
             }
             function writeToScreen(message) {
                 var pre = document.createElement("p");
                 pre.style.wordWrap = "break-word";
                 pre.innerHTML = message;
                 output.appendChild(pre);
             }
             window.addEventListener("load", init, false);
         </script>
     </head>
     <body>
     <h1>Echo Server</h1>
     <div style="text-align: left;">
         <form action="">
             <input onclick="send_echo()" value="Press to send"
                    type="button">
             <input id="textID" name="message" value="Hello Web Sockets"
                    type="text">
             <br>
         </form>
     </div>
     <div id="output"></div>
     </body>
     </html>
    

在 WebLogic Server 版本:14.1.1.0.0 服务器中将上述项目部署为 war 文件(上下文路径:wspractice)。
named server configuration on weblogic(listening on port 7003)
application deployed on named server

在尝试上述 html 时,出现“WebSocket connection to 'ws://localhost:7003/wspractice/echo' failed: Error during WebSocket handshake: Unexpected response code: 404”错误。

当我尝试在 WEB-INF 中访问另一个正常的 jsp 时,它得到了正确的解析,但只有 web-socket 没有得到解析。结果如下图所示。

我知道当服务器无法找到服务时会发生 404(在这种情况下为回显),但即使“回显”端点可用,我也不明白为什么会在这种情况下发生。

Result in browser.
在 chrome、firefox 中尝试过,但结果相同。 请帮助我了解为什么会这样。

【问题讨论】:

    标签: javascript java websocket weblogic web-deployment


    【解决方案1】:

    我尝试对您的代码进行尽可能少的更改。将其作为参考。

    1. EchoServer.java 方法 onMessage 应该有不同的签名。
    import java.io.IOException;
    import javax.websocket.*;
    import javax.websocket.server.ServerEndpoint;
    
    @ServerEndpoint(value = "/echo")
    public class EchoServer {
        @OnMessage
        public void onMessage(final Session session, String msg) {
            System.out.println("Msg: " + msg);
            try {
                session.getBasicRemote().sendText(msg);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    1. html中有错别字
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Web Socket JavaScript Echo Client</title>
            <script language="javascript" type="text/javascript">
                var echo_websocket;
                function init() {
                    output = document.getElementById("output");
                }
                function send_echo() {
                    var wsUri = "ws://localhost:8080/test1/echo";
                    writeToScreen("Connecting to " + wsUri);
                    echo_websocket = new WebSocket(wsUri);
                    echo_websocket.onopen = function (evt) {
                        writeToScreen("Connected !");
                        doSend(document.getElementById("textID").value);
                    };
                    echo_websocket.onmessage = function (evt) {
                        writeToScreen("Received message: " + evt.data);
                        echo_websocket.close();
                    };
                    echo_websocket.onerror = function (evt) {
                        writeToScreen('<span style="color: red;">ERROR:</span> ' + evt);
                        console.log(evt)
                        echo_websocket.close();
                    };
                }
                function doSend(message) {
                    echo_websocket.send(message);
                    writeToScreen("Sent message: " + message);
                }
                function writeToScreen(message) {
                    var pre = document.createElement("p");
                    pre.style.wordWrap = "break-word";
                    pre.innerHTML = message;
                    output.appendChild(pre);
                }
                window.addEventListener("load", init, false);
            </script>
        </head>
        <body>
            <div style="text-align: left;">
                <form action="">
                    <input onclick="send_echo()" value="Press to send" type="button">
                    <input id="textID" name="message" value="Hello Web Sockets" type="text">
                    <br>
                </form>
            </div>
            <div id="output"></div>
        </body>
    </html>
    
    1. 以下 pom 引用 glassfish 嵌入式服务器,因此您可以通过运行以下命令测试 sn-ps
    mvn package embedded-glassfish:run
    

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.smirnov</groupId>
        <artifactId>test1</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>test1</name>
    
        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>7.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.glassfish.embedded</groupId>
                    <artifactId>maven-embedded-glassfish-plugin</artifactId>
                    <version>4.0</version>
                    <configuration>
                        <goalPrefix>embedded-glassfish</goalPrefix>
                        <app>${basedir}/target/${project.artifactId}-${project.version}.war</app>
                        <autoDelete>true</autoDelete>
                        <port>8080</port>
                        <name>test1</name>
                        <contextRoot>test1</contextRoot>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    你可以看到这个小项目here

    【讨论】:

      猜你喜欢
      • 2015-02-20
      • 2020-02-27
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      相关资源
      最近更新 更多