mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
成功进行ipc通信
This commit is contained in:
parent
ab17e3cd30
commit
881570c513
689
package-lock.json
generated
689
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"electron": "^27.0.2",
|
||||
"express": "^4.18.2",
|
||||
"path": "^0.12.7",
|
||||
"stream": "^0.0.2",
|
||||
|
107
src/main.ts
107
src/main.ts
@ -1,20 +1,32 @@
|
||||
// 运行在 Electron 主进程 下的插件入口
|
||||
const express = require("express");
|
||||
|
||||
let groups: Group[] = []
|
||||
let friends: User[] = []
|
||||
const express = require("express")
|
||||
const { ipcMain } = require('electron');
|
||||
const { webContents } = require('electron')
|
||||
|
||||
function getFriend(qq: string){
|
||||
return friends.find(friend => friend.uid == qq)
|
||||
const CHANNEL_SEND_MSG = "llonebot_sendMsg"
|
||||
|
||||
type PostDataSendMsg = {
|
||||
action: string,
|
||||
params: {
|
||||
user_id: string,
|
||||
group_id: string,
|
||||
message: SendMessage[];
|
||||
}
|
||||
}
|
||||
|
||||
function getGroup(qq: string){
|
||||
return groups.find(group => group.uid == qq)
|
||||
function sendIPCCallSendQQMsg(postData: PostDataSendMsg){
|
||||
let contents = webContents.getAllWebContents();
|
||||
for (const content of contents) {
|
||||
try{
|
||||
content.send(CHANNEL_SEND_MSG, postData)
|
||||
}catch (e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 加载插件时触发
|
||||
function onLoad(plugin: any) {
|
||||
|
||||
function startExpress(event: any){
|
||||
// const original_send = (window.webContents.__qqntim_original_object && window.webContents.__qqntim_original_object.send) || window.webContents.send;
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
@ -23,48 +35,61 @@ function onLoad(plugin: any) {
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/', (req: any, res: any) => {
|
||||
// original_send.call(window.webContents, "发送消息", {test: "test"})
|
||||
// event.reply("发送消息", {test: "test"})
|
||||
|
||||
res.send('llonebot已启动');
|
||||
})
|
||||
// 处理POST请求的路由
|
||||
app.post('/', (req: any, res: any) => {
|
||||
let json_data: {action: string, params: {
|
||||
user_id: string,
|
||||
group_id: string,
|
||||
message: SendMessage[];
|
||||
}} = req.body;
|
||||
let peer: Peer| null = null;
|
||||
if (json_data.action == "send_private_msg"){
|
||||
let friend = getFriend(json_data.params.user_id)
|
||||
if (friend) {
|
||||
peer = {
|
||||
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){
|
||||
peer = {
|
||||
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))
|
||||
}
|
||||
console.log(req.body); // 输出POST请求的请求体数据
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
sendIPCCallSendQQMsg(jsonData);
|
||||
// let json_data: {action: string, params: {
|
||||
// user_id: string,
|
||||
// group_id: string,
|
||||
// message: SendMessage[];
|
||||
// }} = req.body;
|
||||
// let peer: Peer| null = null;
|
||||
// if (json_data.action == "send_private_msg"){
|
||||
// let friend = getFriend(json_data.params.user_id)
|
||||
// if (friend) {
|
||||
// peer = {
|
||||
// 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){
|
||||
// peer = {
|
||||
// chatType: "group",
|
||||
// name: group.name,
|
||||
// uid: group.uid
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (peer) {
|
||||
// original_send.call(window.webContents, "发送消息", peer, json_data.params.message)
|
||||
// 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请求已收到');
|
||||
});
|
||||
app.listen(port, () => {
|
||||
console.log(`服务器已启动,监听端口 ${port}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 加载插件时触发
|
||||
function onLoad(plugin: any) {
|
||||
ipcMain.on("startExpress", (event: any, arg: any) => {
|
||||
startExpress(event)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,5 +1,17 @@
|
||||
// Electron 主进程 与 渲染进程 交互的桥梁
|
||||
const { contextBridge } = require("electron");
|
||||
const { ipcRenderer } = require('electron');
|
||||
|
||||
// 在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,
|
||||
});
|
5
src/llapi.d.ts → src/renderer.d.ts
vendored
5
src/llapi.d.ts → src/renderer.d.ts
vendored
@ -78,3 +78,8 @@ declare var LLAPI: {
|
||||
getGroupsList(forced: boolean): Promise<Group[]>
|
||||
getFriendsList(forced: boolean): Promise<User[]>
|
||||
};
|
||||
|
||||
declare var llonebot: {
|
||||
listenSendMessage: (handle: (msg: any)=>void)=>void
|
||||
startExpress: ()=>void
|
||||
};
|
@ -1,26 +1,41 @@
|
||||
/// <reference path="./llapi.d.ts" />
|
||||
/// <reference path="./renderer.d.ts" />
|
||||
|
||||
// import express from "express";
|
||||
// const { ipcRenderer } = require('electron');
|
||||
|
||||
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 uid_maps: Record<string, string> = {} // 一串加密的字符串 -> qq号
|
||||
|
||||
function onLoad(){
|
||||
LLAPI.getAccountInfo().then(accountInfo => {
|
||||
llonebot.startExpress();
|
||||
window.LLAPI.getAccountInfo().then(accountInfo => {
|
||||
self_qq = accountInfo.uid
|
||||
})
|
||||
|
||||
LLAPI.getGroupsList(false).then(groupsList => {
|
||||
window.LLAPI.getGroupsList(false).then(groupsList => {
|
||||
groups = groupsList
|
||||
})
|
||||
|
||||
|
||||
|
||||
LLAPI.on("new-messages", (messages) => {
|
||||
console.log("收到新消息", messages)
|
||||
window.LLAPI.on("new-messages", (messages) => {
|
||||
try{
|
||||
llonebot.listenSendMessage(console.log);
|
||||
console.log("ipc监听成功")
|
||||
} catch (e){
|
||||
console.log("ipc监听失败", e)
|
||||
}
|
||||
messages.forEach(message => {
|
||||
let onebot_message_data: any = {
|
||||
self: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user