当前位置: 首页 > 面试题库 >

Web套接字在OpenShift上断开连接(使用WildFly 8.2.1)

周洋
2023-03-14
问题内容

我正在使用OpenShift和WildFly 8.2.1 final来实现新的HTML5
websocket。我使用了本教程来设置这个项目。

每当我打开MyTest.html时,这就是JavaScript记录的内容:

JS: Server Connected...
JS: Server Disconnected...

服务器连接,然后立即断开连接。为什么?我究竟做错了什么?有什么我想念的吗?

这是模式代码->

serverendpoint.java

package testing;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/serverendpoint")
public class serverendpoint {
    @OnOpen
    public void handleOpen () {
        System.out.println("JAVA: Client is now connected...");
    }

    @OnMessage
    public String handleMessage (String message) {
        System.out.println("JAVA: Received from client: "+ message);
        String replyMessage = "echo "+ message; 
        System.out.println("JAVA: Send to client: "+ replyMessage);
        return replyMessage;
    }

    @OnClose
    public void handleClose() {
        System.out.println("JAVA: Client is now disconnected...");
    }

    @OnError
    public void handleError (Throwable t) {
        t.printStackTrace();
    }
}

MyTest.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My WS Website</title>
</head>
<body>
    <form>
        <input id="textMessage" type="text">
        <input onclick="sendMessage();" value="Send Message" type="button">
    </form>
    <br>
    <textarea id="messageTextArea" rows="10" cols="50"></textarea>
    <script type="text/javascript">
        var wsUri = "ws://" + document.location.hostname + ":8000" + document.location.pathname + "serverendpoint";
        var webSocket = new WebSocket(wsUri);
        var messageTextArea = document.getElementById("messageTextArea");
        webSocket.onopen = function(message) { processOpen(message);};
        webSocket.onmessage = function(message) { processMessage(message);};
        webSocket.onclose = function(message) { processClose(message);};
        webSocket.onerror = function(message) { processError(message);};
        function processOpen (message) {
            messageTextArea.value += "JS: Server Connected..."+"\n";
        }
        function processMessage(message) {
            messageTextArea.value += "JS: Receive from Server ==> "+message.data+"\n";
        }
        function sendMessage () {
            if (textMessage.value !="close") {
                webSocket.send(textMessage.value);
                messageTextArea.value += "JS: Send to Server ==> "+textMessage.value+"\n";
                textMessage.value="";
            } else webSocket.close();
        }
        function processClose(message) {
            webSocket.send("JS: Client disconnected...")
            messageTextArea.value += "JS: Server Disconnected..."+"\n";
        }
        function processError (message) {
            messageTextArea.value += "JS: error ..."+"\n";
        }
    </script>
</body>
</html>

和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>testing</groupId>
    <artifactId>testing</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>testing</name>

    <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>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

<profiles>
    <profile>
     <!-- When built in OpenShift the 'openshift' profile will be used when invoking mvn. -->
     <!-- Use this profile for any OpenShift specific customization your app will need. -->
     <!-- By default that is to put the resulting archive into the 'deployments' folder. -->
     <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
     <id>openshift</id>
     <build>
        <finalName>testing</finalName>
        <plugins>
          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <outputDirectory>deployments</outputDirectory>
                      <warName>ROOT</warName>
                </configuration>
            </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

谢谢您的帮助!


问题答案:

我认为您在打开websocket时正在尝试连接到不存在的端点。

这(请注意,您错过了/../):

var wsUri = "ws://" + document.location.hostname + ":8000" + document.location.pathname + "/../serverendpoint";

…将适用于您部署到WildFly的文件,如下所示:

├── pom.xml
└── src
    └── main
        ├── java
        │   └── testing
        │       └── serverendpoint.java
        └── webapp
            ├── MyTest.html
            └── WEB-INF
                └── web.xml

我已经在OpenShift Online上的WildFly 10磁带上使用您的代码(带有修改的端点路径)检查了此内容。



 类似资料:
  • 问题内容: 检测插座是否已掉落的最合适方法是什么?还是实际上是否发送过数据包? 我有一个库,用于通过Apple网关(在GitHub上提供)将Apple Push Notifications发送到iPhone 。客户端需要打开一个套接字并发送每个消息的二进制表示形式。但不幸的是,Apple不会返回任何确认。该连接也可以重用于发送多个消息。我正在使用简单的Java套接字连接。相关代码为: 在某些情况下

  • 我在服务器上有一个专用于套接字的Node/Express应用程序,在客户端上它是Angular 1.5。使用相同的架构(例如单独的套接字服务器)在http上本地运行代码,一切都运行得非常好。 当我在本地运行代码时,它创建了一个连接,并通过xhr进行了很少的轮询。在采用https的cloudflare上,它会进行大量轮询,不断重新连接,并且似乎不是所有消息都会到达web客户端 消息到达cloudfl

  • 在代理上使用HTTP隧道时,TCP套接字有问题。 客户端(C++)打开到服务器(JAVA)的TCP套接字。我添加了对HTTP代理的支持。一切正常,客户端发送“HTTP Connect”请求如下所示,并在以下情况下继续普通TCP连接: 然而,如果代理中配置了空闲超时,并且没有发送实际数据,则尽管客户端每60秒发送TCP保持活动数据包,但连接将终止。空闲超时配置为10分钟。

  • 问题内容: 我使用TCP Keep-Alive选项来检测死连接。它与使用读取套接字的连接一起工作良好: Epoll等待通过套接字上的EPOLLIN | EPOLLHUP退出而没有问题。 但是,如果我尝试向套接字写很多东西,直到得到EAGAIN,然后轮询读写,则在断开电缆连接时不会出现错误: 如何解决呢? 有人看到过类似的问题吗? 有什么可能的方向吗? 编辑: 附加信息 当我使用wireshark监

  • 问题内容: 大家好。我有一个使用ServerSocket和Socket类用Java编写的服务器。 我希望能够检测并处理断开连接,然后在必要时重新连接新客户端。 检测客户端断开连接,关闭套接字然后接受新客户端的正确步骤是什么? 问题答案: 大概是在从套接字读取数据,也许是在输入流上使用包装器,例如BufferedReader。在这种情况下,当相应的读取操作返回-1(对于原始read()调用)或为nu

  • 我正在开发一个通过TCP/IP承载第三方设备的服务器,并且已经经历了突然的连接中断(设备通过蜂窝连接)。我需要找到一种方法来检测断开,而不必将数据写入设备本身。 我的简化套接字代码如下: 如有任何反馈,将不胜感激。