gpt4 book ai didi

javascript - 在主渲染器和渲染器之间加载 html 后获取消息时出现问题

转载 作者:行者123 更新时间:2023-11-28 03:12:34 26 4
gpt4 key购买 nike

我正在构建一个 Electron 应用程序,但遇到了以下问题。我正在使用设备中的套接字获取信息,并且工作正常。如果程序获取某种类型的消息,我想更改页面的 html,所以基本上我使用了 loadUrl 方法,但是,在向渲染器进程发送消息后,我似乎没有收到它。

我的代码:

photoViewOn = false;
...
app.on('ready', function(){
// Create new window
mainWindow = new BrowserWindow({
backgroundColor: '#000000',
fullscreen : true,
frame : false,
icon : __dirname + "/res/logo.png",
webPreferences: {
nodeIntegration : true
}
});


mainWindow.webContents.openDevTools();
// Load html in window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'bigNames.html'),
protocol: 'file:',
slashes:true,
}))...)

function HTMLupdate(msg) {
mainWindow && mainWindow.webContents.send('update', msg);


var server = socketBuilder('localhost', '7777', {
message: (msg, rinfo) => {
try {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
var infoMap = processCyrano(msg);
//if (infoMap["Com"] === "")
if (infoMap != null) {
if (infoMap["Com"] === "INFO") {
if (photoViewOn) {
photoViewOn = false;
bigNamesView();
}
console.log("Inside Infomap");
console.log(`Left Fencer: ${infoMap["LeftName"]}`);
console.log(`Right Fencer: ${infoMap["RightName"]}`);
HTMLupdate(infoMap);
}
}
}

catch (error) {
console.log(`Error ${error}`);
}


},
error: (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
},
listen: () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
}
});


function photoView() {
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'photos.html'),
protocol: 'file:',
slashes:true,
}));
}


function bigNamesView() {
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'bigNames.html'),
protocol: 'file:',
slashes:true,
}));
}

function processCyrano(msg) {

try {

let stripMsg = msg.toString().split("%");
let info = {};
let compInfo = stripMsg[0].split("|");
console.log(compInfo);

if(compInfo[2] === "INFO" || compInfo[2] === "DISP") {
let firstFencerInfo = stripMsg[1].split("|")
let secondFencerInfo = stripMsg[2].split("|")
info.Protocol = compInfo[1];
info.Com = compInfo[2]
info.Piste = compInfo[3]
info.Compe = compInfo[4];
info.Phase = compInfo[5];
info.PoulTab = compInfo[6];
info.Match = compInfo[7];
info.Round = compInfo[8];
info.RightId = firstFencerInfo[1];
info.RightName = firstFencerInfo[2];
info.RightNat = firstFencerInfo[3];
info.Rscore = firstFencerInfo[4];
info.Rstatus = firstFencerInfo[5];
info.RYcard = firstFencerInfo[6];
info.Rrcard = firstFencerInfo[7];
info.Rlight = firstFencerInfo[8];
info.RWlight = firstFencerInfo[9];
info.LeftId = secondFencerInfo[1];
info.LeftName = secondFencerInfo[2];
info.LeftNat = secondFencerInfo[3];
info.Lscore = secondFencerInfo[4];
info.Lstatus = secondFencerInfo[5];
info.LYcard = secondFencerInfo[6];
info.Lrcard = secondFencerInfo[7];
info.Llight = secondFencerInfo[8];
info.LWlight = secondFencerInfo[9];
lastMatch = info;
return info;
}

else if (compInfo[2] === "PHOTO-NEXT") {
console.log("Photo-Next received");
photoViewOn = true;
photoView();
}

else if (compInfo[2] === "PHOTO-SCORE") {
console.log("Photo-score received");
photoViewOn = true;
photoView();
}

else if (compInfo[2] === "PHOTO-STOP") {
console.log("Photo-Stop received");
return lastMatch;
}
return null;
}
catch (error) {
//Avoid empty messages of the protocol
console.log(`Error ${error}`);
return null;
}
}

基本上我的尝试是,如果收到“Photo-Score”消息,请调用 photoView() (这很好用),如果收到“Photo-Stop”,请调用 bigNamesView() 并开始使用 HTMLUpdate 发送信息(msg),但这对我不起作用。知道为什么会发生这种情况吗?请注意,我删除了一些不相关的代码。谢谢。

最佳答案

嗨,我的 friend ,你的问题是 lastMatch = info 只发生在内部

if(compInfo[2] === "INFO"|| compInfo[2] === "DISP") {

因此,只有当收到 "Photo-Stop" 时,您才返回 lastMatch
.

在代码中,您的逻辑仅在我们收到 INFO 调用 bigNamesView() 时才显示

但只有当我们收到 PHOTO-STOP 调用 bigNamesView() 时您才需要

.

另外关于 loadurl 方法,您立即在 HTMLupdate 中调用 mainWindow.webContents.send('update', msg); 您应该等待事件 did-finish-load然后调用发送更新消息

win.webContents.on('did-finish-load', () => {
HTMLupdate(infoMap);
})

关于javascript - 在主渲染器和渲染器之间加载 html 后获取消息时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59970149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com