Java8 + Tomcat8 实现Websocket 例子

背景

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

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


第 7 章 WebSocket

环境:Java8 + Tomcat8

7.1. Server

package websocket;

/**
 * Websocket Server
 * 
 * @author netkiller<netkiller@msn.com>
 */

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

环境:Java8 + Tomcat8

7.1. Server

package websocket;

/**
 * Websocket Server
 * 
 * @author netkiller<netkiller@msn.com>
 */

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>


转载于:https://my.oschina.net/neochen/blog/670672

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值