Cortana 运行 遇到问题
Cortana ran into an issue
我创建了一个 javascript 应用程序(又名 UWA),以便与我的 Belkin wemo 一起玩,然后用 Cortana 打开或关闭灯。以下函数调用得很好,但 Cortana 最终遇到了问题。如果我删除对 HTTP 调用的调用,该程序可以正常工作。谁能告诉我下面的函数有什么问题,不幸的是没有更多的细节暴露出来(当然在真正的程序中被替换为正确的URL):
function setWemo(status) {
WinJS.xhr({ url: "<url>" }).then(function () {
var userMessage = new voiceCommands.VoiceCommandUserMessage();
userMessage.spokenMessage = "Light is now turned " + status;
var statusContentTiles = [];
var statusTile = new voiceCommands.VoiceCommandContentTile();
statusTile.contentTileType = voiceCommands.VoiceCommandContentTileType.titleOnly;
statusTile.title = "Light is set to: " + status;
statusContentTiles.push(statusTile);
var response = voiceCommands.VoiceCommandResponse.createResponse(userMessage, statusContentTiles);
return voiceServiceConnection.reportSuccessAsync(response);
}).done();
}
确保您的后台任务可以访问 WinJS 命名空间。对于后台任务,由于没有任何 default.html,除非您明确执行,否则 base.js 不会自动导入。
我必须将 winjs 更新到版本 4.2 from here (or the source repository on git), then add that to my project to update from the released version that comes with VS 2015. WinJS 4.0 has a bug where it complains about gamepad controls if you try to import it this way (see this MSDN forum post)
然后我添加了一行
importScripts("/Microsoft.WinJS.4.0/js/base.js");
到脚本起始代码的顶部以导入 WinJS。如果没有这个,您可能会在调试控制台中弹出 "WinJS is undefined" 之类的错误,但出于某种原因,每当我点击它时,我都不会在 visual studio 中遇到调试中断。这导致 Cortana 会话挂起,什么都不做,从不发送最终响应。
我还要补充一点,你应该处理错误和处理进度,这样你就可以定期向 Cortana 发送进度报告,以确保它不会让你超时(这就是它给你错误的原因,可能大约 5 秒后):
WinJS.xhr({ url: "http://urlhere/", responseType: "text" }).done(function completed(webResponse) {
... handle response here
},
function error(errorResponse) {
... error handling
},
function progress(requestProgress) {
... <some kind of check to see if it's been longer than a second or two here since the last progress report>
var userProgressMessage = new voiceCommands.VoiceCommandUserMessage();
userProgressMessage.DisplayMessage = "Still working on it!";
userProgressMessage.SpokenMessage = "Still working on it";
var response = voiceCommands.VoiceCommandResponse.createResponse(userProgressMessage);
return voiceServiceConnection.reportProgressAsync(response);
});
我创建了一个 javascript 应用程序(又名 UWA),以便与我的 Belkin wemo 一起玩,然后用 Cortana 打开或关闭灯。以下函数调用得很好,但 Cortana 最终遇到了问题。如果我删除对 HTTP 调用的调用,该程序可以正常工作。谁能告诉我下面的函数有什么问题,不幸的是没有更多的细节暴露出来(当然在真正的程序中被替换为正确的URL):
function setWemo(status) {
WinJS.xhr({ url: "<url>" }).then(function () {
var userMessage = new voiceCommands.VoiceCommandUserMessage();
userMessage.spokenMessage = "Light is now turned " + status;
var statusContentTiles = [];
var statusTile = new voiceCommands.VoiceCommandContentTile();
statusTile.contentTileType = voiceCommands.VoiceCommandContentTileType.titleOnly;
statusTile.title = "Light is set to: " + status;
statusContentTiles.push(statusTile);
var response = voiceCommands.VoiceCommandResponse.createResponse(userMessage, statusContentTiles);
return voiceServiceConnection.reportSuccessAsync(response);
}).done();
}
确保您的后台任务可以访问 WinJS 命名空间。对于后台任务,由于没有任何 default.html,除非您明确执行,否则 base.js 不会自动导入。
我必须将 winjs 更新到版本 4.2 from here (or the source repository on git), then add that to my project to update from the released version that comes with VS 2015. WinJS 4.0 has a bug where it complains about gamepad controls if you try to import it this way (see this MSDN forum post)
然后我添加了一行
importScripts("/Microsoft.WinJS.4.0/js/base.js");
到脚本起始代码的顶部以导入 WinJS。如果没有这个,您可能会在调试控制台中弹出 "WinJS is undefined" 之类的错误,但出于某种原因,每当我点击它时,我都不会在 visual studio 中遇到调试中断。这导致 Cortana 会话挂起,什么都不做,从不发送最终响应。
我还要补充一点,你应该处理错误和处理进度,这样你就可以定期向 Cortana 发送进度报告,以确保它不会让你超时(这就是它给你错误的原因,可能大约 5 秒后):
WinJS.xhr({ url: "http://urlhere/", responseType: "text" }).done(function completed(webResponse) {
... handle response here
},
function error(errorResponse) {
... error handling
},
function progress(requestProgress) {
... <some kind of check to see if it's been longer than a second or two here since the last progress report>
var userProgressMessage = new voiceCommands.VoiceCommandUserMessage();
userProgressMessage.DisplayMessage = "Still working on it!";
userProgressMessage.SpokenMessage = "Still working on it";
var response = voiceCommands.VoiceCommandResponse.createResponse(userProgressMessage);
return voiceServiceConnection.reportProgressAsync(response);
});