Java OpenShift上的Web套接字断开连接(使用WildFly 8.2.1)
我正在使用OpenShift和WildFly 8.2.1 final来实现新的HTML5 websocket。我使用教程来设置这个项目 每当我打开MyTest.html时,JavaScript都会记录以下内容:Java OpenShift上的Web套接字断开连接(使用WildFly 8.2.1),java,maven,websocket,openshift,wildfly,Java,Maven,Websocket,Openshift,Wildfly,我正在使用OpenShift和WildFly 8.2.1 final来实现新的HTML5 websocket。我使用教程来设置这个项目 每当我打开MyTest.html时,JavaScript都会记录以下内容: JS: Server Connected... JS: Server Disconnected... 服务器连接,然后立即断开连接。为什么?我做错了什么?有什么我遗漏的吗 这是模式代码--> serverendpoint.java package testing; import ja
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>
我的WS网站
var wsUri=“ws://”+document.location.hostname+”:8000“+document.location.pathname+“serverendpoint”;
var webSocket=新的webSocket(wsUri);
var messageTextArea=document.getElementById(“messageTextArea”);
webSocket.onopen=函数(消息){processOpen(消息);};
webSocket.onmessage=函数(消息){processMessage(消息);};
webSocket.onclose=函数(消息){processClose(消息);};
webSocket.onerror=函数(消息){processError(消息);};
函数processOpen(消息){
messageTextArea.value++=“JS:服务器已连接…”++“\n”;
}
函数processMessage(消息){
messageTextArea.value+=“JS:Receive from Server==>”+message.data+“\n”;
}
函数sendMessage(){
如果(textMessage.value!=“关闭”){
发送(textMessage.value);
messageTextArea.value+=“JS:Send to Server==>”+textMessage.value+“\n”;
textMessage.value=“”;
}else webSocket.close();
}
函数processClose(消息){
send(“JS:客户端已断开连接…”)
messageTextArea.value++=“JS:服务器已断开连接…”++“\n”;
}
函数processError(消息){
messageTextArea.value+=“JS:error…”+“\n”;
}
以及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>
4.0.0
测试
测试
战争
1.0-快照
测试
UTF-8
1.8
1.8
爪哇
JavaEEAPI
7
假如
openshift
测试
org.apache.maven.plugins
maven战争插件
2.3
假的
部署
根
谢谢你的帮助 openshift在套接字的端口编号上有一些奇怪的规则。你把它命名为一件事,把它称为另一件事。。。。 我想您希望服务器使用8080端口,让客户端找到自己的端口 注意:我只通过node.js在Open Shift使用过插座,我记得我在port#上拔了头发。。。这是我的密码:
self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
self.port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
我不知道这两个变量名的确切含义
你可能想
更新:我一直在复制这个问题,但都没有用。首先,我试图在本地主机(win7桌面)上复制教程,但进展不大。我使用的是JavaJDK1.8.0@,还有wildfly-8.2.1.Final。。。我可以让服务器显示在(带有wildfly启动屏幕),但我根本无法让它运行(404-未找到)。所以,我帮不了什么忙。。。然而
宾果。。。我必须将浏览器客户端中的url更改为http://localhost:8080/websocket-聊天/
现在我可以在桌面本地主机上正常工作了。明天我将推到OpenShift
所以我一直在尝试复制你对OpenShift的推送。我现在了解了您的pom.xml问题。真正的问题是你不能将文件FTP到OpenShift,你必须通过git来推送它们。javee7库非常庞大,pom.xml文件非常复杂。你不可能想把整个库都推到openshift,也不可能用简单的方法将POM文件集成到简单的环境中(我已经试过了!)我不断地遇到maven错误
这篇教程有点像黑客。。他在javee7库中生成war文件(避免了所有POM依赖性困难),然后将它们复制回virgin chat组件目录。然后他删除聊天组件目录中的几乎所有内容,包括源代码,然后通过git将war文件推送到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文件(duh.)
- 我经常使用不同的端口和ws://代码
- 当所有这些都完成后,我仍然得到与您相同的输出。我得到一个连接,当我尝试发送文本时,我得到一个JS错误,并立即断开连接
- rhc工具在windows机器上简直是个麻烦。事实上,我根本无法让它工作
- 当我仔细看代码时,套接字是通过html5实现的简单JS套接字。我的怀疑是该版本并不那么健壮。我更希望看到一个专用的JS套接字库,就像另一个注释一样,html5版本我们无法控制告诉服务器在哪个端口上托管套接字。我们唯一可以使用的代码是控件是客户端代码,这并不是我们真正想要的
- 唯一的测试方法是
├── pom.xml └── src └── main ├── java │ └── testing │ └── serverendpoint.java └── webapp ├── MyTest.html └── WEB-INF └── web.xml