Reputation: 94
I am having trouble with running my Java EE application. I have the folloving code for server 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>
Client seem to send message, but OnMessage is never intercepted. I have the following warn's when I start Glassfish 4.1.1:
I have spent I lot of time debugging, but I'm still not able to find the issue... I use Java 8 SDK and NetBeans IDE.
(In the code I use no decoders/encoders, because I thought that they were the issue and removed them from ServerEnpoint)
Upvotes: 0
Views: 472
Reputation: 94
I don't know what the issue with Glassfish is... I swithed to Tomcat and added manually to WEB_INF/lib all external libraries I needed (javax.json in my case). Tomcat seems to give no warnings to the original code and everything works just fine.
Upvotes: 0