OpenShift 上的 Web 套接字断开连接(使用 WildFly 8.2.1) [英] Web socket disconnecting on OpenShift (with WildFly 8.2.1)

查看:28
本文介绍了OpenShift 上的 Web 套接字断开连接(使用 WildFly 8.2.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 OpenShift 和 WildFly 8.2.1 final 来实现新的 HTML5 websocket.我使用教程来设置这个项目.

I am using OpenShift and WildFly 8.2.1 final to implement the new HTML5 websocket. I used this tutorial to set this project up.

每当我打开 MyTest.html 时,JavaScript 都会记录以下内容:

Whenever I open my MyTest.html, this is what the JavaScript logs:

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

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

The server connects and then immediately disconnects. Why? What am I doing wrong? Is there something that I am missing?

这里是模式代码 -->

Here is mode code -->

serverendpoint.java

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:

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..."+"
";
        }
        function processMessage(message) {
            messageTextArea.value += "JS: Receive from Server ==> "+message.data+"
";
        }
        function sendMessage () {
            if (textMessage.value !="close") {
                webSocket.send(textMessage.value);
                messageTextArea.value += "JS: Send to Server ==> "+textMessage.value+"
";
                textMessage.value="";
            } else webSocket.close();
        }
        function processClose(message) {
            webSocket.send("JS: Client disconnected...")
            messageTextArea.value += "JS: Server Disconnected..."+"
";
        }
        function processError (message) {
            messageTextArea.value += "JS: error ..."+"
";
        }
    </script>
</body>
</html>

和 pom.xml 文件:

And the pom.xml file:

<?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 时正试图连接到一个不存在的端点.

I think you are trying to connect to a non-existing endpoint, when opening the websocket.

这个(注意你缺少/../):

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

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

...will work for your files deployed to WildFly as follows:

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

我已经在 OpenShift Online 上的 WildFly 10 墨盒上使用您的代码(使用修改后的端点路径)检查了这一点.

I've checked this using your code (with the modified endpoint path) on WildFly 10 cartridge on OpenShift Online.

这篇关于OpenShift 上的 Web 套接字断开连接(使用 WildFly 8.2.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆