Java 玻璃鱼网袋不工作
我正在尝试使用websocket应用程序。作为我用于Glassfish的服务器,工作正常,但我在websocket方面有问题 在控制台中使用javascript运行html时出现错误404 web.xmlJava 玻璃鱼网袋不工作,java,gradle,websocket,glassfish,Java,Gradle,Websocket,Glassfish,我正在尝试使用websocket应用程序。作为我用于Glassfish的服务器,工作正常,但我在websocket方面有问题 在控制台中使用javascript运行html时出现错误404 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/n
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Simple web application </display-name>
</web-app>
在src/main/java/websockets中有一个WebSocketServer.java
package websockets;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.OnOpen;
import javax.websocket.OnError;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocketServer {
@OnMessage
public void onMessage(String message, Session session)
throws IOException, InterruptedException {
// Print the client message for testing purposes
System.out.println("Received: " + message);
// Send the first message to the client
session.getBasicRemote().sendText("This is the first server message");
// Send 3 messages to the client every 5 seconds
int sentMessages = 0;
while(sentMessages < 3){
Thread.sleep(5000);
session.getBasicRemote().
sendText("This is an intermediate server message. Count: "
+ sentMessages);
sentMessages++;
}
// Send a final message to the client
session.getBasicRemote().sendText("This is the last server message");
}
@OnOpen
public void onOpen () {
System.out.println("Client connected");
}
@OnClose
public void onClose () {
System.out.println("Connection closed");
}
@OnError
public void onError(Session aclientSession, Throwable aThrowable) {
System.out.println("Error : " + aclientSession); System.out.println("Error :" + aThrowable);
}
}
packagewebsockets;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.io.IOException;
导入javax.websocket.OnClose;
导入javax.websocket.OnMessage;
导入javax.websocket.OnOpen;
导入javax.websocket.Session;
导入javax.websocket.OnOpen;
导入javax.websocket.OnError;
导入javax.websocket.server.ServerEndpoint;
@ServerEndpoint(“/websocket”)
公共类WebSocketServer{
@OnMessage
公共void onMessage(字符串消息、会话)
抛出IOException、InterruptedException{
//出于测试目的打印客户端消息
System.out.println(“接收:+消息”);
//将第一条消息发送到客户端
session.getBasicRemote().sendText(“这是第一条服务器消息”);
//每5秒向客户端发送3条消息
int=0;
while(sentMessages<3){
睡眠(5000);
session.getBasicRemote()。
sendText(“这是一封中间服务器邮件。计数:”
+Sent消息);
sentMessages++;
}
//向客户端发送最后一条消息
session.getBasicRemote().sendText(“这是最后一条服务器消息”);
}
@奥诺彭
公共开放(){
System.out.println(“客户端连接”);
}
@一次
公共空间关闭(){
System.out.println(“连接关闭”);
}
@一个错误
public void onError(会话aclientSession,可丢弃可浏览){
System.out.println(“错误:+aclientSession”);System.out.println(“错误:+athroable”);
}
}
格雷德尔建房时没有出错。在中运行应用程序时(vagrant virtualmachine)
抛出错误:
到“ws://localhost:8080/web/WebSocket”的WebSocket连接失败:
WebSocket握手期间出错:意外响应代码:404
在glassfish中,在上下文根:web中添加应用程序
怎么了?你能帮助我吗?请谢谢:)因为在添加以下选项后,它可以工作:
atmosphere.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true");
atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "true");
或在web.xml中:
<init-param>
<param-name>org.atmosphere.useNative</param-name>
<param-value>true</param-value>
</init-param>
org.atmosphere.useNative
符合事实的
您应该将ServerEndPoint设置为一个值。ServerEndPoint(value=“/websocket”)任何人都可以评论一下,他对这个解决方案有什么不适吗?请详细说明原因
<init-param>
<param-name>org.atmosphere.useNative</param-name>
<param-value>true</param-value>
</init-param>