Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从渲染进程调用的工作进程的postmessage将转到何处?_Javascript_Html_Electron_Web Worker_Postmessage - Fatal编程技术网

Javascript 从渲染进程调用的工作进程的postmessage将转到何处?

Javascript 从渲染进程调用的工作进程的postmessage将转到何处?,javascript,html,electron,web-worker,postmessage,Javascript,Html,Electron,Web Worker,Postmessage,我使用ElectronJS制作一个应用程序,从图像生成STL文件,并使用OpenCV访问图像每个像素的值。辅助进程用于写入STL文件。我遇到的复杂情况是,我没有在创建工作进程的文件(向工作进程发送消息的同一位置)或主进程中接收消息 以下是工作进程的创建过程: let worker = new Worker('../workers/convertor.js') function convertFile() { // Not converting if there is no file

我使用ElectronJS制作一个应用程序,从图像生成STL文件,并使用OpenCV访问图像每个像素的值。辅助进程用于写入STL文件。我遇到的复杂情况是,我没有在创建工作进程的文件(向工作进程发送消息的同一位置)或主进程中接收消息

以下是工作进程的创建过程:

let worker = new Worker('../workers/convertor.js')
 function convertFile() {
    // Not converting if there is no file selected
    if(filePath != null) {
        // The mat for image analysis
        let src = cv.imread('img-for-analysis', cv.IMREAD_GRAYSCALE)
        // Variable to hold the pixel values of the image
        let pixelValues = [...Array(src.rows)].map(e => Array(src.cols).fill(null))
        //Storing the values of the pixels in the array
        for(let i = 0; i < src.rows; i++) {
            for (let j = 0; j < src.cols; j++) {
                pixelValues[i][j] = src.ucharAt(i, j * src.channels())
            }
        }
        worker.postMessage([filePath, fileName, pixelValues])
    }
}
这是工人的代码

onmessage = function (e) {
   //Creating the write stream
   let stream = fs.createWriteStream(e.data[1].split('.')[0] + '.stl', {flags: 'w'})
   this.self.postMessage([50])

   convert(stream, e.data[2])
   self.close()
}
这就是我向员工发送信息的方式:

let worker = new Worker('../workers/convertor.js')
 function convertFile() {
    // Not converting if there is no file selected
    if(filePath != null) {
        // The mat for image analysis
        let src = cv.imread('img-for-analysis', cv.IMREAD_GRAYSCALE)
        // Variable to hold the pixel values of the image
        let pixelValues = [...Array(src.rows)].map(e => Array(src.cols).fill(null))
        //Storing the values of the pixels in the array
        for(let i = 0; i < src.rows; i++) {
            for (let j = 0; j < src.cols; j++) {
                pixelValues[i][j] = src.ucharAt(i, j * src.channels())
            }
        }
        worker.postMessage([filePath, fileName, pixelValues])
    }
}

代码的所有其他部分都可以正常工作,但我在控制台中没有收到任何响应。

我找到了答案。调用this.self.postmessage()就是向工作者发布消息。我只需要从worker调用postmessage()。在我想要接收消息的文件中,我将执行以下操作:

worker.onmessage = function (e) {
    // Code
}