本文介绍了混淆使用Nginx推送流的Android客户端使用库服务器已发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的服务器用于Nginx作为Web服务器,并添加 nginx_push_stream .在用于推送流之前,Restfull曾被使用过,然后更改为Websocket,但是当客户端或服务器带宽较小时,websocket有时会丢失.在2019年,从Websocekt到服务器发送事件(SSE)/事件源,例如事件流或文本/事件流,以减少客户端或服务器的损失.

Our server is used to Nginx as webserver and add compile module of nginx_push_stream. Before used to push stream had used to Restfull then changed to Websocket but websocket sometimes loss when the client or server had small bandwith. At 2019 year, from Websocekt to Server Sent Event (SSE) / event-source such as event stream or text/event-stream to reduce loss both of client or server.

请任何对库事件流有任何想法的人都可以使用android客户端和iPhone客户端.

Please anyone have any idea for library event stream is able to use to android client and iPhone client.

我已经习惯了Okhttp,但是还没有准备好使用的事件流,RxSSE无法在Android中使用,完全没有响应.

I have already used to Okhttp but there is not ready yet used event stream, RxSSE is not able to use in Android no respond at all.

我希望明年OkHttp已经更新了适用于Android客户端和iPhone客户端的OkHttp-EventSource

I hope that next year OkHttp is already update OkHttp-EventSource for Android Client also iPhone Client

推荐答案

3天后,Struggling有了搜索库来支持Android客户端的SSE.然后,我找到了这个博客访问SSE 对实施SSE很有帮助,这也是图书馆SSE

After 3 days, Struggling had search library for supporting SSE of Android client. Then, i found this blog Accessing SSE help me a lot to implementation SSE, also this the library SSE

此示例实现以kotlin版本为例,甚至认为库是Java版本.

This sample implementation SSE in kotlin version, even thought library is java version.

1.准备事件处理程序源

interface DefaultEventHandler : EventHandler {

    @Throws(Exception::class)
    override fun onOpen() {
        Log.i("open","open")
    }

    @Throws(Exception::class)
    override fun onClosed() {
        Log.i("close","close")
    }

    @Throws(Exception::class)
    override fun onMessage(event: String, messageEvent: MessageEvent) {
        Log.i("event", messageEvent.data)
    }

    override fun onError(t: Throwable) {
        Log.e("error", t.toString())
    }

    override fun onComment(comment: String) {
        Log.i("event", comment)
    }
}

class MessageEventHandler : DefaultEventHandler {

    override fun onMessage(event: String, messageEvent: MessageEvent) {
        super.onMessage(event, messageEvent)
        val data = messageEvent.data
        Log.i("message", data)
    }
}

2.实施事件源


import java.net.URI
import java.util.concurrent.TimeUnit

.....
 
fun initEventSource(url: String) {
   val eventHandler = MessageEventHandler()

   try {
     val eventSource: EventSource = EventSource.Builder(handler, URI.create(url))
        .reconnectTimeMs(3000)
        .build()
   
      eventSource.start()

      TimeUnit.SECONDS.sleep(10)
   } catch (e: Exception) {
      Log.e("error", e.toString())
   } 
}

我希望这是从客户端到服务器的替代方法协议,而不是使用的RESTfull或Websocket.当服务器总是向客户端发送数据而无需客户端作为流发送请求时.

I hope this would be alternative method protocol from client to server than used RESTfull or Websocket. When server always sent data to client without need request from client as stream.

我添加了使用SSE库的要点 https://gist.github.com/subhanshuja/9079ec9df0927b1da26ee57cf9da1f26 .

I have added a gist of using library SSE https://gist.github.com/subhanshuja/9079ec9df0927b1da26ee57cf9da1f26.

这篇关于混淆使用Nginx推送流的Android客户端使用库服务器已发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 15:14