ipcMain事件处理程序中的Electron TypeError [英] Electron TypeError in ipcMain event handler
问题描述
我对一件奇怪的事情有些困惑。我希望我能对此行为有所解释。
I'm a little bit confused about a strange thing. I hope i get some explanation about this behaviour.
这是我的小学习项目。一个简单的tcp客户端-服务器聊天程序。客户端已被写入电子文件,但尚无法正常工作。
Here is my little learning project. A simple tcp client-server chat program. The client is written electron but its not working yet.
我的聊天模块是:
'use strict';
let Socket = require('net').Socket;
let EventEmitter = require('events');
let util = require('util');
function Chat() {
EventEmitter.call(this);
this.socket = new Socket({
allowHalfOpen:true
});
this.socket.on('data', (data) => {
let message = JSON.parse(data);
if (message.isServer) {
this.emit('server', message);
} else {
this.emit('message', message);
}
});
this.socket.on('error', (err) => {
this.socket.destroy();
this.emit('error', err);
});
};
Chat.prototype.join = function(port, host, user) {
this.user = user;
this.socket.connect(port, host, () => {
this.emit('connected');
});
}
Chat.prototype.send = function(msg, cb) {
let message = JSON.stringify({
usr: this.user,
msg: msg
});
this.socket.write(message, cb || null);
};
Chat.prototype.leave = function(cb) {
this.socket.destroy();
if (typeof cb === 'function') {
cb();
}
};
util.inherits(Chat, EventEmitter);
module.exports = Chat;
在电子main.js中,我尝试这样使用:
In the electron main.js i tried use like this:
const electron = require('electron');
const ipcMain = require('electron').ipcMain;
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const Chat = require('./lib/chat');
const DEFAULT_PORT = **;
const DEFAULT_HOST = '**';
// Report crashes to our server.
electron.crashReporter.start();
// 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.
var mainWindow = null;
var chat = new Chat();
// 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();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.setMenu(null);
mainWindow.loadURL('file://' + __dirname + '/index.html');
ipcMain.on('start-connect', function(event, uname) {
chat.join(**, '*****', uname);
chat.on('connected', () => {
event.sender.send('connected');
});
chat.on('server', message => {
event.sender.send('server', message);
});
chat.on('message', (message) => {
if (message.user === chat.user) {
message.user = 'You';
}
event.sender.send('message', message);
});
ipcMain.on('submit', (event, msg) => {
chat.send(msg);
});
});
// Emitted when the window is closed.
mainWindow.on('closed', function() {
if (chat !== null) {
chat = null;
}
mainWindow = null;
});
});
从index.html发送完启动连接方法后,电子抛出错误:聊天。加入不是一个函数,我不知道为什么,因为事件处理程序是一个关闭权?
After from the index.html i send the start-connect method, the electron throw error: chat.join is not a function and i don't know why because the event handler is a closure right?
请帮助我:)
推荐答案
我有一张脸特殊的问题,当包含ipcMain时已包含
I have face one particular issue, when including the ipcMain as you have included
const ipcMain = require('electron').ipcMain;
ipcMain在上述步骤后未定义,因此在未定义时会出现typeError。
更改库以包括ipc-main代替
const ipcMain = require('ipc-main');
这篇关于ipcMain事件处理程序中的Electron TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!