improved video playback
post and get request lib
This commit is contained in:
parent
fd84ddb42f
commit
fad3fe019f
@ -47,11 +47,8 @@ class MainPage : HttpHandler {
|
|||||||
val s = path.substring(path.length - 3)
|
val s = path.substring(path.length - 3)
|
||||||
if (s == ".js") mime = "application/javascript"
|
if (s == ".js") mime = "application/javascript"
|
||||||
if (s == "css") mime = "text/css"
|
if (s == "css") mime = "text/css"
|
||||||
if (s == "mp4") {
|
if (s == "mp4") return sendVideo(fs, t) // special reply on videos
|
||||||
mime = "text/css"
|
|
||||||
sendVideo(fs, t)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val h = t.responseHeaders
|
val h = t.responseHeaders
|
||||||
h["Content-Type"] = mime
|
h["Content-Type"] = mime
|
||||||
|
|
||||||
@ -60,19 +57,16 @@ class MainPage : HttpHandler {
|
|||||||
val buffer = ByteArray(0x10000)
|
val buffer = ByteArray(0x10000)
|
||||||
var count: Int
|
var count: Int
|
||||||
|
|
||||||
// todo check if output stream is even open
|
|
||||||
// fs.skip(4096)
|
|
||||||
while (fs.read(buffer).also { count = it } >= 0) {
|
while (fs.read(buffer).also { count = it } >= 0) {
|
||||||
os.write(buffer, 0, count)
|
os.write(buffer, 0, count)
|
||||||
}
|
}
|
||||||
debug("left loop!")
|
|
||||||
fs.close()
|
fs.close()
|
||||||
os.close()
|
os.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendVideo(fs: InputStream, t: HttpExchange) {
|
private fun sendVideo(fs: InputStream, t: HttpExchange) {
|
||||||
var mime = "video/mp4"
|
val size = fs.available()
|
||||||
var offset = 0
|
var offset = 0
|
||||||
t.requestHeaders["Range"]?.get(0)?.apply {
|
t.requestHeaders["Range"]?.get(0)?.apply {
|
||||||
offset = Integer.valueOf(this.substring(this.indexOf("bytes=") + 6, this.indexOf("-")))
|
offset = Integer.valueOf(this.substring(this.indexOf("bytes=") + 6, this.indexOf("-")))
|
||||||
@ -80,24 +74,29 @@ class MainPage : HttpHandler {
|
|||||||
println(offset)
|
println(offset)
|
||||||
val h = t.responseHeaders
|
val h = t.responseHeaders
|
||||||
h["Content-Type"] = "video/mp4"
|
h["Content-Type"] = "video/mp4"
|
||||||
h["Content-Length"] = "${145310237 - offset}"
|
h["Content-Length"] = "${size - offset}"
|
||||||
h["Accept-Ranges"] = "bytes"
|
h["Accept-Ranges"] = "bytes"
|
||||||
|
|
||||||
if (offset != 0) {
|
if (offset != 0) {
|
||||||
h["Content-Range"] = "bytes $offset-145310236/145310237"
|
h["Content-Range"] = "bytes $offset-${size - 1}/$size"
|
||||||
|
// send back partial header on partial data!
|
||||||
|
t.sendResponseHeaders(206, 0)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// send back normal 200 status code on normal request
|
||||||
|
t.sendResponseHeaders(200, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.sendResponseHeaders(200, 0)
|
|
||||||
val os = t.responseBody
|
val os = t.responseBody
|
||||||
val buffer = ByteArray(1024)
|
val buffer = ByteArray(1024)
|
||||||
var count: Int
|
var count: Int
|
||||||
|
|
||||||
// todo check if output stream is even open
|
// todo check if output stream is even open
|
||||||
|
|
||||||
// fs.skip(offset.toLong())
|
// skip offset bytes
|
||||||
|
fs.skip(offset.toLong())
|
||||||
while (fs.read(buffer).also { count = it } >= 0) {
|
while (fs.read(buffer).also { count = it } >= 0) {
|
||||||
|
|
||||||
os.write(buffer, 0, count)
|
os.write(buffer, 0, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package eu.heili.hometheater.website
|
|||||||
import com.sun.net.httpserver.HttpServer
|
import com.sun.net.httpserver.HttpServer
|
||||||
import eu.heili.hometheater.basicutils.Log.Log.criticalerror
|
import eu.heili.hometheater.basicutils.Log.Log.criticalerror
|
||||||
import eu.heili.hometheater.basicutils.Log.Log.info
|
import eu.heili.hometheater.basicutils.Log.Log.info
|
||||||
|
import eu.heili.hometheater.website.datarequests.TestData
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.net.BindException
|
import java.net.BindException
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
@ -13,6 +14,7 @@ class Webserver {
|
|||||||
try {
|
try {
|
||||||
val server = HttpServer.create(InetSocketAddress(8080), 0)
|
val server = HttpServer.create(InetSocketAddress(8080), 0)
|
||||||
server.createContext("/", MainPage())
|
server.createContext("/", MainPage())
|
||||||
|
server.createContext("/data/test", TestData())
|
||||||
// todo insert get and post request sites here!
|
// todo insert get and post request sites here!
|
||||||
server.executor = java.util.concurrent.Executors.newCachedThreadPool() // creates a default executor
|
server.executor = java.util.concurrent.Executors.newCachedThreadPool() // creates a default executor
|
||||||
server.start()
|
server.start()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.wasteinformationserver.website.basicrequest
|
package eu.heili.hometheater.website.basicrequest
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange
|
import com.sun.net.httpserver.HttpExchange
|
||||||
import com.sun.net.httpserver.HttpHandler
|
import com.sun.net.httpserver.HttpHandler
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.wasteinformationserver.website.basicrequest
|
package eu.heili.hometheater.website.basicrequest
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange
|
import com.sun.net.httpserver.HttpExchange
|
||||||
import com.sun.net.httpserver.HttpHandler
|
import com.sun.net.httpserver.HttpHandler
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package eu.heili.hometheater.website.datarequests
|
||||||
|
|
||||||
|
import eu.heili.hometheater.website.basicrequest.PostRequest
|
||||||
|
import java.util.HashMap
|
||||||
|
|
||||||
|
class TestData : PostRequest() {
|
||||||
|
override fun request(params: HashMap<String, String>): String {
|
||||||
|
return """{"status":"ok"}"""
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,13 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
<script src="js/BaseRequest.js"></script>
|
||||||
|
<script src="js/index.js"></script>
|
||||||
<title>Home-Theater</title>
|
<title>Home-Theater</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
main body
|
<div class="mediawrapper">
|
||||||
|
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
27
src/resources/wwwroot/js/BaseRequest.js
Normal file
27
src/resources/wwwroot/js/BaseRequest.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
class Req {
|
||||||
|
static post(url, data, callback) {
|
||||||
|
const xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function(){
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
callback(JSON.parse(xhttp.responseText));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xhttp.open("POST",url,true);
|
||||||
|
xhttp.send(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static get(url, callback) {
|
||||||
|
const xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function(){
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
callback(JSON.parse(xhttp.responseText));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xhttp.open("GET",url,true);
|
||||||
|
xhttp.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
static ready(callback){
|
||||||
|
document.addEventListener('DOMContentLoaded', callback);
|
||||||
|
}
|
||||||
|
}
|
5
src/resources/wwwroot/js/index.js
Normal file
5
src/resources/wwwroot/js/index.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Req.ready((event) => {
|
||||||
|
Req.post("/data/test","action=test",function (dta) {
|
||||||
|
console.log(dta);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user