Raspberry PI 默认网络服务器上的 Windows IoT Core

问题描述 投票:0回答:1

有人找到了如何制作一个与默认 IoT Core 类似的网络服务器吗?找到的最相似的示例是 this,但是当我尝试在页面中插入一些 javascript 时,无法识别。在IoT Core的默认Web服务器中,有很多运行良好的js和jQuery脚本。 有人有想法吗? 非常感谢

webserver windows-10-iot-core
1个回答
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 服务器。结果将如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.