ipcMain事件处理程序中的Electron TypeError

时间:2015-11-27 21:14:43

标签: javascript node.js electron

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

这是我的小学习项目。一个简单的tcp客户端 - 服务器聊天程序。客户端是电子编写的,但它还没有工作。

我的聊天模块是:

'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中,我试过这样使用:

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发送启动连接方法之后,电子投掷错误:chat.join不是函数,我不知道为什么因为事件处理程序是一个闭包权?

请帮助我:))

1 个答案:

答案 0 :(得分:0)

当包含ipcMain时,我面临一个特定的问题

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

在上面的步骤之后,ipcMain未定义,因此你将在undefined上获得typeError。 将库更改为包含ipc-main

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