/*
 * Copyright 2018 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flowci.core.common.manager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.flowci.core.common.domain.PushBody;
import com.flowci.core.common.domain.PushEvent;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author yang
 */
@Log4j2
@Component
public class SocketPushManager {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    public void push(String topic, PushEvent event, Object obj) {
        try {
            PushBody push = new PushBody(event, obj);
            String json = objectMapper.writeValueAsString(push);
            simpMessagingTemplate.convertAndSend(topic, json);
        } catch (IOException e) {
            log.warn(e.getMessage());
        }
    }

    public void push(String topic, byte[] bytes) {
        simpMessagingTemplate.convertAndSend(topic, bytes);
    }
}

@@ -1498,18 +1498,16 @@
  catch (
-IO
 Exceptio
@@ -1509,24 +1509,24 @@
 eption e) %7B%0A
+
             
@@ -1615,24 +1615,42 @@
 %5B%5D bytes) %7B%0A
+        try %7B%0A    
         simp
@@ -1697,16 +1697,96 @@
 bytes);%0A
+        %7D catch (Exception e) %7B%0A            log.warn(e.getMessage());%0A        %7D%0A
     %7D%0A%7D%0A

handle exception on ws send