cf28a5e61414d917bfd363e1988777d10d0efc55.svn-base 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package org.jeecg.modules.message.websocket;
  2. import org.jeecg.common.api.vo.Result;
  3. import org.jeecg.common.constant.WebsocketConst;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import com.alibaba.fastjson.JSONObject;
  10. @RestController
  11. @RequestMapping("/sys/socketTest")
  12. public class TestSocketController {
  13. @Autowired
  14. private WebSocket webSocket;
  15. @PostMapping("/sendAll")
  16. public Result<String> sendAll(@RequestBody JSONObject jsonObject) {
  17. Result<String> result = new Result<String>();
  18. String message = jsonObject.getString("message");
  19. JSONObject obj = new JSONObject();
  20. obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
  21. obj.put(WebsocketConst.MSG_ID, "M0001");
  22. obj.put(WebsocketConst.MSG_TXT, message);
  23. webSocket.sendMessage(obj.toJSONString());
  24. result.setResult("群发!");
  25. return result;
  26. }
  27. @PostMapping("/sendUser")
  28. public Result<String> sendUser(@RequestBody JSONObject jsonObject) {
  29. Result<String> result = new Result<String>();
  30. String userId = jsonObject.getString("userId");
  31. String message = jsonObject.getString("message");
  32. JSONObject obj = new JSONObject();
  33. obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_USER);
  34. obj.put(WebsocketConst.MSG_USER_ID, userId);
  35. obj.put(WebsocketConst.MSG_ID, "M0001");
  36. obj.put(WebsocketConst.MSG_TXT, message);
  37. webSocket.sendMessage(userId, obj.toJSONString());
  38. result.setResult("单发");
  39. return result;
  40. }
  41. }