WebSocketService.java 1.05 KB
package com.huaheng.framework.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WebSocketService {
    final public static String WEBSOCKET_LOGIN_NAME = "websocket_login_name";

    @Autowired
    private SimpMessageSendingOperations messaging;

    /**
     * 发送广播
     */
    public void broadcast(String topic, Payload payload) {
        messaging.convertAndSend(topic, payload);
    }

    public void broadcast(String topic, String payload) {
        messaging.convertAndSend(topic, payload);
    }

    public void broadcast(String topic, Payload payload, List<String> userList) {
        for (String userLoginName : userList) {
            messaging.convertAndSendToUser(userLoginName, topic, payload);
        }
    }

    public void broadcast(String topic, Payload payload, String user) {
        messaging.convertAndSendToUser(user, topic, payload);
    }
}