如何使用 Firebase 在 IOS 上实施服务器发送事件?

How to implement Server-Sent Events on IOS using Firebase?

我正在尝试使用 rest 来监听 firebase 事件 api.The 问题是没有调用回调方法。我为此使用 EventSource purpose.Is 这是收听事件的正确方法吗?

Auth.auth().currentUser?.getIDTokenForcingRefresh(true, completion: { (token, error) in
    let server : String =  "https://project-XXXXX.firebaseio.com/.json?auth=\(token!)"

    let eventSource: EventSource = EventSource(url: server)
    eventSource.onOpen {
        // When opened
        debugPrint("eventSource open")
    }

    eventSource.onError { (error) in
        // When errors
        debugPrint("error = \(error?.localizedDescription)")
    }
    eventSource.onMessage { (id, event, data) in
        debugPrint("data = \(data)")
        // Here you get an event without event name!
    }

    eventSource.addEventListener("child_added") { (id, event, data) in
        debugPrint("data = \(data)")
        // Here you get an event 'event-name'
    }
})

我终于找到了答案。我必须添加 'put' 事件监听器而不是 'child_added' 事件监听器。根据documentation,我们只能使用以下事件侦听器:

  • 补丁
  • 保持活跃
  • 取消
  • auth_revoked

(参考:https://github.com/inaka/EventSource/issues/86#issuecomment-354029202