您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

2021-05-31

package com.saruan.xinfang.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.saruan.xinfang.mapper.LcdWindowMapper;
import com.saruan.xinfang.model.BipTWindow;
import com.saruan.xinfang.model.LcdWindow;
import com.saruan.xinfang.model.SysUser;
import com.saruan.xinfang.vo.SocketVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Component
public class SocketService {
    @Autowired
    public SysUserService userService;
    @Autowired
    public LcdWindowService lcdWindowService;
    @Autowired
    public WindowService windowService;
    @Autowired
    public LcdWindowMapper lcdWindowMapper;
    @Value("${socket.port}")
    Integer PORT;
    @Value("${socket.ip}")
    String ip ;
    private static PrintWriter out;
    @Value("${xinfang.picture}")
    private String pictureUrl;
    private static Map<Long, Socket> socketMap = new ConcurrentHashMap<Long, Socket>();
    public String ckId;  //lcd唯一码
    private static BufferedReader bufferedReader;
    @PostConstruct
    public void onOpen() throws Exception {
        //InetAddress ia = InetAddress.getByName(ip);
        ServerSocket server = new ServerSocket(PORT);
        System.out.println("socket success!");
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Socket clientSocket = server.accept();
                        System.out.println("新的socket连接进入:"+clientSocket);
                        //bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
                        InputStream inputStream = clientSocket.getInputStream();
                        byte[] bytes = new byte[1024];
                        int len;
                        StringBuilder sb = new StringBuilder();
                        while ((len = inputStream.read(bytes)) != -1) {
                            //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8
                            String s = new String(bytes, 0, len,"UTF-8");
                            sb.append(s);
                            if (s.endsWith("#")){
                                break;
                            }
                        }
                        ckId = sb.toString();
                        System.out.println("唯一码:"+ckId);
                        LcdWindow lcdWindow1 = new LcdWindow();
                        lcdWindow1.setLcdid(ckId);
                        List<LcdWindow> lcdWindows = lcdWindowService.selectAll(lcdWindow1);
                        for (LcdWindow lcdWindow : lcdWindows) {
                            socketMap.put(lcdWindow.getWindowid(),clientSocket);//保存窗口ID和socket
                        }
                        System.out.println("socketMap"+socketMap);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("socket连接失败!");
                    }
                }
            }
        });
        t.start();
        Thread.sleep(1000);
    }

    public void SendMessage(SocketVo sv) throws IOException {
        Socket socket = socketMap.get(sv.getId());
        if (socket != null) {
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            out.println(JSON.toJSONString(sv));
            out.flush();
        }

    }

    public void SendPetitionPeople(SocketVo sv) throws IOException {
        //获取系统路径
        String url = System.getProperty("catalina.home");
        Socket socket = socketMap.get(sv.getId());
        if (socket != null) {
            //先存
            LcdWindow lcdWindow = new LcdWindow();
            lcdWindow.setWindowid(sv.getId());
            lcdWindow.setUserid(sv.getUserId());
            lcdWindowMapper.updateByWindowId(lcdWindow);
            //在通过窗口id查屏幕唯一码
            String lcdId = lcdWindowMapper.selectLcdIdByWindowId(sv.getId());
            System.out.println("lcdId"+lcdId);
            if (lcdId.equals(ckId)) {
                LcdWindow lcdWindow1 = new LcdWindow();
                lcdWindow1.setLcdid(lcdId);
                List<LcdWindow> lcdWindows = lcdWindowService.selectAll(lcdWindow1);
                System.out.println("lcdWindows"+lcdWindows);
                if (lcdWindows.size() == 1) {
                    for (LcdWindow window : lcdWindows) {
                        if (window.getUserid() != 0) {
                            SysUser user = userService.select(sv.getUserId());
                            //图片路径
                            window.setPicture(pictureUrl+user.getAvatar());//登记人员图片
                            window.setUsername(user.getUserName());//登记人员姓名
                            window.setZw("普通职员");//职位
                            window.setState("正在办理");//状态
                            BipTWindow select = windowService.select(window.getWindowid());
                            window.setPoweraffairsName(select.getFname());//责任单位
                        }
                    }
                }
                if (lcdWindows.size() == 2) {
                    for (LcdWindow window : lcdWindows) {
                        if (window.getUserid() != 0) {
                            SysUser user = userService.select(sv.getUserId());
                            //图片路径
                            window.setPicture(pictureUrl+user.getAvatar());//登记人员图片
                            window.setUsername(user.getUserName());//登记人员姓名
                            window.setZw("普通职员");//职位
                            window.setState("正在办理");//状态
                            BipTWindow select = windowService.select(window.getWindowid());
                            window.setPoweraffairsName(select.getFname());//责任单位
                        } else {
                            SysUser user = userService.select(sv.getUserId());
                            window.setPicture("");//登记人员图片
                            window.setUsername("");//登记人员姓名
                            window.setZw("普通职员");//职位
                            window.setState("暂停办理");//状态
                            System.out.println("window.getWindowid()"+window.getWindowid());
                            BipTWindow select = windowService.select(window.getWindowid());
                            window.setPoweraffairsName(select.getFname());//责任单位
                        }
                    }
                }
                JSONObject jo = new JSONObject();
                jo.put("type","login");
                jo.put("content",lcdWindows);
                out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                System.out.println(JSONArray.toJSONString(lcdWindows));
                out.println(jo.toString());
                out.flush();
            }else {
                out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                out.println("未找到对应的lcd屏幕");
                out.flush();
            }
        }else {
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            out.println("未创建socket连接");
            out.flush();
        }
    }
    public boolean Close(String id) throws IOException {
        boolean flag = false;
        Socket socket = socketMap.get(Long.valueOf(id));
        if (!socket.isClosed()) {
            socket.close();
            socketMap.remove(Long.valueOf(id));
            flag = true;
            System.out.println("窗口 - " + id + " 正常关闭");
            System.out.println("当前socket列表:" + socketMap.keySet());
        }
        return flag;
    }
}

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进