웨이커 AI 원문 연동 방법
언론사 원문: 웨이커가 수신한 원시 데이터 상태 웨이커 AI 원문: AI로 언론사 원문의 분석과 태깅, 번역, 요약 등 가공 프로세스가 완료된 상태
AI WebSocket은 위젯, DataSet, Ws 어떤 키를 사용하시든 사용이 가능합니다.
웨이커에서 제공하는 WebSocket은 STOMP 형식입니다.
Spring boot와 Kotlin을 통한 예제 코드로 간단한 설명을 작성합니다.
package com.example.demo
import com.example.demo.client.StompClient
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
import org.springframework.boot.runApplication
import org.springframework.messaging.simp.stomp.StompHeaders
import org.springframework.web.socket.WebSocketHttpHeaders
@SpringBootApplication(exclude = [DataSourceAutoConfiguration::class])
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
val productKey = "***********"
val authKey = "Bearer *****************"
val destination = "/news/us"
val stompClient = StompClient()
val sessionHandler = StompClient.MyStompSessionHandler(
productKey = productKey,
authKey = authKey,
destination = destination
)
val connectHeader = WebSocketHttpHeaders()
connectHeader["Waiker-Product-Key"] = productKey
connectHeader["Authorization"] = authKey
connectHeader["Sec-Websocket-Protocol"] = "v12.stomp, v11.stomp, v10.stomp"
connectHeader["host"] = "oapi.waiker.ai"
connectHeader["accept-version"] ="13"
connectHeader["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
stompClient.connect("wss://oapi.waiker.ai/v2/center-ws", connectHeader, sessionHandler)
}
- stompClient를 통한 connect 연결 시 인증키가 헤더에 포함되어야합니다.
- Sec-Websocket-Protocal 의 경우 STOMP 헤더 포함에 필요한 데이터 입니다.
- connect 이후 StompClient의 SessionHandler에서 afterConnected를 콜백하여 session.subscribe가 실행됩니다.
- subscribe(구독) 시에도 인증 헤더 값이 필요합니다.
- subscirbe 시에 header로 destination, product-key, auth가 필요합니다.
package com.example.demo.client
import org.springframework.messaging.converter.MappingJackson2MessageConverter
import org.springframework.messaging.simp.stomp.*
import org.springframework.stereotype.Component
import org.springframework.web.socket.WebSocketHttpHeaders
import org.springframework.web.socket.client.standard.StandardWebSocketClient
import org.springframework.web.socket.messaging.WebSocketStompClient
import java.lang.reflect.Type
@Component
class StompClient {
private lateinit var stompClient: WebSocketStompClient
init {
val websocketClient = StandardWebSocketClient()
stompClient = WebSocketStompClient(websocketClient)
stompClient.messageConverter = MappingJackson2MessageConverter()
}
fun connect(url: String, headers: WebSocketHttpHeaders, sessionHandler: StompSessionHandler): StompSession {
val connectFuture = stompClient.connect(url, headers, sessionHandler)
return connectFuture.get()
}
class MyStompSessionHandler(
private val productKey: String,
private val authKey: String,
private val destination: String
) : StompSessionHandlerAdapter() {
override fun afterConnected(session: StompSession, connectedHeaders: StompHeaders) {
println("Connected to STOMP server")
val headers = StompHeaders()
headers["destination"] = destination
headers["Waiker-Product-Key"] = productKey
headers["Authorization"] = authKey
session.subscribe(headers, this)
}
override fun getPayloadType(headers: StompHeaders): Type {
return String::class.java
}
override fun handleFrame(headers: StompHeaders, payload: Any?) {
println("Received: $payload")
}
override fun handleTransportError(session: StompSession, exception: Throwable) {
println("Transport error: ${exception.message}")
}
override fun handleException(
session: StompSession,
command: StompCommand?,
headers: StompHeaders,
payload: ByteArray,
exception: Throwable
) {
println("Error: ${exception.message}")
}
}
}