本文由蘑菇街前端技术团队分享,原题“Electron 从零到一”,即时通讯网有修订和改动。
cover-opti.png (15.03 KB, 下载次数: 908)
下载附件 保存到相册
2 年前 上传
1-.png (50.78 KB, 下载次数: 951)
{ "targets": [{ "conditions": [ ["OS=='mac'", { "libraries": [ "<(module_root_dir)/lib/mac/security.a" ] }], ["OS=='win'", { "libraries": [ "<(module_root_dir)/lib/win/security.lib" ] }], ... ] ... }]
if (process.platform === 'win32') { addon = require('../lib/security_win.node'); } else { addon = require('../lib/security_mac.node'); }
npm i protobuff -S
pbjs -t json-module --sparse --force-long -w commonjs -o src/im/data/pbJson.js proto/*.proto
var Long = require("long"); $protobuf.util.Long = Long; $protobuf.configure(); $protobuf.util.LongBits.prototype.toLong = function toLong (unsigned) { return new $protobuf.util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned)).toString(); };
import PbJson from './path/to/src/im/data/pbJson.js'; // 封装数据 let encodedMsg = PbJson.lookupType('pb-api').ctor.encode(data).finish(); // 解封数据 let decodedMsg = PbJson.lookupType('pb-api').ctor.decode(buff);
2.png (40.63 KB, 下载次数: 947)
import { EventEmitter } from 'events'; const webSocketConfig = 'wss://xxxx'; class SocketServer extends EventEmitter { connect () { if(this.socket){ this.removeEvent(this.socket); this.socket.close(); } this.socket = new WebSocket(webSocketConfig); this.bindEvents(this.socket); return this; } close () {} async getSocket () { } bindEvents() {} removeEvent() {} onMessage (e) { // 消息解包 let decodedMSg = 'xxx; this.emit(decodedMSg); } async send(sendData) { const socket = await this.getSocket() socket.send(sendData); } ... }
class PocketManager extends EventEmitter { encode (id, type, version, api, payload) { let headerBuffer = Buffer.alloc(8); let payloadBuffer = Buffer.alloc(0); let offset = 0; let keyLength = Buffer.from(id).length; headerBuffer.writeUInt16BE(keyLength, offset); offset += 2; headerBuffer.write(id, offset, offset + keyLength, 'utf8'); ... payloadBuffer = Buffer.from(payload); return Buffer.concat([headerBuffer, payloadBuffer], 8 + payloadBuffer.length); } decode () {} }
import { EventEmitter } from 'events'; class BaseWindow extends EventEmitter { open () {} close () {} isExist () {} destroy() {} createWindow() { this.win = new BrowserWindow({ ...this.browserConfig, }); } ... }
class ImWindow extends BaseWindow { browserConfig = { width: 0, height: 0, show: false, } ... }
import sqlite3 from 'sqlite3'; class DAO { constructor(dbFilePath) { this.db = new sqlite3.Database(dbFilePath, (err) => { // }); } run(sql, params = []) { return new Promise((resolve, reject) => { this.db.run(sql, params, function (err) { if (err) { reject(err); } else { resolve({ id: this.lastID }); } }); }); } ... }
class BaseModel { constructor(dao, tableName) { this.dao = dao; this.tableName = tableName; } delete(id) { return this.dao.run(`DELETE FROM ${this.tableName} WHERE id = ?`, [id]); } ... }
const dao = new AppDAO('path/to/database-file.sqlite3'); const messageModel = new MessageModel(dao);
import { Tray, nativeImage } from 'electron'; class TrayManager { ... setState() { // 设置默认状态 } startBlink(){ if(!this.tray){ return; } let emptyImg = nativeImage.createFromPath(path.join(__dirname, './empty.ico')); let noticeImg = nativeImage.createFromPath(path.join(__dirname, './newMsg.png')); let visible; clearInterval(this.trayTimer); this.trayTimer = setInterval(()=>{ visible = !visible; if(visible){ this.tray.setImage(noticeImg); }else{ this.tray.setImage(emptyImg); } },500); } //停止闪烁 stopBlink(){ clearInterval(this.trayTimer); this.setState(); } }
2-.png (16.76 KB, 下载次数: 951)
import { remote, ipcRenderer, ipcMain } from 'electron'; function sendIPCEvent(event, ...data) { if (require('./is-electron-renderer')) { const currentWindow = remote.getCurrentWindow(); if (currentWindow) { currentWindow.webContents.send(event, ...data); } ipcRenderer.send(event, ...data); return; } ipcMain.emit(event, null, ...data); } export default sendIPCEvent;
class ipcMainManager extends EventEmitter { constructor() { ipcMain.on('imPush', (name, data) => { this.emit(name, data); }) this.listern(); } listern() { this.on('imPush', (name, data) => { // }); } } class ipcRendererManager extends EventEmitter { push (name, data) { ipcRenderer.send('imPush', name, data); } }
import electron from 'electron'; function getUserDataPath() { if (require('./is-electron-renderer')) { return electron.remote.app.getPath('userData'); } return electron.app.getPath('userData'); } export default getUserDataPath;
来源:即时通讯网 - 即时通讯开发者社区!
轻量级开源移动端即时通讯框架。
快速入门 / 性能 / 指南 / 提问
轻量级Web端即时通讯框架。
详细介绍 / 精编源码 / 手册教程
移动端实时音视频框架。
详细介绍 / 性能测试 / 安装体验
基于MobileIMSDK的移动IM系统。
详细介绍 / 产品截图 / 安装体验
一套产品级Web端IM系统。
详细介绍 / 产品截图 / 演示视频
精华主题数超过100个。
连续任职达2年以上的合格正式版主
为论区做出突出贡献的开发者、版主等。
Copyright © 2014-2024 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.109375 second(s), 39 queries , Gzip On.