nodejs calls exe execution file to connect websocket

Recently, there is a demand that the front end needs to call a websocket service packaged into an exe file.

Obviously, under the framework of the browser, it is impossible to accomplish this. Because of the browser's sandbox mechanism and security policy, it is not allowed to read local files.

So, we need the help of nodejs.

So, to integrate the nodejs environment without deviating from the scope of front-end development, I had to decide to use eletron to package my browser application.

If you don’t know how to use electron to package front-end applications, please refer to my other articles or search on Baidu bing:

Problems encountered when vuecli4 electron13.3.0 creates client applications and installs and packages_win.loadurl('app://./index.html')_屋阿综's Blog-CSDN Blog

Vue electron node koa realizes desktop application

Electron integrates browser and node environment inside, which allows us to develop front-end interface interactive js) and nodejs (server) server functions at the same time under one framework.

Now the functionality puzzle of reading local files has been solved.

We now need a websocket service file packaged as exe to test it.

Since I am not familiar with other languages, I use python to write websocket services.

start

Note: You can use any backend language you are familiar with to write websocket services. I use python, based on the following two points:

1. I am relatively familiar with python, and it is extremely convenient to use;

2. Use python to directly package the service into an exe file, which is very convenient!

If you also want to use python like me, please install and configure the python environment on Baidu first. Because this is a prerequisite .

1. Install the corresponding modules first.

# python 安装websockets 模块
# 打开cmd

pip install websockets


# 然后确认有没有安装成功 

pip list 

# 可以查看当前安装的模块

2. Write socket service code

Write a python file - websocket.py, the code is as follows:

# websocket.py

import asyncio
import websockets


async def echo(websocket, path):
    # fetch msg
    async for message in websocket:
        print("got a message:{}".format(message))
        await websocket.send(message)


async def main():
    # start a websocket server
    # 开启socket服务 地址为 ws://localhost:8765
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever

asyncio.run(main())

3. Generate exe file

Because our ultimate goal is to execute an exe file through node, and then connect to the webocket service.

To use python to package py files, you first need to install the three-party dependency pyinstaller.

First install globally:

# 打开cmd

pip install pyinstaller

# 查看是否安装成功

pip list 

# 可以看到安装的所有依赖

Then enter the folder where the py file is located, enter cmd, and execute the following command:

# 进入写有websocket.py的文件夹内 执行一下命令

pyinstaller --onefile websocket.py

After successful execution, the word successful will finally appear.

Then a dist file will be automatically generated in this folder, and there will be a packaged exe file in the dist:

 As shown above, our exe file has been generated, python is really easy to use, right?

4. Use nodejs to execute this exe file and create a websocket service

No more nonsense, create a new server.js, the code is as follows:

// server.js

// 注意 一下代码是nodejs代码

// node开启子进程
const cp = require('child_process');

// 然后执行我们刚生成的exe文件 看看能不能起来websocket服务  
// 注意地址要正确

let child = cp.spawn('D://Local_file/py/excersise/dist/websocket.exe')

Open cmd and execute node server.js.

5. Test whether the connection can be successful

Open the websocket test tool (downloadable online, small and convenient), enter the connection address, and start the test;

 

As above, the test connection is successful. 

6. How to obtain the installation address of the electron client

In background.js (electron configuration js file), you can write the following code:

// 以下代码是利用electron获取electron安装的位置

const { app } = electron;
let path = require('path');
let exePath = path.dirname(app.getPath('exe'));\

// exePath 就是 安装地址

 

Why get the installation address?

As mentioned earlier, my requirement is to read and execute a file named websocket.exe in the installation directory.

After getting the address, I can execute the exe file through the above nodejs method.

Summary: The front end hardly touches exe execution. So when I first had this need, I was at a loss. My example mainly uses python and nodejs to help verify and solve the actual demand problem.

end~

Guess you like

Origin blog.csdn.net/jmszl1991/article/details/131771447