webserverwindows-10-iot-core

Windows IoT Core on Raspberry PI default webserver


Have someone found how to make a similar webserver as the default IoT Core one? The most similar example found is this but when I try to insert some javascript in the page, is not recognized. In the default webserver of the IoT Core there are a lot of js and jQuery scripts that runs very well. Someone have ideas please? Thanx a lot


Solution

  • Based on this sample, you can add a HTML file to your project and use this HTML file host the content of the web page, then insert some javascript in it.

    HTML file:

    <!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>  
    

    You need edit part of code like this:

                        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();
                        }
    

    After deploying your app to Raspberry Pi, while the app running, you can visit the web server. The result will look like this:

    enter image description here