forked from kkevsekk1/AutoX
-
Notifications
You must be signed in to change notification settings - Fork 154
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
Showing
9 changed files
with
86 additions
and
33 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
Binary file not shown.
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
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
69 changes: 61 additions & 8 deletions
69
codeeditor/src/main/java/com/aiselp/autojs/codeeditor/web/FileHttpServer.kt
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 |
---|---|---|
@@ -1,18 +1,71 @@ | ||
package com.aiselp.autojs.codeeditor.web | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import android.util.Log | ||
import fi.iki.elonen.SimpleWebServer | ||
import androidx.annotation.RequiresApi | ||
import com.yanzhenjie.andserver.AndServer | ||
import com.yanzhenjie.andserver.Server | ||
import com.yanzhenjie.andserver.annotation.Config | ||
import com.yanzhenjie.andserver.framework.config.WebConfig | ||
import com.yanzhenjie.andserver.framework.website.StorageWebsite | ||
import java.io.File | ||
import java.io.IOException | ||
import java.net.ServerSocket | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.random.Random | ||
|
||
class FileHttpServer(context: Context,path:File) : | ||
SimpleWebServer("127.0.0.1", SPort, path, false) { | ||
|
||
class FileHttpServer(context: Context, path: File) { | ||
companion object { | ||
var SPort = 42201 | ||
fun isPortAvailable(port: Int): Boolean { | ||
try { | ||
ServerSocket(port).use { | ||
return true // 端口可用 | ||
} | ||
} catch (e: IOException) { | ||
return false // 端口已被占用 | ||
} | ||
} | ||
} | ||
|
||
private val port = getPort() | ||
val server: Server = AndServer.webServer(context) | ||
.port(port) | ||
.timeout(10, TimeUnit.SECONDS) | ||
.build(); | ||
|
||
private fun getPort(): Int { | ||
while (true) { | ||
val port = Random.nextInt(2000, 50000) | ||
if (isPortAvailable(port)) { | ||
return port | ||
} | ||
} | ||
} | ||
val port = SPort | ||
init { | ||
SPort++ | ||
Log.d("FileHttpServer", "FileHttpServer init host: ${this.hostname} port: $port") | ||
fun getAddress(): String { | ||
return "http://127.0.0.1:$port" | ||
} | ||
fun start(){ | ||
Log.d("FileHttpServer", "FileHttpServer init host: 127.0.0.1 port: $port") | ||
server.startup() | ||
} | ||
fun stop(){ | ||
server.shutdown() | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
@Config | ||
class AppConfig : WebConfig { | ||
override fun onConfig(context: Context, delegate: WebConfig.Delegate) { | ||
// 增加一个位于/sdcard/Download/AndServer/目录的网站 | ||
delegate.addWebsite( | ||
StorageWebsite( | ||
File( | ||
context.filesDir, "${EditorAppManager.WEB_PUBLIC_PATH}/dist" | ||
).path | ||
) | ||
) | ||
} | ||
} |
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