package org.onetwo.boot.core.web.socket;

import org.onetwo.common.utils.LangUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;

public class StompMessageSender {
	@Autowired
	private WebSocketProperties properties;
	
	@Autowired
	private SimpMessageSendingOperations messageSendingOperations;
	
	private String defaultUserDestination  = "/inbox";

	/***
	 * 默认把第一个前缀作为广播地址
	 * @param payload
	 */
	public void sendToBroadcast(Object payload) {
		String destination = LangUtils.getFirst(properties.getBroker().getSimplePrefixes());
		sendTo(destination, payload);
	}
	
	public void sendTo(String destination, Object payload) {
		messageSendingOperations.convertAndSend(destination, payload);
	}

	/***
	 * 发送到某个用户的邮箱，即点对点消息
	 * 用户邮箱默认的destination为：inbox
	 * @param user
	 * @param payload
	 */
	public void sendToUserInbox(String user, Object payload) {
		this.sendToUser(user, defaultUserDestination, payload);
	}
	
	public void sendToUser(String user, String destination, Object payload) {
		messageSendingOperations.convertAndSendToUser(user, destination, payload);
	}

	public SimpMessageSendingOperations getMessageSendingOperations() {
		return messageSendingOperations;
	}

	public void setMessageSendingOperations(SimpMessageSendingOperations messageSendingOperations) {
		this.messageSendingOperations = messageSendingOperations;
	}
	
}

@@ -77,16 +77,60 @@
 gUtils;%0A
+import org.onetwo.common.utils.StringUtils;%0A
 import o
@@ -689,32 +689,389 @@
  payload);%0A%09%7D%0A%09%0A
+%09/***%0A%09 * %E5%8F%91%E9%80%81%E5%88%B0 /broadcast/%7Bsubpath%7D%0A%09 * %E9%BB%98%E8%AE%A4%E6%8A%8A%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%89%8D%E7%BC%80%E4%BD%9C%E4%B8%BA%E5%B9%BF%E6%92%AD%E5%9C%B0%E5%9D%80%0A%09 * @param subpath%0A%09 * @param payload%0A%09 */%0A%09public void sendToBroadcast(String subpath, Object payload) %7B%0A%09%09String destination = LangUtils.getFirst(properties.getBroker().getSimplePrefixes());%0A%09%09destination = destination + StringUtils.appendStartWithSlash(subpath);%0A%09%09sendTo(destination, payload);%0A%09%7D%0A%09%0A
 %09public void sen

fix stomp sender