Skip to content

Commit

Permalink
add accessToken into header
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Dec 4, 2023
1 parent f548a69 commit 7f6fdaa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ConnectFactory private constructor(
@JvmOverloads
fun createWebsocketClient(scope: CoroutineScope = CoroutineScope(CoroutineName("WSClient"))): WSClient? {
val builder = StringBuilder()
val header = mutableMapOf<String, String>()
var ws: WSClient? = null
if (config.miraiHttp) {
builder.append(config.url)
Expand All @@ -49,11 +50,12 @@ class ConnectFactory private constructor(
if (config.isAccessToken) {
builder.append("?access_token=")
builder.append(config.token)
header["Authorization"] = "Bearer ${config.token}"
}
}
val url = builder.toString()
try {
ws = WSClient.createAndConnect(scope, URI.create(url), logger, actionHandler)
ws = WSClient.createAndConnect(scope, URI.create(url), logger, actionHandler, header)
} catch (e: Exception) {
logger.error("▌ {} 连接错误,请检查服务端是否开启 ┈━═☆", url)
}
Expand All @@ -66,7 +68,6 @@ class ConnectFactory private constructor(
*/
@JvmOverloads
suspend fun createWebsocketServerAndWaitConnect(scope: CoroutineScope = CoroutineScope(CoroutineName("WSServer"))): Pair<WSServer, Bot>? {
val builder = StringBuilder()
var pair: Pair<WSServer, Bot>? = null
try {
val address = InetSocketAddress(config.reversedPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class WSClient(
private val scope: CoroutineScope,
uri: URI,
private val logger: Logger,
private val actionHandler: ActionHandler
) : WebSocketClient(uri) {
private val actionHandler: ActionHandler,
header: Map<String, String> = mapOf()
) : WebSocketClient(uri, header) {
private var eventBus: EventBus? = null
fun createBot(): Bot {
return Bot(this, actionHandler)
Expand Down Expand Up @@ -79,8 +80,8 @@ class WSClient(
private const val HEART_BEAT = "heartbeat"

val mutex = Mutex()
fun createAndConnect(scope: CoroutineScope, uri: URI, logger: Logger, actionHandler: ActionHandler): WSClient? {
val ws = WSClient(scope, uri, logger, actionHandler)
fun createAndConnect(scope: CoroutineScope, uri: URI, logger: Logger, actionHandler: ActionHandler, header: Map<String, String> = mapOf()): WSClient? {
val ws = WSClient(scope, uri, logger, actionHandler, header)
return if (ws.connectBlocking()) ws else null
}
}
Expand Down

0 comments on commit 7f6fdaa

Please sign in to comment.