gpt4 book ai didi

javascript - ipcMain 事件处理程序中的 Electron TypeError

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:02 24 4
gpt4 key购买 nike

我对一件奇怪的事情感到有点困惑。我希望我能得到一些关于这种行为的解释。

这是我的小学习项目。一个简单的tcp客户端-服务器聊天程序。客户端是用Electron编写的,但还没有运行。

我的聊天模块是:

'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;

在 Electron main.js 中我尝试像这样使用:

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发送start-connect方法后, Electron 抛出错误:chat.join不是一个函数,我不知道为什么,因为事件处理程序是一个闭包,对吧?

请帮帮我:)

最佳答案

在包含您所包含的 ipcMain 时,我遇到了一个特殊问题

const ipcMain  = require('electron').ipcMain;

在上述步骤之后,ipcMain 未定义,因此您将在未定义时收到 typeError 。更改库以包含 ipc-main

const ipcMain = require('ipc-main');

关于javascript - ipcMain 事件处理程序中的 Electron TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33964864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com