簡體   English   中英

WebSocket EndPoint 未得到解決:錯誤代碼:404

[英]WebSocket EndPoint not getting resolved: Error Code: 404

我正在嘗試創建一個非常基本的網絡套接字應用程序。 步驟如下:

  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)。
weblogic 上的命名服務器配置(偵聽端口 7003)
部署在命名服務器上的應用程序

嘗試上述 html 時,出現“WebSocket connection to 'ws://localhost:7003/wspractice/echo' failed: Error during WebSocket handshake: Unexpected response code: 404”錯誤。

當我嘗試在 WEB-INF 中訪問另一個普通的 jsp 時,它得到了正確的解析,但只有 web-socket 沒有得到解析。 結果如下圖所示。

我理解 404 發生在服務器無法找到服務(在這種情況下是回聲)時,但我不明白為什么會在這種情況下發生,即使“回聲”端點可用。

結果在瀏覽器中
在 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>

你可以在這里看到這個小項目。

成功運行 websockets

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM