微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebSocket EndPoint无法解决:错误代码:404

如何解决WebSocket EndPoint无法解决:错误代码:404

我正在尝试创建一个非常基本的Web套接字应用程序。遵循的步骤:

  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时,获得“与'ws:// localhost:7003 / wspractice / echo'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:404”错误

当我尝试访问WEB-INF中的另一个普通的jsp时,它已被正确解析,但是只有web-socket未被解析。结果如下图所示。

当服务器无法找到服务(在这种情况下为echo)但我无法理解为什么在这种情况下即使“ echo”端点可用时,也会发生404错误

Result in browser
尝试使用chrome,firefox,但结果相同。 请帮助我了解为什么会这样。

解决方法

我试图对您的代码进行尽可能少的更改。用作参考。

  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嵌入式服务器,因此您可以通过运行以下命令来测试代码段
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

Successfull run of the websockets

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。