package com.smartAnji.control.utils; |
import java.io.IOException; |
/** |
* WebSocketService |
*/ |
@ServerEndpoint (value = "/websocket" , configurator = GetHttpSessionConfigurator. class ) |
public class WebSocketService { |
// concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 |
private static CopyOnWriteArraySet<WebSocketService> webSocketSet = new CopyOnWriteArraySet<WebSocketService>(); |
// websocket自带了session,与某个客户端的连接会话,需要通过它来给客户端发送数据 |
private Session session; |
private HttpSession httpSession; // HttpSession协议,jsp的session |
// 通信流程 : pageName(httpsession) <--> session(webSocketSet) |
private String pageName; // 用户名(网页名) |
private static List<String> pageList = new ArrayList<String>(); // 在线用户列表(静态) |
// pageName和session绑定的路由表,由pageName找到相应的session |
private static Map<String, Session> onlineUsers = new HashMap<String, Session>(); |
/** |
* 连接建立成功调用的方法 |
* |
* @param session |
* 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 |
* @throws IOException |
*/ |
@OnOpen |
public void onOpen(Session session, EndpointConfig config) throws IOException { |
this .session = session; |
this .httpSession = (HttpSession) config.getUserProperties().get(HttpSession. class .getName()); |
this .pageName = (String) this .httpSession.getAttribute( "user" ); // 获取当前用户 |
if (pageName != null ) { |
onlineUsers.put(pageName, this .session); |
pageList.add(pageName); // 加入pagelist中 |
webSocketSet.add( this ); // 加入webSocketSet中 |
System.out.println(pageName + " ----> 新连接加入!当前在线人数为 " + pageList.size()); |
} |
} |
/** |
* 连接关闭调用的方法 |
*/ |
@OnClose |
public void onClose() { |
if (onlineUsers.containsKey(pageName)) { |
webSocketSet.remove( this ); // 从set中删除 |
pageList.remove(pageName); // 从在线列表移除这个用户 |
onlineUsers.remove(pageName); // 从路由表删除 |
System.out.println(pageName + "移除成功" ); |
System.out.println(pageName + "关闭!当前在线人数为" + pageList.size()); |
} |
} |
/** |
* 发生错误时调用 |
* |
* @param session |
* @param error |
*/ |
@OnError |
public void onError(Session session, Throwable error) { |
System.out.println( "发生错误" ); |
error.printStackTrace(); |
} |
/** |
* 收到客户端消息后调用的方法 |
* |
* @param message |
* 客户端发出的消息 |
* @throws IOException |
*/ |
@OnMessage |
public void onMessage(String message) throws IOException { |
HashMap<String, String> messageMap = MessageUtil.getMessage(message); // 处理消息类 |
String fromName = messageMap.get( "fromName" ); // 消息来自人 的userId |
String toName = messageMap.get( "toName" ); // 消息发往人的 userId |
String mapContent = messageMap.get( "content" ); |
String type = messageMap.get( "type" ); |
if (MessageUtil.MESSAGE.equals(type)) { |
if ( "all" .equals(toName)) { |
String content = MessageUtil.sendContent(MessageUtil.MESSAGE, mapContent); |
System.out.println( "++++++++++++++++++++" +content); |
broadcastAll(content); |
} else { |
try { |
singleChat(fromName, toName, mapContent); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
} |
} |
/** |
* 广播消息的方法 |
* |
* @param message |
* @throws IOException |
*/ |
private static void broadcastAll(String message) throws IOException { |
// 群发消息 |
for (WebSocketService item : webSocketSet) { |
try { |
item.session.getBasicRemote().sendText(message); |
} catch (IOException e) { |
e.printStackTrace(); |
continue ; |
} |
} |
} |
/** |
* 对特定用户发送消息的方法 |
* |
* @param fromName |
* @param toName |
* @param mapContent |
* @throws IOException |
*/ |
private void singleChat(String fromName, String toName, String mapContent) throws IOException { |
String contentTemp = MessageUtil.sendContent(MessageUtil.MESSAGE, mapContent); |
for (String pageName : pageList) { |
if ( "navPage" .equals(pageName) || "overPage" .equals(pageName)) { |
session = onlineUsers.get(pageName); |
session.getBasicRemote().sendText(contentTemp); |
} else { |
System.out.println( "客户端未打开!" ); |
} |
} |
// if (pageList.contains(toName)) { |
// session = onlineUsers.get(toName); |
// session.getBasicRemote().sendText(contentTemp); |
// } else { |
// System.out.println("客户端未打开!"); |
// } |
} |
} |
by: 发表于:2017-08-29 16:24:59 顶(0) | 踩(0) 回复
??
回复评论