是否有人找到了如何制作与默认IoT Core相似的网络服务器?找到的最相似的示例是this但是当我尝试在页面中插入一些javascript时,无法识别。在物联网核心的默认Web服务器中,有很多js和jQuery脚本运行得非常好。 有人有想法吗? Thanx很多
答案 0 :(得分:1)
基于this示例,您可以将HTML文件添加到项目中,并使用此HTML文件托管网页内容,然后在其中插入一些javascript。
HTML文件:
<!DOCTYPE html>
<html>
<head>
<title>Background Message</title>
</head>
<body>
Hello from the background process!<br />
<script type="text/javascript">
var myVariable = 'Hello, I come from script!';
window.alert(myVariable);
</script>
</body>
</html>
您需要编辑部分代码:
using (var response = output.AsStreamForWrite())
{
string page = "";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("index.html");
var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
foreach (var line in readFile)
{
page += line;
}
page += query;
byte[] bodyArray = Encoding.UTF8.GetBytes(page);
var bodyStream = new MemoryStream(bodyArray);
var header = "HTTP/1.1 200 OK\r\n" +
$"Content-Length: {bodyStream.Length}\r\n" +
"Connection: close\r\n\r\n";
byte[] headerArray = Encoding.UTF8.GetBytes(header);
await response.WriteAsync(headerArray, 0, headerArray.Length);
await bodyStream.CopyToAsync(response);
await response.FlushAsync();
}
将应用程序部署到Raspberry Pi后,在应用程序运行时,您可以访问Web服务器。结果将如下所示: