我想推送应该使用websockets连续更新到多个客户端的服务器时间。我在下面给出了netbeans日志中的错误。我应该做什么来将服务器时间推送到多个客户端而没有任何错误。 服务器代码:
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());
}
}
}
}
客户代码:
<%@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>
我在netbeans日志中遇到的错误是:
14-Feb-2018 10:13:18.431 INFO [http-nio-8080-exec-18] org.apache.catalina.core.StandardContext.reload重新加载具有名称[/ ServerPush]的上下文已启动 201-Feb-2018 10:13:18.453 INFO [http-nio-8080-exec-18] org.apache.catalina.core.StandardContext.reload重新加载具有名称[/ ServerPush]的上下文已完成 201-Feb-2018 10:13:21.572 INFO [http-nio-8080-exec-19] org.apache.catalina.core.StandardContext.reload重新加载具有名称[/ ServerPush]的上下文已启动 201-Feb-2018 10:13:21.598 INFO [http-nio-8080-exec-19] org.apache.catalina.core.StandardContext.reload重新加载具有名称[/ ServerPush]的上下文已完成 18-Feb-2018 10:13:21.847 SEVERE [http-nio-8080-exec-24] org.apache.coyote.AbstractProtocol $ ConnectionHandler.process错误读取请求,被忽略 显示java.lang.NullPointerException 在org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:58) 在org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:64) 在org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133) at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:800) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1376) 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在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) 在java.lang.Thread.run(Thread.java:748)