Java web项目中使用C#配合开发,实现双屏显示功能

一、前言

最近系统开发中,新增了一个需求,要求客户登记以后,需要有一个对外的屏幕显示客户登记信息,方便客户确认个人信息及相关信息是否有误。在了解相关需求以后,我打算使用C#来配合实现双屏显示功能。作为一名java程序员,在使用C#开发过程中,感觉也挺有意思的,特此,简单记录一下实现思路。

二、代码实现

java服务端

  1. 创建一个spring boot项目,项目结构如下:
    在这里插入图片描述
  2. 导入maven依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  1. 编写application.yml配置文件:
server:
  port: 8080
  servlet:
    context-path: /dsserver
  1. 编写index.html页面:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
<div>
    <button onclick="openDSForm()">打开双屏显示</button><br/>
    <input id="content" type="text"/>
    <button onclick="sendMessage()">发送消息</button>
</div>
</body>
<script>
    var spWebsocket;
    function initSpWebsocket() {
        if ('WebSocket' in window) {
            spWebsocket = (spWebsocket && spWebsocket.readyState == spWebsocket.OPEN) ? spWebsocket : new WebSocket("ws://127.0.0.1:59999");
            // 连接发生错误的回调方法
            spWebsocket.onerror = function (err) {
                console.log(err);
            };
            // 连接成功建立的回调方法
            spWebsocket.onopen = function () {

                console.log("spWebsocket打开关闭!");
            };
            // 接收到消息的回调方法
            spWebsocket.onmessage = function (event) {
                console.log(event.data);
            }
            // 连接关闭的回调方法
            spWebsocket.onclose = function () {

                console.log("spWebsocket连接关闭!");
            }
        } else {
            alert("当前浏览器不支持websocket!");
        }
    }

    function openDSForm() {
        if(!(spWebsocket && spWebsocket.readyState == spWebsocket.OPEN)) {
            initSpWebsocket();
        }else {
            spWebsocket.send("openDS");
        }
    }

    function sendMessage() {
        if(!(spWebsocket && spWebsocket.readyState == spWebsocket.OPEN)) {
            initSpWebsocket();
        }else {
            var input =  document.getElementById("content");
            var functionName = "acceptMessage"; // 需要调用双屏显示页面的js中的方法名称
            spWebsocket.send(functionName + "^" + input.value);
        }
    }
</script>
</html>
  1. 编写用于双屏显示的页面doubleScreenShow.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>双屏显示</title>
</head>
<body>
    <div>
        <h1 style="color: red">双屏显示内容:<label id="content" ></label></h1>
    </div>
</body>
    <script>
        function acceptMessage(msg) {
            var input =  document.getElementById("content");
            input.innerText = msg;
        }
    </script>
</html>

开发中,我为了方便,并没有完全使用C#的窗口控件来做双屏显示客户端,而是只使用了C#窗口控件嵌套浏览器页面的做法,毕竟我对C#了解程度有限,开发时间也不充裕。

  1. 编写TestController,用于转发双屏显示页面:
@Controller
public class TestController {

    @GetMapping("/dsShow")
    public String dsShow() {
        return "doubleScreenShow.html";
    }
}

以上,整个就是服务端的代码,只是用于演示,整个代码结构还是很简单的。

C#双屏代码

  1. 创建一个C#的winform窗口程序:
    在这里插入图片描述
    在这里插入图片描述
  2. 管理需要依赖的程序包:
    在这里插入图片描述
  • Fleck:创建websocket服务器需要的引用依赖:
    在这里插入图片描述
  • CefSharp:winform窗口浏览器插件内核默认是IE,使用CefSharp相关引用,可以使用谷歌内核的浏览器控件:
    在这里插入图片描述
  1. 主窗口代码:
using CefSharp.WinForms;
using System.Windows.Forms;

namespace doublescreen_client
{
    public partial class Form1 : Form
    {
        private ChromiumWebBrowser chrome = null;
        public Form1(string url)
        {
            InitializeComponent();
            // 初始化一个浏览器
            if (url != null && !"".Equals(url))
            {
                chrome = new ChromiumWebBrowser(url)
                {
                    Dock = DockStyle.Fill
                };
                this.Controls.Add(chrome);
            }
            this.Text = "双屏显示客户端";
            this.ShowInTaskbar = false;// 不在工具栏显示图片
            this.Hide(); // 隐藏窗口
        }

        private delegate void DeletegateShowDSForm();
        // 显示窗口
        public void DoShow()
        {
            if (this.InvokeRequired)
            {
                DeletegateShowDSForm dsf = new DeletegateShowDSForm(ShowDSFrom);
                Invoke(dsf);
            }
            else
            {
                ShowDSFrom();
            }
        }

        public void ShowDSFrom()
        {
            this.TopMost = true;// 窗口置顶
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Maximized;
            this.Visible = true;
            this.Activate();
            this.Show();
        }

        public ChromiumWebBrowser GetBrowser()
        {
            return this.chrome;
        }
    }
}
  1. Program.cs中的执行逻辑代码:
using CefSharp.WinForms;
using Fleck;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace doublescreen_client
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool openFlag = CheckSoftWareOpenMore("doublescreen-client"); // 判断当前软件是否已经打开了

            if (openFlag) // 如果已经打开了
            {
                DialogResult result = MessageBox.Show("你已经打开一个照相软件,请勿重复打开!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (result == DialogResult.OK)
                {
                    System.Environment.Exit(0);
                }
            }
            string serverUrl = "http://localhost:8080/dsserver/dsShow";

            Form1 form =  new Form1(serverUrl);
            ChromiumWebBrowser chrome = form.GetBrowser();
            // 创建一个websocket服务器,进行两个屏幕的通信
            WebSocketServer server  = new WebSocketServer("ws://127.0.0.1:59999" );

            //出错后进行重启
            server.RestartAfterListenError = true;

            server.Start(socket =>
            {
                socket.OnOpen = () =>
                {

                };
                socket.OnClose = () =>
                {

                };
                socket.OnMessage = message =>
                {
                    if (message == "ping...")
                    {
                        return;
                    }
                    string[] myParams = message.Split('^'); //获取参数值
                    if ("openDS".Equals(myParams[0]))
                    {

                        if (!form.Visible || FormWindowState.Minimized.Equals(form.WindowState))
                        {
                            form.DoShow();
                        }
                        return;
                    }
                    if (form.Visible)
                    {
                        string JSFunction = myParams[0] + "('" + myParams[1] + "')"; //拼接js方法

                        chrome.GetBrowser().MainFrame.EvaluateScriptAsync(JSFunction); // 调用网页中的js方法
                    }


                };
            });

            Screen[] sc = Screen.AllScreens; // 获取所有屏幕

            int showOnMonitor = 1; // 默认在附屏幕上显示

            if (sc.Length == 1)
            {
                showOnMonitor = 0; // 如果只有一个屏幕就在主屏幕显示
            }

            form.StartPosition = FormStartPosition.Manual; // 设置手动指定窗口位置
            form.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top); // 将窗口设置到另一个屏幕上
            form.WindowState = FormWindowState.Minimized; // 默认开始最小化窗口
            Application.Run(form);
        }
        // 判断程序是否已经打开
        static bool CheckSoftWareOpenMore(string softwareName)
        {
            Process[] processes = Process.GetProcessesByName(softwareName);
            if (processes.Length > 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

三、测试

  1. 启动服务端,访问:http://localhost:8080/dsserver/index.html
    在这里插入图片描述

  2. 由于使用了CefSharp,需要配置visual studio 的debug方式,配置管理器,选择运行平台:
    在这里插入图片描述
    在这里插入图片描述

  3. 启动C#双屏显示软件,由于最开始隐藏了窗口,所以没有弹窗,可以通过以下标志判断是否运行:
    在这里插入图片描述

  4. 点击网页的【打开双屏显示】按钮:
    在这里插入图片描述
    由于websocket连接需要时间,可能需要多点击两次,然后,就可以发现在另外一个屏幕上打开了一个窗口了:
    在这里插入图片描述

  5. 在主屏幕的浏览器上发生消息,进行测试:
    在这里插入图片描述
    双屏页面:
    在这里插入图片描述
    可以看到双屏显示的页面成功显示消息了。

四、最后:

这只是提供了一种实现双屏显示的思路,java部分的代码很简单,由于我对C#也不是很了解,代码风格应该也不好,大家只做参考,具体实现我就不做过多阐述,免得误人子弟,大家有兴趣看看注释。

实际业务肯定比这个复杂,原理大概就是这样,可以通过这个实现原理,把对人员的增删改查,和一些绑定信息的修改都实时的在对外的屏幕上向用户显示。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,你可以通过使用`javax.servlet.ServletContext`接口来实现类似于C#`System.Web.HttpContext.Current.Request.MapPath()`的功能。下面是一个示例代码: ```java import javax.servlet.ServletContext; public class MapPathExample { public static String mapPath(String path) { ServletContext context = ServletContextProvider.getServletContext(); // 通过Provider获取ServletContext对象 String realPath = context.getRealPath(path); // 获取真实路径 return realPath; } } class ServletContextProvider { private static ServletContext servletContext; public static void setServletContext(ServletContext context) { servletContext = context; } public static ServletContext getServletContext() { return servletContext; } } ``` 在你的Java Web应用程序的`web.xml`文件,你需要添加一个`ServletContextListener`来设置`ServletContext`对象。下面是一个示例`web.xml`文件的配置: ```xml <web-app> <!-- 其他配置 --> <listener> <listener-class>com.example.ServletContextListenerImpl</listener-class> </listener> </web-app> ``` 然后,你需要实现`ServletContextListener`接口,在`contextInitialized`方法设置`ServletContext`对象。下面是一个示例`ServletContextListener`的实现: ```java import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ServletContextListenerImpl implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { ServletContext context = servletContextEvent.getServletContext(); ServletContextProvider.setServletContext(context); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { // 需要实现该方法,可以留空 } } ``` 现在,你可以在你的Java代码使用`MapPathExample.mapPath(path)`方法来获取路径的真实物理路径,就像在C#使用`System.Web.HttpContext.Current.Request.MapPath()`一样。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值