Search in sources :

Example 1 with WebSocketServer

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();
}
Also used : SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) ClientHandshake(org.java_websocket.handshake.ClientHandshake) WebSocketServer(org.java_websocket.server.WebSocketServer) HashMap(java.util.HashMap) Draft(org.java_websocket.drafts.Draft) InetSocketAddress(java.net.InetSocketAddress) SessionInformation(eu.chargetime.ocpp.model.SessionInformation) List(java.util.List) Logger(org.apache.logging.log4j.Logger) WebSocket(org.java_websocket.WebSocket) DefaultSSLWebSocketServerFactory(org.java_websocket.server.DefaultSSLWebSocketServerFactory) LogManager(org.apache.logging.log4j.LogManager) WebSocketServer(org.java_websocket.server.WebSocketServer) InetSocketAddress(java.net.InetSocketAddress) ClientHandshake(org.java_websocket.handshake.ClientHandshake) WebSocket(org.java_websocket.WebSocket) SessionInformation(eu.chargetime.ocpp.model.SessionInformation)

Aggregations

SessionInformation (eu.chargetime.ocpp.model.SessionInformation)1 InetSocketAddress (java.net.InetSocketAddress)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SSLContext (javax.net.ssl.SSLContext)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 WebSocket (org.java_websocket.WebSocket)1 Draft (org.java_websocket.drafts.Draft)1 ClientHandshake (org.java_websocket.handshake.ClientHandshake)1 DefaultSSLWebSocketServerFactory (org.java_websocket.server.DefaultSSLWebSocketServerFactory)1 WebSocketServer (org.java_websocket.server.WebSocketServer)1