
HTTP Server Sample
3 ratings
)Overview
HTTP Server Sample
Web Server ========== This sample is a demonstration of Chrome's new networking stack. Chrome now has the ability to listen on a tcp port AND to accept. How it works Create a socket and convert it to a server socket. socket.create("tcp", {}, function(_socketInfo) { socketInfo = _socketInfo; // Cache globally [eek] socket.listen(socketInfo.socketId, "127.0.0.1", 8080, 20, function(result) { //Accept the first response socket.accept(socketInfo.socketId, onAccept); }); }); Create an onAccept handler that is triggered for an incomming request. var onAccept = function(acceptInfo) { // This is a request that the system is processing. // Read the data. socket.read(acceptInfo.socketId, function(readInfo) { // Parse the request. var data = arrayBufferToString(readInfo.data); // We only want to handle get requests if(data.indexOf("GET ") == 0) { // we can only deal with GET requests var uriEnd = data.indexOf(" ", 4); if(uriEnd < 0) { /* throw a wobbler */ return; } var uri = data.substring(4, uriEnd); var file = filesMap[uri]; // pick out the file we want to server if(!!file == false) { /* File does not exist */ return; } write200Response(acceptInfo.socketId, file); } else { // Throw an error socket.destroy(acceptInfo.socketId); // We need to say that we can accept another incoming request. socket.accept(socketInfo.socketId, onAccept); } }); }; Write the response back to the client. var write200Response = function(socketId, file) { var contentType = (file.type === "") ? "text/plain" : file.type; var contentLength = file.size; // Create an ArrayBuffer for the HTTP response. var header = stringToUint8Array("HTTP/1.0 200 OK\nContent-length: " + file.size + "\nContent-type:" + contentType + "\n\n"); var outputBuffer = new ArrayBuffer(header.byteLength + file.size); var view = new Uint8Array(outputBuffer) view.set(header, 0); // Read the file from the filesystem. var fileReader = new FileReader(); fileReader.onload = function(e) { view.set(new Uint8Array(e.target.result), header.byteLength); // Write it out to the client that made the request socket.write(socketId, outputBuffer, function(writeInfo) { // Kill the client socket socket.destroy(socketId); // We need to say that we can accept another incoming request. socket.accept(socketInfo.socketId, onAccept); See the source code at https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/webserver
3.7 out of 53 ratings
Google doesn't verify reviews. Learn more about results and reviews.
Details
- Version3.2
- UpdatedApril 15, 2015
- Offered byChrome App Samples
- Size256KiB
- LanguagesEnglish
- Non-traderThis developer has not identified itself as a trader. For consumers in the European Union, please note that consumer rights do not apply to contracts between you and this developer.
Privacy
Support
For help with questions, suggestions, or problems, visit the developer's support site