【问题标题】:Web socket disconnecting on OpenShift (with WildFly 8.2.1)OpenShift 上的 Web 套接字断开连接(使用 WildFly 8.2.1)
【发布时间】:2016-02-09 14:38:37
【问题描述】:

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

每当我打开 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>

感谢您的帮助!

【问题讨论】:

  • Open shift 对套接字的端口编号有一些奇怪的规则。我不记得细节了。

标签: java maven websocket openshift wildfly


【解决方案1】:

Open shift 对套接字的端口编号有一些奇怪的规则。您将其命名为一件事,将其称为另一件事....

我想你希望服务器使用 8080 端口,让客户端找到自己的端口。

注意:我只在 Open Shift 中通过 node.js 使用过套接字,我记得我在端口 # 上拔过头发。 ...这是我的代码:

self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
self.port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;    

我不知道这两个变量名到底是什么..

您可能想check out this link.

更新:我一直在努力重现该问题,但无济于事。首先,我试图在我的本地主机(win7 桌面)上重现该教程,但我没有取得太大进展。我正在使用 Java JDK 1.8.0_65,使用的是 wildfly-8.2.1.Final... 我可以让服务器显示在 http://localhost:8080/(带有 wildfly 启动屏幕),但我无法获得 http://localhost:8080/chat/完全没有功能(404 - 未找到)。所以,我帮不上什么忙……但是。

Bingo... 我不得不将浏览器客户端中的 url 更改为 http://localhost:8080/websocket-chat/,所以现在我可以在桌面本地主机上运行。明天我会推送到 OpenShift。

所以我一直在尝试复制您对 OpenShift 的推送。我现在了解您的 pom.xml 问题。真正的问题是您无法将文件 FTP 到 OpenShift,您必须通过 git 推送它们。该库 javee7-libraries 很大,并且 pom.xml 文件非常交织在一起。您绝对不想将整个库推送到 openshift,并且没有简单的方法可以将 POM 文件集成到简单的状态(我已经尝试过!)我不断收到 maven 错误。

本教程有点像 hack.. 他在 javee7 库中生成 war 文件(并避免所有 POM 依赖困难),然后将它们复制回原始聊天组件目录。然后他删除了聊天组件目录中的几乎所有内容,包括源代码,然后通过 git 将战争文件推送到 openshift。啊。注意:我通过 atom.io 和命令行来完成所有这些工作。

您尝试做的事情对我来说更有意义,但我无法为独立聊天目录找出正确的 pom.xml 以避免 maven 错误。

我大部分时间都在处理这个问题。这是我学到的。

  • 我能够让示例在我的本地主机上正常工作。
  • 您的 pom.xml(随“WildFly Application Server 8.2.1.Final”插件一起提供)绝对正确,测试文件较少。
  • 您必须使用命令 '$ mvn -f pom.xml package -Popenshift` 生成 .war 文件
  • 为了通过 git 将 .war 文件推送到 OpenShift 服务器,您必须修改 .gitignore 文件,使其能够识别 .war 文件(呃。)
  • 我玩了很多不同的端口号和 ws:// 代码
  • 完成所有这些后,我仍然得到与您相同的输出。我建立了连接,当我尝试发送文本时,我收到一个 JS 错误并立即断开连接。
  • rhc 工具在 windows 机器上是个痛点。事实上,我根本无法让它发挥作用。
  • 当我仔细查看代码时,套接字的东西是通过 html5 对套接字的简单 JS 实现。我怀疑该版本不是那么强大。我更愿意看到像sock.js 这样的专用JS 套接字库另一个注意事项是,对于html5 版本,我们无法控制告诉服务器托管套接字的端口。我们可以控制的唯一代码是客户端代码,这并不是我们真正想要的。
  • 唯一的测试方法是使用库更新 javascript 代码并移动它。

【讨论】:

  • 哦,真的。我在他们的网站上读到,对于网络套接字,它是 8000。但让我试试 8080。
  • 用8080端口,肯定不行。还有其他建议吗?
  • 我一定要测试端口 8080,我在 MyTest.html 中更改了这行代码(var wsUri = "ws://" + document.location.hostname + ":8000" + document.location.pathname + "server endpoint";) to this--> ( var wsUri = "ws://" + document.location.hostname + ":8080" + document.location.pathname + "server endpoint"; ) 这不起作用。它现在甚至不告诉我它已连接
  • 我稍微更新了我的答案。查看https://developers.openshift.com/en/managing-environment-variables.html
  • 哎哟。我刚刚阅读了您的原始教程链接。一年前,我将上传的那张图片存储在我的笔记中。你能让教程运行纯吗?
【解决方案2】:

我认为您在打开 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 盒式磁带上使用您的代码(使用修改后的端点路径)检查了这一点。

【讨论】:

  • @Jira Fiala 我正在尝试调用一个需要名为tr.txt 的文件位置的方法。 tr.txtpom.xmlsrc 具有相同的层次结构。由于它位于文件夹的正下方,我告诉该方法查找名称为“tr.txt”的文件,但打开的 shift wildfly 服务器给了我找不到文件的错误。是否需要在 tr.txt 前面加上/../ 或其他内容才能找到它?
  • 我也尝试将 tr.txt 文件(java 类使用)放在testing 文件夹下,但我得到相同的文件未找到错误。
  • 我也有类似的问题。你能看看这个 SO 帖子:stackoverflow.com/questions/39112243/…。我已尽力尝试修复它,但似乎没有任何效果。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
相关资源
最近更新 更多