use of org.java_websocket.server.WebSocketServer in project Java-OCA-OCPP by ChargeTimeEU.
the class WebSocketListener method open.
@Override
public void open(String hostname, int port, ListenerEvents handler) {
server = new WebSocketServer(new InetSocketAddress(hostname, port), drafts) {
@Override
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
WebSocketReceiver receiver = new WebSocketReceiver(message -> webSocket.send(message));
sockets.put(webSocket, receiver);
SessionInformation information = new SessionInformation.Builder().Identifier(clientHandshake.getResourceDescriptor()).InternetAddress(webSocket.getRemoteSocketAddress()).build();
handler.newSession(sessionFactory.createSession(new JSONCommunicator(receiver)), information);
}
@Override
public void onClose(WebSocket webSocket, int i, String s, boolean b) {
sockets.get(webSocket).disconnect();
sockets.remove(webSocket);
}
@Override
public void onMessage(WebSocket webSocket, String s) {
sockets.get(webSocket).relay(s);
}
@Override
public void onError(WebSocket webSocket, Exception e) {
}
@Override
public void onStart() {
}
};
server.start();
}
Aggregations