在电子桌面应用程序中运行时,套接字服务器连接错误

时间:2019-12-03 01:10:20

标签: node.js socket.io electron

这是在我使用电子构建器编译并构建了电子应用之后。我可以在终端上使用electron .来完美地运行此应用程序,它将在渲染器进程中同时运行已编译的vue应用程序和使用shelljs执行的socketServer。

当我编译该应用程序并尝试从桌面运行它时,出现了套接字连接错误。

我的客户端:

import io from "socket.io-client";
const socket = io("http://localhost:1447");

我的服务器端:

const app = require("express")();
const http = require("http").Server(app);
var io = require("socket.io")(http);
const PORT = 1447;

...lots of code

http.listen(PORT, function() {
  console.log(`successfully listening on ${PORT}!`);
});

我的当选/main.js:

app.on('ready', function () {
  // Initialize the window to our specified dimensions
  win = new BrowserWindow({
    width: 1000, 
    height: 600,
    // icon: path.join(__dirname, './icon/icon.icns'), 
    webPreferences: {
      webSecurity: false, //probably unecessary
      contextIsolation: false,
      preload: path.resolve(__dirname, "./preload.js")
      //preload contains the scripts that call the socketServer
    },
  });

win.loadURL(`file://${__dirname}/dist/index.html`);

我的理论是套接字连接被拒绝,因为我正在使用http://localhost:1447进行连接,但是看到这实际上是在桌面上提供文件,不是正确的端点。我之前也进行了测试,并尽我所能,即使在桌面模式下,socketServer可执行文件也可以在应用启动时正确运行,因此,我相信这是问题所在。

任何帮助将不胜感激

其他参考:Connecting Webpack and Socket Server to electron preload scripts

0 个答案:

没有答案