package org.springblade.websocket.controller;

import lombok.AllArgsConstructor;
import org.springblade.websocket.model.ChatMessage;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Controller;

/**
 * ChatController
 *
 * @author rajeevkumarsingh
 */
@Controller
@AllArgsConstructor
public class ChatController {

	private final SimpMessageSendingOperations messagingTemplate;

	@MessageMapping("/videocloud.sendMessage")
//	@SendTo("/topic/public")
	public void sendMessage(@Payload ChatMessage chatMessage) {
		String routerKey = "/topic/" + chatMessage.getSender();
		System.out.println(routerKey+":rou");
		messagingTemplate.convertAndSend(routerKey, chatMessage);
		return;
	}

	@MessageMapping("/videocloud.addUser")
	@SendTo("/topic/public")
	public ChatMessage addUser(@Payload ChatMessage chatMessage,
							   SimpMessageHeaderAccessor headerAccessor) {
		// Add username in web socket session
		headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
		System.out.println("添加用户"+chatMessage.getSender());
		return chatMessage;
	}

}
