Home pushing server time to multiple clients through websockets
Reply: 0

pushing server time to multiple clients through websockets

user4834
1#
user4834 Published in June 19, 2018, 4:10 pm

I want to push server time which should be updated continously to multiple clients using websockets.I am facing an error in netbeans log which i have given below.what i should do to push the server time to multiple client without any error. server code:

package nikhil;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnOpen;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.server.PathParam;
import javax.websocket.DeploymentException;

@ServerEndpoint("/jnc")
public class WebSocketClockn {


    static ScheduledExecutorService timer
            = Executors.newSingleThreadScheduledExecutor();

    private static Set<Session> allSessions;

    DateTimeFormatter timeFormatter
            = DateTimeFormatter.ofPattern("HH:mm:ss");

//    @OnOpen
    public void showTime(Session session) {
        allSessions = session.getOpenSessions();

        // start the scheduler on the very first connection
        // to call sendTimeToAll every second   
        if (allSessions.size() == 1) {
            timer.scheduleAtFixedRate(() -> sendTimeToAll(session), 0, 1, TimeUnit.SECONDS);
            System.out.println("server is running");
        }
    }

    private void sendTimeToAll(Session session) {
        allSessions = session.getOpenSessions();
        for (Session sess : allSessions) {
            try {
                sess.getBasicRemote().sendText("Local time: "
                        + LocalTime.now().format(timeFormatter));
                System.out.println("server is running");
            } catch (IOException ioe) {
                System.out.println(ioe.getMessage());
            }
        }
    }

}

client code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Getting server updates</h1>


        <script type="text/javascript">
            //window.onload = init;
            var socket = new WebSocket("ws://localhost:8080/ServerPush/jnc");
            socket.onopen = function () {
                console.log('Opened connection 🎉');
            }
            socket.onmessage = function (event) {
                console.log('typeof(event.data)');
                document.getElementById("result").innerHTML = event.data + "<br>";
            };
            socket.onerror = function (event) {
                console.log("Error ", event);
            }
        </script>
        <div id="result"></div>
    </body>
</html>

The error i am facing in netbeans log is:

14-Feb-2018 10:13:18.431 INFO [http-nio-8080-exec-18] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/ServerPush] has started 14-Feb-2018 10:13:18.453 INFO [http-nio-8080-exec-18] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/ServerPush] is completed 14-Feb-2018 10:13:21.572 INFO [http-nio-8080-exec-19] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/ServerPush] has started 14-Feb-2018 10:13:21.598 INFO [http-nio-8080-exec-19] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/ServerPush] is completed 14-Feb-2018 10:13:21.847 SEVERE [http-nio-8080-exec-24] org.apache.coyote.AbstractProtocol$ConnectionHandler.process Error reading request, ignored java.lang.NullPointerException at org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:58) at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:64) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319735 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO