我想将JSON数据从Java应用程序发送到JavaScript。JSON是日志/统计数据,我想用JavaScript对其进行格式化,并在浏览器中实时查看。
我通过Socket
进行了尝试,但JavaScript的WebSocket
并没有真正接受Java中的Socket
。火狐告诉我它是can't establish an connection to ws://localhost:8000
。
Java套接字
ServerSocket serverSocket = new ServerSocket(8000);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println("Message");
JavaScript WebSocket
var exampleSocket = new WebSocket("ws://localhost:8000");
exampleSocket.onmessage = function (event) {
console.log(event.data);
};
如果这是不可能的,因为不兼容。将JSON发送到JavaScript的最简单方法是什么?
实际上有一个针对Java的WebSocket实现:
http://www.html5rocks.com/en/tutorials/websockets/basics/
然后在"服务器端实现">"Java">"Jetty"下http://www.eclipse.org/jetty/