我想覆盖(with ) 中的sendMessage
,sendHeaders
, onMessage
,onHalfClose
方法:ServerInterceptor
Context
val context = Context.current().withValue(TestConstants.CONTEXT_KEY, "testValue1")
val delegatedCall = object : SimpleForwardingServerCall<ReqT, RespT>(call) {
override fun sendMessage(message: RespT) {
this@SimpleServerInterceptor.sendMessage(message)
super.sendMessage(message)
}
override fun sendHeaders(headers: Metadata) {
this@SimpleServerInterceptor.sendHeaders(headers)
super.sendHeaders(headers)
}
override fun close(status: Status, trailers: Metadata) {
this@SimpleServerInterceptor.close(status, trailers)
super.close(status, trailers)
}
}
val delegatedListener: ServerCall.Listener<ReqT> =
if (context === null)
next.startCall(delegatedCall, headers)
else
Contexts.interceptCall(context, delegatedCall, headers, next)
return object : ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(delegatedListener) {
override fun onMessage(message: ReqT) {
this@SimpleServerInterceptor.onMessage(message, headers)
super.onMessage(message)
}
override fun onHalfClose() {
this@SimpleServerInterceptor.onHalfClose(headers)
super.onHalfClose()
}
override fun onCancel() {
this@SimpleServerInterceptor.onCancel(headers)
super.onCancel()
}
override fun onComplete() {
this@SimpleServerInterceptor.onComplete(headers)
super.onComplete()
}
override fun onReady() {
this@SimpleServerInterceptor.onReady(headers)
super.onReady()
}
}
这是输出:
>>>>intercept1
>>>>intercept2: testValue1
>>>>onReady1: null
>>>>onMessage1: null
>>>>onHalfClose1: null
HelloService3.hello: testValue1
>>>>sendHeaders2: testValue1
>>>>sendHeaders1: testValue1
>>>>sendMessage2: testValue1
>>>>sendMessage1: testValue1
您可以看到它对( , )Context
有效,但对( , ) 无效。delegatedCall
sendMessage
sendHeaders
delegatedListener
onMessage
onHalfClose
为什么以及如何解决这个问题?