我在运行Java EE应用程序时遇到了麻烦。我为服务器edpoint设置了以下代码:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.ApplicationScoped;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
@ApplicationScoped
@ServerEndpoint(
value = "/model"
)
public class ModelWebSocket {
private final ModelHandle model = new ModelHandle();
@OnOpen
public void onOpen(Session session) throws IOException {
}
@OnError
public void onError(Throwable error) {
/// put model back to pool, log erros
}
// This code never calls,
@OnMessage
public void onMessage(String message, Session session) {
//TODO: Add decoders/encoders to annotations
try {
ModelInputData inputData = new ModelInputDataDecoder().decode(message);
double input = inputData.getInput();
double timespan = inputData.getTimespan();
model.setInput(input);
ModelProcessData process = model.getOuptput(timespan);
try {
session.getBasicRemote().sendText(
new ModelProcessDataEncoder().encode(process)
);
} catch (IOException | EncodeException ex) {
Logger.getLogger(ModelWebSocket.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (DecodeException ex) {
Logger.getLogger(ModelWebSocket.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And the following stub for a client stub in javascript:
<!DOCTYPE html>
<!--
This is a test
-->
<html>
<head>
<title>Test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<!-- insert the main script here -->
<script type ="text/javascript">
//copied from https://learn.javascript.ru/websockets
var socket = new WebSocket(
"ws://localhost:8080/SimulatorWeb/model"
);
socket.onopen = function () {
alert("Connection OK");
};
socket.onclose = function (event) {
if (event.wasClean) {
alert('Connection closed');
} else {
alert('Connection interrupted'); // например, "убит" процесс сервера
}
alert('Code: ' + event.code + ' redason: ' + event.reason);
};
socket.onmessage = function (event) {
alert("Data accepted " + event.data);
};
socket.onerror = function (error) {
alert("Error " + error.message);
};
function send(){
socket.send("dummy");
}
</script>
<button type="button" onclick="send();" >Send</button>
</body>
</html>
客户端似乎发送消息,但OnMessage从未被截获。当我启动Glassfish 4.1.1时,我有以下警告:
- WARN: WELD-000411: Observer method [backkedannotatedmethod] privateorg.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType (@ObservesProcessAnnotatedType)接收所有注释的事件类型。考虑使用@WithAnnotations或a来限制事件带边界的泛型类型。警告:WELD-000411:观察者方法(BackedAnnotatedMethod)公共org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType (@ObservesProcessAnnotatedType)接收所有注释的事件类型。考虑使用@WithAnnotations或a来限制事件带边界的泛型类型。警告:WELD-000411:观察者方法(BackedAnnotatedMethod)org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType (@ObservesProcessAnnotatedType, BeanManager)接收所有事件注释类型。考虑使用@WithAnnotations限制事件
我花了很多时间调试,但我仍然无法找到问题…我使用Java 8 SDK和NetBeans IDE。
(在代码中,我没有使用解码器/编码器,因为我认为它们是问题并从ServerEnpoint中删除了它们)
我不知道Glassfish的问题是什么…我切换到Tomcat,并手动添加到WEB_INF/lib所有我需要的外部库(javax;Json)。Tomcat似乎没有给原始代码任何警告,一切都工作得很好。