首页 > 解决方案 > 如何在 WebUI 中渲染 StreamObserver (ktor + freemarker)

问题描述

如何在 freemarker 中处理 StreamObserver 的输出?我有以下控制器代码以便订阅流频道。

else -> {
                try {
                    //jsonResponse = gnhc.getRequestJsonOutput(pathlist,pretty = true)
                    jsonResponseRaw =  gnhc.subscribev1(pathlist, subId, writer).toString()
                    jsonResponse = jsonResponseRaw
                    application.log.debug("SDN_JSON_PROCESSOR: ${jsonResponse}")
                } catch (e: Exception) {
                    jsonResponse = e.toString()
                    application.log.error("Failed to set channel", e)
                } finally {
                    gnhc.shutdownNow()
                }
            }
        }
        call.respond(FreeMarkerContent("subscribe.ftl", mapOf("hostname" to hostname, "port" to port, "cmd" to cmd, "result" to jsonResponse,"rawresult" to jsonResponseRaw, "pathlist" to pathlist, "error" to error), etag = "e"))
    }

观察者在这里声明:

try {
            // simple observer without writer and subId
            val sr: StreamObserver<Gnmi.SubscribeRequest> = stub.subscribe(GnmiStreamObserver(this))

            // Writer + Id
            //val sr: StreamObserver<Gnmi.SubscribeRequest> = stub.subscribe(StreamResponseWriter(_path,_id,_writer))

            sr.onNext(subRequest)
            waitCompleted()
            sr.onCompleted()

        } 

标签: kotlinfreemarkerktor

解决方案


推荐阅读