Electron 不会生成 flask-socketio 服务器:ERR_CONNECTION_REFUSED

Electron won't spawn flask-socketio server: ERR_CONNECTION_REFUSED

我大致按照this and this将烧瓶服务器嵌入电子项目。如果我flask run服务器然后electron .,连接就建立了。但是,如果我尝试在电子启动期间初始化服务器,服务器永远不会启动(或者我不太了解的东西)并且我继续得到 ERR_CONNECTION_REFUSED。我也尝试清除缓存和 DNS 刷新(认为问题可能出在本地)。关于我在这里可以做什么的任何方向?

P.S。这是我第一次真正接触 JS,我主要是一个 Python 的人。抱歉语法错误。

更新:没有代表回复@Miguel,我使用 conda env 是的,它在启动前被激活。此外,设置环境变量后,我已经使用 python -m flask runflask runpython .\scripts\Backend\testFlask.py 成功启动了服务器。在 Electron 中生成这些方法中的任何一个都会导致相同的连接错误。

main.js

const electron = require('electron')
const {app, BrowserWindow, Menu, dialog} = require('electron')
const fs = require('fs')
const path = require('path')
const url = require('url')

let win
let server;
process.env.FLASK_APP = '.\scripts\Backend\testFlask.py';
process.env.FLASK_DEBUG = 1;

module.exports = {
    openFile: function () {
        var files = dialog.showOpenDialog({
            properties: ['openFile']
        });

        if (!files) { return; }

        var file = files[0];
        console.log(typeof file)
        var content = fs.readFileSync(file).toString();

        win.webContents.send('fileOpener', file, content);
        console.log(content);
    }
};

function createWindow(){
    const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize
    win = new BrowserWindow({
        width: width * (2/3),
        height: height * (9/10),
        show: false
        })

    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file',
        slashes: true
    }))

    win.once('ready-to-show', ()=> {
        win.show();
    })

    win.webContents.openDevTools()

    win.on('closed', () => {
        win = null;
    })
}

app.on('ready', function(){
    var script = path.join(__dirname, 'scripts', 'Backend', 'testFlask.py')
    server = require('child_process').spawn('python.exe', [script])
    server.stdout.on('data', (data) => {
        console.log(data.toString())
    })

    createWindow()
    const applicationMenu = require('./menu');
    Menu.setApplicationMenu(applicationMenu);
    })

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (win == null) {
        createWindow();
        const applicationMenu = require('./menu');
        Menu.setApplicationMenu(applicationMenu);
    }
})

testFlask.py

from flask import Flask, request
from flask_socketio import SocketIO, send, emit

app = Flask(__name__)
socketio = SocketIO(app, async_mode='eventlet'test)

@app.route('/')
def welcome():
    print('Welcome')

@socketio.on('message')
def data_received(message):
    print(message)
    send(message)

@socketio.on('connect')
def connection():
    print('Echelon connection established')

if __name__ == '__main__':
    print("testing")
    socketio.run(app, port='8080', debug=True)

socketAPI.js

$(document).ready(function(){
    var socket = io.connect('http://localhost:8080/');
    socket.on('connect', function() {
        console.log(socket.connected) //make sure the connection is established
    });

    socket.on('message', function(message){
        console.log(message + ' received');
    });

    $('form#searchBarForm').submit(function(event) {
        console.log(event.target["0"].value + ' sent') //for debugging
        socket.send(event.target["0"].value);
        return false;
    });
})

我的电脑硬重启似乎解决了这个问题。缓存和 DNS 之外的东西导致了问题。周末回来后,建立了稳定的连接。谢谢@Miguel