成功进行ipc通信

This commit is contained in:
linyuchen 2023-10-27 17:31:19 +08:00
parent ab17e3cd30
commit 881570c513
6 changed files with 789 additions and 58 deletions

689
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"electron": "^27.0.2",
"express": "^4.18.2", "express": "^4.18.2",
"path": "^0.12.7", "path": "^0.12.7",
"stream": "^0.0.2", "stream": "^0.0.2",

View File

@ -1,20 +1,32 @@
// 运行在 Electron 主进程 下的插件入口 // 运行在 Electron 主进程 下的插件入口
const express = require("express");
let groups: Group[] = [] const express = require("express")
let friends: User[] = [] const { ipcMain } = require('electron');
const { webContents } = require('electron')
function getFriend(qq: string){ const CHANNEL_SEND_MSG = "llonebot_sendMsg"
return friends.find(friend => friend.uid == qq)
type PostDataSendMsg = {
action: string,
params: {
user_id: string,
group_id: string,
message: SendMessage[];
}
} }
function getGroup(qq: string){ function sendIPCCallSendQQMsg(postData: PostDataSendMsg){
return groups.find(group => group.uid == qq) let contents = webContents.getAllWebContents();
for (const content of contents) {
try{
content.send(CHANNEL_SEND_MSG, postData)
}catch (e) {
}
}
} }
// 加载插件时触发 function startExpress(event: any){
function onLoad(plugin: any) { // const original_send = (window.webContents.__qqntim_original_object && window.webContents.__qqntim_original_object.send) || window.webContents.send;
const app = express(); const app = express();
const port = 3000; const port = 3000;
@ -23,48 +35,61 @@ function onLoad(plugin: any) {
app.use(express.json()); app.use(express.json());
app.get('/', (req: any, res: any) => { app.get('/', (req: any, res: any) => {
// original_send.call(window.webContents, "发送消息", {test: "test"})
// event.reply("发送消息", {test: "test"})
res.send('llonebot已启动'); res.send('llonebot已启动');
}) })
// 处理POST请求的路由 // 处理POST请求的路由
app.post('/', (req: any, res: any) => { app.post('/', (req: any, res: any) => {
let json_data: {action: string, params: { let jsonData: PostDataSendMsg = req.body;
user_id: string, sendIPCCallSendQQMsg(jsonData);
group_id: string, // let json_data: {action: string, params: {
message: SendMessage[]; // user_id: string,
}} = req.body; // group_id: string,
let peer: Peer| null = null; // message: SendMessage[];
if (json_data.action == "send_private_msg"){ // }} = req.body;
let friend = getFriend(json_data.params.user_id) // let peer: Peer| null = null;
if (friend) { // if (json_data.action == "send_private_msg"){
peer = { // let friend = getFriend(json_data.params.user_id)
chatType: "private", // if (friend) {
name: friend.nickName, // peer = {
uid: friend.uin // chatType: "private",
} // name: friend.nickName,
} // uid: friend.uin
} // }
else if (json_data.action == "send_group_msg"){ // }
let group = getGroup(json_data.params.group_id) // }
if (group){ // else if (json_data.action == "send_group_msg"){
peer = { // let group = getGroup(json_data.params.group_id)
chatType: "group", // if (group){
name: group.name, // peer = {
uid: group.uid // chatType: "group",
} // name: group.name,
} // uid: group.uid
} // }
if (peer) { // }
LLAPI.sendMessage(peer, json_data.params.message).then(res => console.log("消息发送成功:", res), // }
err => console.log("消息发送失败", json_data, err)) // if (peer) {
} // original_send.call(window.webContents, "发送消息", peer, json_data.params.message)
console.log(req.body); // 输出POST请求的请求体数据 // LLAPI.sendMessage(peer, json_data.params.message).then(res => console.log("消息发送成功:", res),
// err => console.log("消息发送失败", json_data, err))
// }
// console.log(req.body); // 输出POST请求的请求体数据
res.send('POST请求已收到'); res.send('POST请求已收到');
}); });
app.listen(port, () => { app.listen(port, () => {
console.log(`服务器已启动,监听端口 ${port}`); console.log(`服务器已启动,监听端口 ${port}`);
}); });
}
// 加载插件时触发
function onLoad(plugin: any) {
ipcMain.on("startExpress", (event: any, arg: any) => {
startExpress(event)
})
} }

View File

@ -1,5 +1,17 @@
// Electron 主进程 与 渲染进程 交互的桥梁 // Electron 主进程 与 渲染进程 交互的桥梁
const { contextBridge } = require("electron"); const { contextBridge } = require("electron");
const { ipcRenderer } = require('electron');
// 在window对象下导出只读对象 // 在window对象下导出只读对象
contextBridge.exposeInMainWorld("plugin_template", { contextBridge.exposeInMainWorld("llonebot", {
listenSendMessage: (handle: (msg: any)=>void)=>{
ipcRenderer.on("sendMsg", (event: any, args: any) => {
handle("收到ipc消息发送消息")
console.log("收到ipc消息发送消息", args); // 处理主进程发送的消息
})
},
startExpress:()=>{
ipcRenderer.send("startExpress");
}
// startExpress,
}); });

View File

@ -78,3 +78,8 @@ declare var LLAPI: {
getGroupsList(forced: boolean): Promise<Group[]> getGroupsList(forced: boolean): Promise<Group[]>
getFriendsList(forced: boolean): Promise<User[]> getFriendsList(forced: boolean): Promise<User[]>
}; };
declare var llonebot: {
listenSendMessage: (handle: (msg: any)=>void)=>void
startExpress: ()=>void
};

View File

@ -1,26 +1,41 @@
/// <reference path="./llapi.d.ts" /> /// <reference path="./renderer.d.ts" />
// import express from "express"; // import express from "express";
// const { ipcRenderer } = require('electron');
const host = "http://localhost:5000" const host = "http://localhost:5000"
let groups: Group[] = []
let friends: User[] = []
function getFriend(qq: string){
return friends.find(friend => friend.uid == qq)
}
function getGroup(qq: string) {
return groups.find(group => group.uid == qq)
}
let self_qq: string = "" let self_qq: string = ""
let uid_maps: Record<string, string> = {} // 一串加密的字符串 -> qq号 let uid_maps: Record<string, string> = {} // 一串加密的字符串 -> qq号
function onLoad(){ function onLoad(){
LLAPI.getAccountInfo().then(accountInfo => { llonebot.startExpress();
window.LLAPI.getAccountInfo().then(accountInfo => {
self_qq = accountInfo.uid self_qq = accountInfo.uid
}) })
LLAPI.getGroupsList(false).then(groupsList => { window.LLAPI.getGroupsList(false).then(groupsList => {
groups = groupsList groups = groupsList
}) })
window.LLAPI.on("new-messages", (messages) => {
try{
llonebot.listenSendMessage(console.log);
LLAPI.on("new-messages", (messages) => { console.log("ipc监听成功")
console.log("收到新消息", messages) } catch (e){
console.log("ipc监听失败", e)
}
messages.forEach(message => { messages.forEach(message => {
let onebot_message_data: any = { let onebot_message_data: any = {
self: { self: {