package cn.superdesk.uniorder.controller._test;

import cn.superdesk.uniorder.dubbo.consumer.websocket.WebSocketDubboCustomerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test/websocket/command")
public class WebsocketCommandController {

	@Resource
	private WebSocketDubboCustomerService webSocketDubboCustomerService;


	/**
	 * http://localhost:8089/web/test/websocket/command/get/send?token=token1&message=fuck
	 *
	 * @param token
	 * @param message
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/get/send")
	public String sendGet(@RequestParam("token") /* identifier */String token,
	                      @RequestParam("message") String message) throws Exception {
		webSocketDubboCustomerService.sendGet(token, message);
		return ("发送成功");
	}

	@PostMapping("post/send")
	public String sendPost(@RequestParam("token") /* identifier */String token, @RequestBody String body) throws Exception {
		webSocketDubboCustomerService.sendPost(token, body);
		return ("发送成功");
	}

	@PostMapping("broadcast")
	public String broadcast(@RequestBody String body) throws Exception {
		webSocketDubboCustomerService.broadcast(body);
		return ("发送成功");
	}


	@PostMapping("post/send/another")
	public String sendPostAnother(@RequestParam("token") String token, @RequestBody String body) throws Exception {
		webSocketDubboCustomerService.sendPostAnother(token, body);
		return ("发送成功");
	}

	@PostMapping("broadcast/another")
	public String broadcastAnother(@RequestBody String body) throws Exception {
		webSocketDubboCustomerService.broadcast(body);
		return ("发送成功");
	}


	@GetMapping("clients")
	public String getClientsNum() {
		return ("目前在线:" + webSocketDubboCustomerService.getClientsNum());
	}
}
