xieb
2023-10-17 e60ea14897e4512efe505b8b3aea7cb2e49df39f
src/main/java/com/dji/sample/component/websocket/service/impl/SendMessageServiceImpl.java
@@ -3,13 +3,17 @@
import com.dji.sample.component.websocket.config.ConcurrentWebSocketSession;
import com.dji.sample.component.websocket.model.CustomWebSocketMessage;
import com.dji.sample.component.websocket.service.ISendMessageService;
import com.dji.sample.component.websocket.service.IWebSocketManageService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.TextMessage;
import java.io.IOException;
import java.util.Collection;
import java.util.Objects;
/**
 * @author sean.zhou
@@ -19,6 +23,12 @@
@Service
@Slf4j
public class SendMessageServiceImpl implements ISendMessageService {
    @Autowired
    private ObjectMapper mapper;
    @Autowired
    private IWebSocketManageService webSocketManageService;
    @Override
    public void sendMessage(ConcurrentWebSocketSession session, CustomWebSocketMessage message) {
@@ -33,7 +43,6 @@
                return;
            }
            ObjectMapper mapper = new ObjectMapper();
            session.sendMessage(new TextMessage(mapper.writeValueAsBytes(message)));
        } catch (IOException e) {
@@ -50,17 +59,17 @@
        try {
            ObjectMapper mapper = new ObjectMapper();
            TextMessage data = new TextMessage(mapper.writeValueAsBytes(message));
            for (ConcurrentWebSocketSession session : sessions) {
                if (!session.isOpen()) {
                    session.close();
                    log.debug("This session is closed.");
                    return;
                if (!Objects.isNull(session)) {
                    if (!session.isOpen()) {
                        session.close();
                        log.debug("This session is closed.");
                        return;
                    }
                    session.sendMessage(data);
                }
                session.sendMessage(data);
            }
        } catch (IOException e) {
@@ -69,4 +78,25 @@
            e.printStackTrace();
        }
    }
}
    @Override
    public void sendBatch(String workspaceId, Integer userType, String bizCode, Object data) {
        if (!StringUtils.hasText(workspaceId)) {
            throw new RuntimeException("项目id不存在:"+workspaceId);
        }
        Collection<ConcurrentWebSocketSession> sessions = Objects.isNull(userType) ?
                webSocketManageService.getValueWithWorkspace(workspaceId) :
                webSocketManageService.getValueWithWorkspaceAndUserType(workspaceId, userType);
        this.sendBatch(sessions, CustomWebSocketMessage.builder()
                        .data(data)
                        .timestamp(System.currentTimeMillis())
                        .bizCode(bizCode)
                        .build());
    }
    @Override
    public void sendBatch(String workspaceId, String bizCode, Object data) {
        this.sendBatch(workspaceId, null, bizCode, data);
    }
}