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

时间:2017-10-27 21:21:22

标签: python-3.x flask socket.io electron flask-socketio

我大致跟随thisthis将烧瓶服务器嵌入到电子项目中。如果我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;
    });
})

1 个答案:

答案 0 :(得分:0)

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