JavaScript
引入js,可以不用引入
<script type="text/javascript" src="${ctxStatic}/websocket/sockjs-0.3.4.min.js"></script>
javaScript
function websocketStart(){
var websocket = null;
//获取主机地址之后的目录如:/Tmall/index.jsp
var pathName=window.document.location.pathname;
//获取带"/"的项目名,如:/Tmall
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
//获取主机地址,如://localhost:8080
var ipPort = window.location.host;
//拼接连接
var url = "ws:"+ipPort+projectName+"/socket/${USER_NO}";
if ("WebSocket" in window)
{
// 打开一个 web socket
websocket = new WebSocket(url);
console.log("此浏览器支持websocket");
}else if('MozWebSocket' in window){
console.log("此浏览器只支持MozWebSocket");
}else{
alert("此浏览器只支持SockJS");
}
websocket.onopen = function (event) {
console.log('建立连接');
}
websocket.onclose = function (event) {
console.log('连接关闭');
}
websocket.onmessage = function (event) {
console.log('收到消息:' + event.data)
}
websocket.onerror = function (event) {
console.log('websocket通信发生错误!');
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function (event) {
websocket.close();
}
}
//建立webSocket连接
websocketStart();
JAVA
package com.thinkgem.jeesite.modules.base.websocket.server;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ServerEndpoint(value="/socket/{USER_NO}")
public class MsgWebSocket {
protected Logger logger = LoggerFactory.getLogger(getClass());
//存储当前websocket,使用set存储当前用户,防止当前用户账户登录多个
private static Map<String, Set<MsgWebSocket>> map = new HashMap<String, Set<MsgWebSocket>>();
//当前session
private Session currentSession;
//登录用户的no
private String user_no;
//连接websocket,存储websocket
@OnOpen
public void onOpne(@PathParam("USER_NO") String user_no, Session session) {
Set<MsgWebSocket> set = new HashSet<MsgWebSocket>();
this.currentSession = session;
this.user_no = user_no;
if(map.containsKey(this.user_no)) {
logger.info("websocket连接成功用户NO【"+user_no+"】,当前用户已有其他终端登录,总共登录了【"+map.get(this.user_no).size()+"】个客户端");
map.get(this.user_no).add(this);
}else {
set.add(this);
map.put(this.user_no, set);
logger.info("websocket连接成功用户NO【"+user_no+"】");
}
}
//移除
@OnClose
public void onClose() {
//如果是正常退出,则从map中删除当前登录人的信息,否者不清空
logger.info("============UserNo:"+ this.user_no +" remove start mapSize:"+ map.get(this.user_no).size() +" ============");
map.get(this.user_no).remove(this);
logger.info("============ remove end mapSize:"+ map.get(this.user_no).size() +" ============");
}
// 给指定用户发送消息
public void sendUserMsg(String user_no, String msg) {
try {
// 获取要接受消息的用户集合
Set<MsgWebSocket> set = map.get(user_no);
// 判断 该用户是否有登录,如果没有则不执行消息推送
if(set != null) {
// 循环获取当前登录该账户的用户
for(MsgWebSocket msgWebSocket : set) {
// 给用户发送消息
msgWebSocket.currentSession.getBasicRemote().sendText(msg);
}
}
} catch (IOException e) {
logger.error("WebSocket消息发送IO异常");
} catch (Exception e) {
logger.error("WebSocket消息发送异常");
}
logger.info("给用户NO【"+user_no+"】发送消息,成功发送");
}
// 给所有在线用户发送消息
public void sendAllUserMsg(String msg) {
try {
// 获取所有Key
Set<String> userNoSet = map.keySet();
for(String key : userNoSet) {
Set<MsgWebSocket> set = map.get(key);
if(set != null) {
// 循环获取当前登录该账户的用户
for(MsgWebSocket msgWebSocket : set) {
// 给用户发送消息
msgWebSocket.currentSession.getBasicRemote().sendText(msg);
}
}
}
} catch (IOException e) {
logger.error("WebSocket消息发送IO异常");
} catch (Exception e) {
logger.error("WebSocket消息发送异常");
}
logger.info("给当前所有在线的用户推送消息,消息发送成功");
}
}
注意说明
@ServerEndpoint(value="/socket/{USER_NO}")
因为ServerEndpoint这些注解是java ee7的内容,tomcat得要8.0才能支持