
love.forte.simbot.component.lovelycat.LovelyCatHttpServer.kt Maven / Gradle / Ivy
/*
*
* * Copyright (c) 2021. ForteScarlet All rights reserved.
* * Project simple-robot
* * File MiraiAvatar.kt
* *
* * You can contact the author through the following channels:
* * github https://github.com/ForteScarlet
* * gitee https://gitee.com/ForteScarlet
* * email [email protected]
* * QQ 1149159218
*
*/
@file:JvmName("lovelycatHttpServers")
package love.forte.simbot.component.lovelycat
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.util.pipeline.*
import io.ktor.utils.io.*
import love.forte.simbot.bot.NoSuchBotException
import love.forte.simbot.component.lovelycat.configuration.LovelyCatServerProperties
import love.forte.simbot.component.lovelycat.message.event.LovelyCatParser
import love.forte.simbot.core.TypedCompLogger
import love.forte.simbot.listener.ListenResult
import love.forte.simbot.listener.MsgGetProcessor
import love.forte.simbot.listener.onMsg
import love.forte.simbot.serialization.json.JsonSerializer
import love.forte.simbot.serialization.json.JsonSerializerFactory
import java.io.Closeable
import java.net.InetAddress
import java.time.LocalDateTime
import kotlin.reflect.jvm.jvmErasure
private val jsonContentType = ContentType.parse("application/json")
private val htmlContentType = ContentType.parse("text/html")
/**
* maybe not use.
*/
private class JsonContentConverter(private val fac: JsonSerializerFactory) : ContentConverter {
override suspend fun convertForReceive(
context: PipelineContext,
): Any? {
val channel = context.subject.value as ByteReadChannel
val message = StringBuilder().apply {
var readLine: Boolean
do {
readLine = channel.readUTF8LineTo(this)
} while (readLine)
channel.cancel()
}.toString()
return fac.getJsonSerializer(context.subject.typeInfo.jvmErasure.java).fromJson(message)
}
override suspend fun convertForSend(
context: PipelineContext,
contentType: ContentType,
value: Any,
): Any? {
val jsonSerializer: JsonSerializer = fac.getJsonSerializer(context.subject.javaClass)
return jsonSerializer.toJson(context.subject)
}
}
interface LovelyCatHttpServer : Closeable {
/**
* 启动可爱猫服务器。
*/
@Throws(Exception::class)
fun start()
/**
* 关闭可爱猫服务器。
*/
override fun close()
}
/**
* 可爱猫事件监听http server。
*/
public class LovelyCatKtorHttpServer(
/** 类型转化函数,根据 'Event' 参数获取对应的解析对象 */
lovelyCatParser: LovelyCatParser,
applicationEngineFactory: ApplicationEngineFactory,
apiManager: LovelyCatApiManager,
jsonSerializerFactory: JsonSerializerFactory,
msgGetProcessor: MsgGetProcessor,
private val lovelyCatServerProperties: LovelyCatServerProperties,
) : LovelyCatHttpServer {
private companion object : TypedCompLogger(LovelyCatKtorHttpServer::class.java)
private val mapSerializer = jsonSerializerFactory.getJsonSerializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy