-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
233e01f
commit 1a99863
Showing
12 changed files
with
326 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,11 @@ | |
## 更新日志 | ||
### v1.1.5 | ||
* 可以指定默认频道 | ||
* 内置服务器,局域网内可配置 | ||
### v1.1.4 | ||
* 默认使用上次缓存视频源 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.lizongying.mytv0 | ||
|
||
import java.io.IOException | ||
import java.net.Inet4Address | ||
import java.net.NetworkInterface | ||
import java.net.ServerSocket | ||
|
||
object PortUtil { | ||
|
||
fun findFreePort(): Int { | ||
var port = -1 | ||
try { | ||
ServerSocket(0).use { socket -> | ||
port = socket.localPort | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
return port | ||
} | ||
|
||
fun lan(): String? { | ||
val networkInterfaces = NetworkInterface.getNetworkInterfaces() | ||
while (networkInterfaces.hasMoreElements()) { | ||
val inetAddresses = networkInterfaces.nextElement().inetAddresses | ||
while (inetAddresses.hasMoreElements()) { | ||
val inetAddress = inetAddresses.nextElement() | ||
if (inetAddress is Inet4Address) { | ||
return inetAddress.hostAddress | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.lizongying.mytv0 | ||
|
||
|
||
import android.content.Context | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.util.Log | ||
import com.lizongying.mytv0.models.TVList | ||
import fi.iki.elonen.NanoHTTPD | ||
import java.io.IOException | ||
import java.nio.charset.StandardCharsets | ||
|
||
class SimpleServer(private val context: Context, port: Int) : NanoHTTPD(port) { | ||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
init { | ||
try { | ||
start() | ||
val host = PortUtil.lan() | ||
(context as MainActivity).setServer("$host:$port") | ||
println("Server running on $host:$port") | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
override fun serve(session: IHTTPSession): Response { | ||
return when (session.uri) { | ||
"/api/hello" -> handleHelloRequest(session) | ||
"/api/channels" -> handleChannelsRequest(session) | ||
else -> handleStaticContent(session) | ||
} | ||
} | ||
|
||
private fun handleHelloRequest(session: IHTTPSession): Response { | ||
val response = "Hello from NanoHTTPD API!" | ||
return newFixedLengthResponse(Response.Status.OK, "text/plain", response) | ||
} | ||
|
||
private fun handleChannelsRequest(session: IHTTPSession): Response { | ||
try { | ||
val inputStream = session.inputStream | ||
val buf = ByteArray(1024) | ||
var read: Int | ||
val sb = StringBuilder() | ||
while (inputStream.available() > 0) { | ||
read = inputStream.read(buf, 0, Math.min(buf.size, inputStream.available())) | ||
sb.append(String(buf, 0, read)) | ||
} | ||
val requestBody = sb.toString() | ||
Log.i(TAG, requestBody) | ||
handler.post { | ||
TVList.str2List(requestBody) | ||
} | ||
} catch (e: IOException) { | ||
return newFixedLengthResponse( | ||
Response.Status.INTERNAL_ERROR, | ||
MIME_PLAINTEXT, | ||
"SERVER INTERNAL ERROR: IOException: " + e.message | ||
) | ||
} | ||
val response = "Success!" | ||
return newFixedLengthResponse(Response.Status.OK, "text/plain", response) | ||
} | ||
|
||
private fun handleStaticContent(session: IHTTPSession): Response { | ||
val html = loadHtmlFromResource(R.raw.index) | ||
return newFixedLengthResponse(Response.Status.OK, "text/html", html) | ||
} | ||
|
||
private fun loadHtmlFromResource(resourceId: Int): String { | ||
val inputStream = context.resources.openRawResource(resourceId) | ||
return inputStream.bufferedReader(StandardCharsets.UTF_8).use { it.readText() } | ||
} | ||
|
||
companion object { | ||
const val TAG = "SimpleServer" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.