本文共 5606 字,大约阅读时间需要 18 分钟。
import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import org.java_websocket.WebSocket;public class WsPool { private static final MapwsUserMap = new HashMap (); /** * 通过websocket连接获取其对应的用户 * * @param conn * @return */ public static String getUserByWs(WebSocket conn) { return wsUserMap.get(conn); } /** * 根据userName获取WebSocket,这是一个list,此处取第一个 * 因为有可能多个websocket对应一个userName(但一般是只有一个,因为在close方法中,我们将失效的websocket连接去除了) * * @param user */ public static WebSocket getWsByUser(String userName) { Set keySet = wsUserMap.keySet(); synchronized (keySet) { for (WebSocket conn : keySet) { String cuser = wsUserMap.get(conn); if (cuser.equals(userName)) { return conn; } } } return null; } /** * 向连接池中添加连接 * * @param inbound */ public static void addUser(String userName, WebSocket conn) { wsUserMap.put(conn, userName); // 添加连接 } /** * 获取所有连接池中的用户,因为set是不允许重复的,所以可以得到无重复的user数组 * * @return */ public static Collection getOnlineUser() { List setUsers = new ArrayList (); Collection setUser = wsUserMap.values(); for (String u : setUser) { setUsers.add(u); } return setUsers; } /** * 移除连接池中的连接 * * @param inbound */ public static boolean removeUser(WebSocket conn) { if (wsUserMap.containsKey(conn)) { wsUserMap.remove(conn); // 移除连接 return true; } else { return false; } } /** * 向特定的用户发送数据 * * @param user * @param message */ public static void sendMessageToUser(WebSocket conn, String message) { if (null != conn && null != wsUserMap.get(conn)) { conn.send(message); } } /** * 向所有的用户发送消息 * * @param message */ public static void sendMessageToAll(String message) { Set keySet = wsUserMap.keySet(); synchronized (keySet) { for (WebSocket conn : keySet) { String user = wsUserMap.get(conn); if (user != null) { conn.send(message); } } } }}
解释:这个是核心类 用于存放和移除查找ws用户信息的
import java.net.InetSocketAddress;import org.java_websocket.WebSocket;import org.java_websocket.handshake.ClientHandshake;import org.java_websocket.server.WebSocketServer;public class WsServer extends WebSocketServer { public WsServer(int port) { super(new InetSocketAddress(port)); } public WsServer(InetSocketAddress address) { super(address); } @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { // ws连接的时候触发的代码,onOpen中我们不做任何操作 } @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { //断开连接时候触发代码 userLeave(conn); } @Override public void onMessage(WebSocket conn, String message) { if(null != message &&message.startsWith("online")){ String userName=message.replaceFirst("online", "");//用户名 userJoin(conn,userName);//用户加入 }else if(null != message && message.startsWith("offline")){ userLeave(conn); } } @Override public void onError(WebSocket conn, Exception ex) { //错误时候触发的代码 ex.printStackTrace(); } /** * 去除掉失效的websocket链接 * @param conn */ private void userLeave(WebSocket conn){ WsPool.removeUser(conn); } /** * 将websocket加入用户池 * @param conn * @param userName */ private void userJoin(WebSocket conn,String userName){ WsPool.addUser(userName, conn); }}
解释:这个是登陆后与前端信息交互的
import org.java_websocket.WebSocketImpl;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Componentpublic class WsStart { @PostConstruct public void startWebSocket() { WebSocketImpl.DEBUG = false; new WsServer(10887).start(); }}
解释:项目启动时开启服务
WsPool.sendMessageToUser( WsPool.getWsByUser( (String)SecurityUtils.getSubject().getPrincipal()) , "msg");
解释:服务器指定push
var websocket = '';var last_health;var health_timeout = 10;var username = $('#principal').val();$(function(){ if(!username){ return; } initWs();})var initWs = function() { if(!$('#principal').val()){ return; } if (window.WebSocket) { websocket = new WebSocket( encodeURI('ws://' + document.domain + ':10887')); websocket.onopen = function() { $('.cover').html('已连接'+$('.cover').html()); websocket.send("online"+username); heartbeat_timer = setInterval(function() { keepalive(websocket) }, 60000); }; websocket.onerror = function() { $('.cover').html('连接发生错误'+$('.cover').html()); }; websocket.onclose = function() { $('.cover').html('已经断开连接'+$('.cover').html()); initWs(); }; // 消息接收 websocket.onmessage = function(message) { $('.cover').html(message.data+''+$('.cover').html()) }; } else { $('.cover').html('该浏览器不支持'+$('.cover').html()); }}// 心跳包function keepalive(ws) { var time = new Date(); if (last_health != -1 && (time.getTime() - last_health > health_timeout)) { // ws.close(); } else { if (ws.bufferedAmount == 0) { ws.send('~HC~'); } }}function wslogout(){ websocket.send("offline"+username);}解释:与服务器交互时的Js
解释:引入jar文件org.java-websocket Java-WebSocket 1.3.0
如果还有什么不全的请留言指出。谢谢!
转载地址:http://jmaqb.baihongyu.com/