我有一个5角应用程序,可以使用websockets来不断地从服务器接收数据。 websocket由Socket.IO在名为Socketio.service
的角度服务下处理。现在,我正试图把它变成一个ElectronJS桌面应用程序。
我的问题是:当我使用ng serve
运行角度应用程序时,一切正常,但当我将其作为ElectronJS应用程序运行时,我收到错误:
file/socket.io/?EIO=3&transport=polling&t=M4aGo_y Failed to load resource: net::ERR_NAME_NOT_RESOLVED
polyfills.29c692a….bundle.js:1 GET http://file/socket.io/?EIO=3&transport=polling&t=M4aGqYZ net::ERR_NAME_NOT_RESOLVED
我已仔细检查了我的代码,并且我没有看到任何奇怪的内容,并且通过Google搜索,我无法找到有类似问题的人。
我的一些现有代码,用于上下文:
Socketio.service
:
// There are more Subjects, and more events to which the service listens,
// but they are all pretty much the same.
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import * as io from 'socket.io-client';
@Injectable()
export class SocketioService {
namespace = '/test';
socket: any;
positionUpdate: Subject<object> = new Subject<object>();
constructor() {
this.socket = io(location.protocol + '//' + 'localhost' + ':5000' +
this.namespace, {
'reconnection': true,
'reconnectionDelay': 500,
'reconnectionAttempts': 10
});
const self = this;
this.socket.on('advisor_position', function(msg) {
self.positionUpdate.next(msg);
});
}
}
main.js
我正在使用ElectronJS:
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
而且,我的packages.json中引用了socket.io-client
:
{
...
"dependencies": {
...
"socket.io-client": "^2.0.4",
...
},
...
}
答案 0 :(得分:0)
好吧,问题似乎来自type Test<T> = (arg?: T) => void;
class Wrapper<T, F extends Test<T>> {
public execute: F;
public static create<T1, F1 extends Test<T1>>(method: F1): Wrapper<T1, F1> {
return new Wrapper(method);
}
constructor(method: F) {
this.execute = method;
}
}
const func1 = Wrapper.create((a: string) => console.log(a));
const func2 = Wrapper.create(() => { });
func1.execute('3');
func2.execute();
func1.execute(5); // 5 is not assignable to type "string"
func1.execute(); // expected 1 argument
func2.execute(5); // expected 0 arguments
。显然,电子的默认协议不是http(没有尝试使用console.log来查看什么是电子协议)。修复方法是:location.protocol
更改location.protocol
(使用http:
也可以。)