燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2023-11-06 52e6812c5cf6d139ae183afff976029c50adf4b4
1
已添加1个文件
已修改2个文件
83 ■■■■■ 文件已修改
src/main/java/com/yssh/controller/SuYuanController.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/service/SocketService.java 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/controller/SuYuanController.java
@@ -3,6 +3,7 @@
import cn.hutool.core.date.DateTime;
import com.yssh.entity.*;
import com.yssh.service.CommonService;
import com.yssh.service.SocketService;
import com.yssh.service.VocValsService;
import com.yssh.utils.DateUtils;
import com.yssh.utils.StringUtils;
@@ -46,6 +47,9 @@
    @Resource
    private VocValsService vocValsService;
    @Resource
    private SocketService socketService;
    private final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
@@ -214,4 +218,19 @@
        return Result.OK(suYuanService.selectFastById(id, date));
    }
    @ApiOperation(value = "发送消息")
    @ApiOperationSupport(order = 19)
    @GetMapping("/sendMsg")
    public Result sendMsg(@RequestParam(value = "msg") String msg) {
        if (StringUtils.isEmpty(msg)) return Result.error("消息不能为空");
        try {
            String rs = socketService.sendMsg(msg);
            return Result.OK(rs);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
}
src/main/java/com/yssh/service/SocketService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,60 @@
package com.yssh.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * Socket服务类
 * @author WWW
 * @date 2023-11-06
 */
@Service
public class SocketService {
    /**
     * Socket的IP
     */
    @Value("${socket.ip}")
    private String ip;
    /**
     * Socket的端口
     */
    @Value("${socket.port}")
    private int port;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * å‘送消息
     */
    public String sendMsg(String msg) throws Exception {
        try {
            // åˆ›å»ºSocket对象,指定服务端的IP地址和端口号
            Socket socket = new Socket(ip, port);
            // èŽ·å–è¾“å…¥æµå’Œè¾“å‡ºæµ è¾“入流和输出流是通过socket对象来进行数据传输的。
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            // å°†ç”¨æˆ·è¾“入的信息发送给服务端
            out.println(msg);
            // æŽ¥æ”¶æœåŠ¡ç«¯çš„å“åº”å¹¶æ‰“å°
            String rs = in.readLine();
            socket.close();
            return rs;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw ex;
        }
    }
}
src/main/resources/application.yml
@@ -58,3 +58,7 @@
  pathMapping:
  # æ˜¯å¦å¼€å¯Knife4j增强模式,默认是 false
  enable: true
socket:
  ip: 127.0.0.1
  port: 6666