Windows Raspberry PI 默认网络服务器上的 IoT Core
Windows IoT Core on Raspberry PI default webserver
有人找到如何制作与默认 IoT 核心服务器类似的网络服务器吗?找到的最相似的示例是 this 但是当我尝试在页面中插入一些 javascript 时,无法识别。在 IoT Core 的默认网络服务器中,有很多运行良好的 js 和 jQuery 脚本。
有人有想法吗?
非常感谢
基于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后,应用运行即可访问网络服务器。结果将如下所示:
有人找到如何制作与默认 IoT 核心服务器类似的网络服务器吗?找到的最相似的示例是 this 但是当我尝试在页面中插入一些 javascript 时,无法识别。在 IoT Core 的默认网络服务器中,有很多运行良好的 js 和 jQuery 脚本。 有人有想法吗? 非常感谢
基于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后,应用运行即可访问网络服务器。结果将如下所示: