【问题标题】:Windows IoT Core on Raspberry PI default webserverRaspberry PI 默认网络服务器上的 Windows IoT Core
【发布时间】:2016-12-08 06:53:09
【问题描述】:

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

【问题讨论】:

    标签: windows webserver core iot


    【解决方案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 服务器。结果将如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多