From 6d0d1e12777dcb0bfe1b8afdd3bcd7a253462f27 Mon Sep 17 00:00:00 2001 From: huangfeng Date: Thu, 26 Sep 2024 11:37:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0socket=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E5=B9=B6=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TestController.java | 61 ++++++------------- .../xymanager_common/model/MessageModel.java | 9 +++ .../socket/WebSocketServer.java | 25 +++++++- 3 files changed, 52 insertions(+), 43 deletions(-) create mode 100644 xymanager_common/src/main/java/com/shxy/xymanager_common/model/MessageModel.java diff --git a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TestController.java b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TestController.java index a52ecf2..9d909c5 100644 --- a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TestController.java +++ b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TestController.java @@ -1,57 +1,36 @@ package com.shxy.xymanager_admin.controller; -import com.shxy.xymanager_common.annotation.Log; + +import com.shxy.xymanager_common.base.BaseController; +import com.shxy.xymanager_common.base.ResponseReult; +import com.shxy.xymanager_framework.socket.WebSocketServer; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.annotation.Resource; -@Api(value = "测试接口", tags = "测试接口tags") -@Controller +@RestController +@Api(tags = {"测试接口"}) +@RequestMapping("test") @Slf4j -public class TestController { +public class TestController extends BaseController { - @ApiOperation(value = "测试", notes = "测试notes", httpMethod = "POST") - @ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")}) - @RequestMapping("/user/login") - @Log(title = "测试", type = "查询") - public String gettest() { - log.info("有一个沙雕点了链接"); - return "/api/index2"; - } + @Resource + WebSocketServer webSocketServer; - @GetMapping("/user/logins") - @Log(title = "测试", type = "查询") - public String hhtest() { - log.info("有一个沙雕点了链接"); - return "/api/index2.html"; + @GetMapping("sendMsg") + @ApiOperation("发送消息") + public void sendMsg(String msg) { + webSocketServer.sendNotice(msg); } -// @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST") -//// @ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")}) -// @RequestMapping("/login") -// @Log(title = "登录", type = "查询") -// public String login(String username, String password) { -// //获取主题对象 -// Subject subject = SecurityUtils.getSubject(); -// try { -// subject.login(new UsernamePasswordToken(username,password)); -// System.out.println("登录成功!!!"); -// return "登录成功"; -// } catch (UnknownAccountException e) { -// e.printStackTrace(); -// System.out.println("用户错误!!!"); -// } catch (IncorrectCredentialsException e) { -// System.out.println("密码错误!!!"); -// } -// return "登录失败"; -// } - + @GetMapping("test") + @ApiOperation("test") + public ResponseReult test() { + return ResponseReult.success("OK"); + } } diff --git a/xymanager_common/src/main/java/com/shxy/xymanager_common/model/MessageModel.java b/xymanager_common/src/main/java/com/shxy/xymanager_common/model/MessageModel.java new file mode 100644 index 0000000..828784a --- /dev/null +++ b/xymanager_common/src/main/java/com/shxy/xymanager_common/model/MessageModel.java @@ -0,0 +1,9 @@ +package com.shxy.xymanager_common.model; + +import lombok.Data; + +@Data +public class MessageModel { + int type; + String content; +} diff --git a/xymanager_framework/src/main/java/com/shxy/xymanager_framework/socket/WebSocketServer.java b/xymanager_framework/src/main/java/com/shxy/xymanager_framework/socket/WebSocketServer.java index 151b0c5..f40bb1b 100644 --- a/xymanager_framework/src/main/java/com/shxy/xymanager_framework/socket/WebSocketServer.java +++ b/xymanager_framework/src/main/java/com/shxy/xymanager_framework/socket/WebSocketServer.java @@ -1,5 +1,7 @@ package com.shxy.xymanager_framework.socket; +import com.shxy.xymanager_common.model.MessageModel; +import com.shxy.xymanager_common.util.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -29,12 +31,31 @@ public class WebSocketServer { log.info("剩余socket连接数" + sessionPools.size()); } - public void sendMessage(String message) { + public void sendWarning(String message) { + MessageModel msg = new MessageModel(); + msg.setType(1); + msg.setContent(message); + String json = JSONUtil.object2Json(msg); Iterator it = sessionPools.listIterator(); while (it.hasNext()) { Session session = it.next(); try { - session.getBasicRemote().sendText(message); + session.getBasicRemote().sendText(json); + } catch (Exception ignore) { + } + } + } + + public void sendNotice(String message) { + MessageModel msg = new MessageModel(); + msg.setType(2); + msg.setContent(message); + String json = JSONUtil.object2Json(msg); + Iterator it = sessionPools.listIterator(); + while (it.hasNext()) { + Session session = it.next(); + try { + session.getBasicRemote().sendText(json); } catch (Exception ignore) { } }