Java8 + Tomcat8 实现Websocket 例子

背景

公司网站上线需要一个实时报价功能,于是想到了 Websocket, 我在多年前就尝试过Websocket 那时 HTML5标准尚未产生,各家实现均有不同,Websocket 版本也非常多,实现兼容也比较复杂,于是放弃Websocket。最近看到Websocket 开始流行起来,兼容也不再是障碍。

以下节选自《Netkiller Java 手札》,下面例子实现一个简单的 Echo Server


第 7 章 WebSocket

目录

7.1. Server 7.2. Client

环境:Java8 + Tomcat8

7.1. Server

package websocket;

/**
 * Websocket Server
 * 
 * @author netkiller<[email protected]>
 */

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/echo")
public class PriceServer {

	private Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());

	/**
	 * Callback hook for Connection open events. This method will be invoked
	 * when a client requests for a WebSocket connection.
	 * 
	 * @param session
	 *            the session which is opened.
	 */
	@OnOpen
	public void onOpen(Session session) {
		sessions.add(session);
	}

	/**
	 * Callback hook for Connection close events. This method will be invoked
	 * when a client closes a WebSocket connection.
	 * 
	 * @param session
	 *            the session which is opened.
	 */
	@OnClose
	public void onClose(Session session) {
		sessions.remove(session);
	}

	/**
	 * Callback hook for Message Events. This method will be invoked when a
	 * client send a message.
	 * 
	 * @param message
	 *            The text message
	 * @param session
	 *            The session of the client
	 */
	@OnMessage
	public void onMessage(String message, Session session) {
		System.out.println("Message Received: " + message);
		for (Session remote : sessions) {
			System.out.println("Sending to " + remote.getId());
			remote.getAsyncRemote().sendText(message);
		}
	}
}



第 7 章 WebSocket

目录

7.1. Server 7.2. Client

环境:Java8 + Tomcat8

7.1. Server

package websocket;

/**
 * Websocket Server
 * 
 * @author netkiller<[email protected]>
 */

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/echo")
public class PriceServer {

	private Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());

	/**
	 * Callback hook for Connection open events. This method will be invoked
	 * when a client requests for a WebSocket connection.
	 * 
	 * @param session
	 *            the session which is opened.
	 */
	@OnOpen
	public void onOpen(Session session) {
		sessions.add(session);
	}

	/**
	 * Callback hook for Connection close events. This method will be invoked
	 * when a client closes a WebSocket connection.
	 * 
	 * @param session
	 *            the session which is opened.
	 */
	@OnClose
	public void onClose(Session session) {
		sessions.remove(session);
	}

	/**
	 * Callback hook for Message Events. This method will be invoked when a
	 * client send a message.
	 * 
	 * @param message
	 *            The text message
	 * @param session
	 *            The session of the client
	 */
	@OnMessage
	public void onMessage(String message, Session session) {
		System.out.println("Message Received: " + message);
		for (Session remote : sessions) {
			System.out.println("Sending to " + remote.getId());
			remote.getAsyncRemote().sendText(message);
		}
	}
}



7.2. Client

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<script language="JavaScript">
		var wsuri = "ws://localhost:8080/m.example.com/echo";
		var ws = null;

		function connectEndpoint() {
			ws = new WebSocket(wsuri);
			ws.onmessage = function(evt) {
				//alert(evt.data);
				document.getElementById("echo").value = evt.data;
			};

			ws.onclose = function(evt) {
				//alert("close");
				document.getElementById("echo").value = "end";
			};

			ws.onopen = function(evt) {
				//alert("open");
				document.getElementById("echo").value = "open";
			};
		}

		function sendmsg() {
			ws.send(document.getElementById("send").value);
		}
	</script>
<body onload="connectEndpoint()">
	<input type="text" size="20" value="5" id="send">
	<input type="button" value="send" onclick="sendmsg()">
	<br>
	<input type="text" id="echo">
</body>
</html>

Java8 + Tomcat8 实现Websocket 例子_第1张图片


你可能感兴趣的:(java,websocket,netkiller)