package com.matrix.component.websoket; import com.matrix.core.tools.LogUtil; import net.sf.json.JSONObject; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; /** * 观察者实现类 * @author jyy */ public class WebSoketMessageRabbitObserver implements WebSoketMessageObserver { @Override public void userConnection(WebSocketSession session) { } @Override public void handleTextMessage(WebSocketSession session, TextMessage message) { WebSoketCommonMessage commonMessage = (WebSoketCommonMessage) JSONObject.toBean(JSONObject.fromObject(message.getPayload()), WebSoketCommonMessage.class); if (WebSoketCommonMessage.MESSAGE_TYPE_P2P == commonMessage.getMessageType()) { commonMessage.setUserId(session.getAttributes().get("userId")+""); LogUtil.info("发送消息给指定用户"+commonMessage.getTargetUserId()); WebSocketPushHandler.sendMessageToUser(commonMessage.getTargetUserId(), new TextMessage(commonMessage.toString())); }else if(WebSoketCommonMessage.MESSAGE_TYPE_GROUP == commonMessage.getMessageType()) { commonMessage.setUserId(session.getAttributes().get("userId")+""); LogUtil.info("发送广播消息"+commonMessage.getTargetUserId()); WebSocketPushHandler.sendMessagesToUsers(new TextMessage(commonMessage.toString())); } } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { } }