java WebSocket 简易聊天消息推送
2015-06-27 19:54
525 查看
分类:
HTML5WebSocket2014-11-18 20:021946人阅读评论(4)收藏举报
webSocket消息推送聊天
环境:
JDK.1.7.0_51
apache-tomcat-7.0.53
java jar包:tomcat-coyote.jar、tomcat-juli.jar、websocket-api.jar
ChatAnnotation消息发送类:
[java]
view plaincopyprint?
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import com.util.HTMLFilter;
/**
* WebSocket 消息推送服务类
* @author 胡汉三
*
* 2014-11-18 下午7:53:13
*/
@ServerEndpoint(value =
"/websocket/chat")
public class ChatAnnotation {
private static
final Log log = LogFactory.getLog(ChatAnnotation.class);
private static
final String GUEST_PREFIX = "Guest";
private static
final AtomicInteger connectionIds = new AtomicInteger(0);
private static
final Map<String,Object> connections =
new HashMap<String,Object>();
private final String nickname;
private Session session;
public ChatAnnotation() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
}
@OnOpen
public void start(Session session) {
this.session = session;
connections.put(nickname, this);
String message = String.format("* %s %s", nickname,
"has joined.");
broadcast(message);
}
@OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s",
nickname, "has disconnected.");
broadcast(message);
}
/**
* 消息发送触发方法
* @param message
*/
@OnMessage
public void incoming(String message) {
// Never trust the client
String filteredMessage = String.format("%s: %s",
nickname, HTMLFilter.filter(message.toString()));
broadcast(filteredMessage);
}
@OnError
public void onError(Throwable t)
throws Throwable {
log.error("Chat Error: " + t.toString(), t);
}
/**
* 消息发送方法
* @param msg
*/
private static
void broadcast(String msg) {
if(msg.indexOf("Guest0")!=-1){
sendUser(msg);
} else{
sendAll(msg);
}
}
/**
* 向所有用户发送
* @param msg
*/
public static
void sendAll(String msg){
for (String key : connections.keySet()) {
ChatAnnotation client = null ;
try {
client = (ChatAnnotation) connections.get(key);
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
log.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
/**
* 向指定用户发送消息
* @param msg
*/
public static
void sendUser(String msg){
ChatAnnotation c = (ChatAnnotation)connections.get("Guest0");
try {
c.session.getBasicRemote().sendText(msg);
} catch (IOException e) {
log.debug("Chat Error: Failed to send message to client", e);
connections.remove(c);
try {
c.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
c.nickname, "has been disconnected.");
broadcast(message);
}
}
}
HTMLFilter工具类:
[java]
view plaincopyprint?
/**
* HTML 工具类
*
* @author 胡汉三
*/
public final
class HTMLFilter {
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new
char[message.length()];
message.getChars(0, message.length(), content,
0);
StringBuilder result = new StringBuilder(content.length +
50);
for (int i =
0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
页面:
[html]
view plaincopyprint?
<%@ page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%
String path =
request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<?xml
version="1.0"
encoding="UTF-8"?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en">
<head>
<title>测试</title>
<style
type="text/css">
input#chat {
width: 410px
}
#console-container {
width: 400px;
}
#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}
#console p {
padding: 0;
margin: 0;
}
</style>
<script
type="text/javascript">
var Chat = {};
Chat.socket =
null;
Chat.connect = (function(host) {
if ('WebSocket' in window) {
Chat.socket =
new WebSocket(host);
} else if ('MozWebSocket' in window) {
Chat.socket =
new MozWebSocket(host);
} else {
Console.log('Error: WebSocket is not supported by this browser.');
return;
}
Chat.socket.onopen =
function () {
Console.log('Info: WebSocket connection opened.');
document.getElementById('chat').onkeydown =
function(event) {
if (event.keyCode == 13) {
Chat.sendMessage();
}
};
};
Chat.socket.onclose =
function () {
document.getElementById('chat').onkeydown =
null;
Console.log('Info: WebSocket closed.');
};
Chat.socket.onmessage =
function (message) {
Console.log(message.data);
};
});
Chat.initialize =
function() {
if (window.location.protocol == 'http:') {
Chat.connect('ws://' + window.location.host + '/socket2/websocket/chat');
} else {
Chat.connect('wss://' + window.location.host + '/socket2/websocket/chat');
}
};
Chat.sendMessage = (function() {
var message =
document.getElementById('chat').value;
if (message != '') {
Chat.socket.send(message);
document.getElementById('chat').value =
'';
}
});
var Console = {};
Console.log = (function(message) {
var console =
document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap =
'break-word';
p.innerHTML = message;
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop =
console.scrollHeight;
});
Chat.initialize();
document.addEventListener("DOMContentLoaded", function() {
// Remove elements with "noscript" class - <noscript> is not allowed in XHTML
var noscripts =
document.getElementsByClassName("noscript");
for (var i = 0; i
< noscripts.length; i++) {
noscripts[i].parentNode.removeChild(noscripts[i]);
}
}, false);
</script>
</head>
<body>
<div
class="noscript"><h2
style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></div>
<div>
<p>
<input
type="text" placeholder="请输入内容"
id="chat"
/>
</p>
<div
id="console-container">
<div
id="console"/>
</div>
</div>
</body>
</html>
程序发布时记得删除tomcat-coyote.jar、tomcat-juli.jar、websocket-api.jar这三个jar包在启动Tomcat。
程序截图,Guest0用户发送信息的信息,在后台进行了判断只发送给自己:
Guest1:
Guest2:
HTML5WebSocket2014-11-18 20:021946人阅读评论(4)收藏举报
webSocket消息推送聊天
环境:
JDK.1.7.0_51
apache-tomcat-7.0.53
java jar包:tomcat-coyote.jar、tomcat-juli.jar、websocket-api.jar
ChatAnnotation消息发送类:
[java]
view plaincopyprint?
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import com.util.HTMLFilter;
/**
* WebSocket 消息推送服务类
* @author 胡汉三
*
* 2014-11-18 下午7:53:13
*/
@ServerEndpoint(value =
"/websocket/chat")
public class ChatAnnotation {
private static
final Log log = LogFactory.getLog(ChatAnnotation.class);
private static
final String GUEST_PREFIX = "Guest";
private static
final AtomicInteger connectionIds = new AtomicInteger(0);
private static
final Map<String,Object> connections =
new HashMap<String,Object>();
private final String nickname;
private Session session;
public ChatAnnotation() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
}
@OnOpen
public void start(Session session) {
this.session = session;
connections.put(nickname, this);
String message = String.format("* %s %s", nickname,
"has joined.");
broadcast(message);
}
@OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s",
nickname, "has disconnected.");
broadcast(message);
}
/**
* 消息发送触发方法
* @param message
*/
@OnMessage
public void incoming(String message) {
// Never trust the client
String filteredMessage = String.format("%s: %s",
nickname, HTMLFilter.filter(message.toString()));
broadcast(filteredMessage);
}
@OnError
public void onError(Throwable t)
throws Throwable {
log.error("Chat Error: " + t.toString(), t);
}
/**
* 消息发送方法
* @param msg
*/
private static
void broadcast(String msg) {
if(msg.indexOf("Guest0")!=-1){
sendUser(msg);
} else{
sendAll(msg);
}
}
/**
* 向所有用户发送
* @param msg
*/
public static
void sendAll(String msg){
for (String key : connections.keySet()) {
ChatAnnotation client = null ;
try {
client = (ChatAnnotation) connections.get(key);
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
log.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
/**
* 向指定用户发送消息
* @param msg
*/
public static
void sendUser(String msg){
ChatAnnotation c = (ChatAnnotation)connections.get("Guest0");
try {
c.session.getBasicRemote().sendText(msg);
} catch (IOException e) {
log.debug("Chat Error: Failed to send message to client", e);
connections.remove(c);
try {
c.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
c.nickname, "has been disconnected.");
broadcast(message);
}
}
}
import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import com.util.HTMLFilter; /** * WebSocket 消息推送服务类 * @author 胡汉三 * * 2014-11-18 下午7:53:13 */ @ServerEndpoint(value = "/websocket/chat") public class ChatAnnotation { private static final Log log = LogFactory.getLog(ChatAnnotation.class); private static final String GUEST_PREFIX = "Guest"; private static final AtomicInteger connectionIds = new AtomicInteger(0); private static final Map<String,Object> connections = new HashMap<String,Object>(); private final String nickname; private Session session; public ChatAnnotation() { nickname = GUEST_PREFIX + connectionIds.getAndIncrement(); } @OnOpen public void start(Session session) { this.session = session; connections.put(nickname, this); String message = String.format("* %s %s", nickname, "has joined."); broadcast(message); } @OnClose public void end() { connections.remove(this); String message = String.format("* %s %s", nickname, "has disconnected."); broadcast(message); } /** * 消息发送触发方法 * @param message */ @OnMessage public void incoming(String message) { // Never trust the client String filteredMessage = String.format("%s: %s", nickname, HTMLFilter.filter(message.toString())); broadcast(filteredMessage); } @OnError public void onError(Throwable t) throws Throwable { log.error("Chat Error: " + t.toString(), t); } /** * 消息发送方法 * @param msg */ private static void broadcast(String msg) { if(msg.indexOf("Guest0")!=-1){ sendUser(msg); } else{ sendAll(msg); } } /** * 向所有用户发送 * @param msg */ public static void sendAll(String msg){ for (String key : connections.keySet()) { ChatAnnotation client = null ; try { client = (ChatAnnotation) connections.get(key); synchronized (client) { client.session.getBasicRemote().sendText(msg); } } catch (IOException e) { log.debug("Chat Error: Failed to send message to client", e); connections.remove(client); try { client.session.close(); } catch (IOException e1) { // Ignore } String message = String.format("* %s %s", client.nickname, "has been disconnected."); broadcast(message); } } } /** * 向指定用户发送消息 * @param msg */ public static void sendUser(String msg){ ChatAnnotation c = (ChatAnnotation)connections.get("Guest0"); try { c.session.getBasicRemote().sendText(msg); } catch (IOException e) { log.debug("Chat Error: Failed to send message to client", e); connections.remove(c); try { c.session.close(); } catch (IOException e1) { // Ignore } String message = String.format("* %s %s", c.nickname, "has been disconnected."); broadcast(message); } } }
HTMLFilter工具类:
[java]
view plaincopyprint?
/**
* HTML 工具类
*
* @author 胡汉三
*/
public final
class HTMLFilter {
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new
char[message.length()];
message.getChars(0, message.length(), content,
0);
StringBuilder result = new StringBuilder(content.length +
50);
for (int i =
0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
/** * HTML 工具类 * * @author 胡汉三 */ public final class HTMLFilter { public static String filter(String message) { if (message == null) return (null); char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuilder result = new StringBuilder(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return (result.toString()); } }
页面:
[html]
view plaincopyprint?
<%@ page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%
String path =
request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<?xml
version="1.0"
encoding="UTF-8"?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en">
<head>
<title>测试</title>
<style
type="text/css">
input#chat {
width: 410px
}
#console-container {
width: 400px;
}
#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}
#console p {
padding: 0;
margin: 0;
}
</style>
<script
type="text/javascript">
var Chat = {};
Chat.socket =
null;
Chat.connect = (function(host) {
if ('WebSocket' in window) {
Chat.socket =
new WebSocket(host);
} else if ('MozWebSocket' in window) {
Chat.socket =
new MozWebSocket(host);
} else {
Console.log('Error: WebSocket is not supported by this browser.');
return;
}
Chat.socket.onopen =
function () {
Console.log('Info: WebSocket connection opened.');
document.getElementById('chat').onkeydown =
function(event) {
if (event.keyCode == 13) {
Chat.sendMessage();
}
};
};
Chat.socket.onclose =
function () {
document.getElementById('chat').onkeydown =
null;
Console.log('Info: WebSocket closed.');
};
Chat.socket.onmessage =
function (message) {
Console.log(message.data);
};
});
Chat.initialize =
function() {
if (window.location.protocol == 'http:') {
Chat.connect('ws://' + window.location.host + '/socket2/websocket/chat');
} else {
Chat.connect('wss://' + window.location.host + '/socket2/websocket/chat');
}
};
Chat.sendMessage = (function() {
var message =
document.getElementById('chat').value;
if (message != '') {
Chat.socket.send(message);
document.getElementById('chat').value =
'';
}
});
var Console = {};
Console.log = (function(message) {
var console =
document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap =
'break-word';
p.innerHTML = message;
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop =
console.scrollHeight;
});
Chat.initialize();
document.addEventListener("DOMContentLoaded", function() {
// Remove elements with "noscript" class - <noscript> is not allowed in XHTML
var noscripts =
document.getElementsByClassName("noscript");
for (var i = 0; i
< noscripts.length; i++) {
noscripts[i].parentNode.removeChild(noscripts[i]);
}
}, false);
</script>
</head>
<body>
<div
class="noscript"><h2
style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></div>
<div>
<p>
<input
type="text" placeholder="请输入内容"
id="chat"
/>
</p>
<div
id="console-container">
<div
id="console"/>
</div>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>测试</title> <style type="text/css"> input#chat { width: 410px } #console-container { width: 400px; } #console { border: 1px solid #CCCCCC; border-right-color: #999999; border-bottom-color: #999999; height: 170px; overflow-y: scroll; padding: 5px; width: 100%; } #console p { padding: 0; margin: 0; } </style> <script type="text/javascript"> var Chat = {}; Chat.socket = null; Chat.connect = (function(host) { if ('WebSocket' in window) { Chat.socket = new WebSocket(host); } else if ('MozWebSocket' in window) { Chat.socket = new MozWebSocket(host); } else { Console.log('Error: WebSocket is not supported by this browser.'); return; } Chat.socket.onopen = function () { Console.log('Info: WebSocket connection opened.'); document.getElementById('chat').onkeydown = function(event) { if (event.keyCode == 13) { Chat.sendMessage(); } }; }; Chat.socket.onclose = function () { document.getElementById('chat').onkeydown = null; Console.log('Info: WebSocket closed.'); }; Chat.socket.onmessage = function (message) { Console.log(message.data); }; }); Chat.initialize = function() { if (window.location.protocol == 'http:') { Chat.connect('ws://' + window.location.host + '/socket2/websocket/chat'); } else { Chat.connect('wss://' + window.location.host + '/socket2/websocket/chat'); } }; Chat.sendMessage = (function() { var message = document.getElementById('chat').value; if (message != '') { Chat.socket.send(message); document.getElementById('chat').value = ''; } }); var Console = {}; Console.log = (function(message) { var console = document.getElementById('console'); var p = document.createElement('p'); p.style.wordWrap = 'break-word'; p.innerHTML = message; console.appendChild(p); while (console.childNodes.length > 25) { console.removeChild(console.firstChild); } console.scrollTop = console.scrollHeight; }); Chat.initialize(); document.addEventListener("DOMContentLoaded", function() { // Remove elements with "noscript" class - <noscript> is not allowed in XHTML var noscripts = document.getElementsByClassName("noscript"); for (var i = 0; i < noscripts.length; i++) { noscripts[i].parentNode.removeChild(noscripts[i]); } }, false); </script> </head> <body> <div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable Javascript and reload this page!</h2></div> <div> <p> <input type="text" placeholder="请输入内容" id="chat" /> </p> <div id="console-container"> <div id="console"/> </div> </div> </body> </html>可指定发送给某个用户,也可全部发送,详情见ChatAnnotation类的broadcast方法。
程序发布时记得删除tomcat-coyote.jar、tomcat-juli.jar、websocket-api.jar这三个jar包在启动Tomcat。
程序截图,Guest0用户发送信息的信息,在后台进行了判断只发送给自己:
Guest1:
Guest2:
相关文章推荐
- Java的3种Base64加密方法
- java中的匿名内部类总结
- Java内存分析
- 泛型程序设计
- java战斗系列-战斗MAVENPW结构
- Eclipse安装SVN插件
- Vijava 学习笔记之数据中心下(集群和非集群的资源池和ESXI)树形结构展示
- 转 Spring 组件 <context:component-scan base-pakage="">用法
- SSH框架整合(一) —— Spring与Struts2整合
- Java 基础总结(一)
- 关于Spring注解 @Service @Component @Controller @Repository 用法
- 2015062705 - EffactiveJava笔记 - 第38条 检查参数的有效性
- ubuntu14.04下解决eclipse菜单栏消失问题
- java学习之旅29--面向对象_02_面向对象的方式思考问题
- java 中多线程之间的通讯之等待唤醒机制
- Java基础——线程(一)
- java web学习
- java对图片的转码和解码 实用
- java web 使用json要加入的jar 文件
- MyEclipse 快捷键1(CTRL)