I'm a little bit confused about a strange thing. I hope i get some explanation about this behaviour.
Here is my little learning project. A simple tcp client-server chat program. The client is written electron but its not working yet.
My chat module is:
'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;
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;
});
});
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?
Pls help me out :)