WildDogTeam / wilddog-issue

关于野狗产品的问题,bug反馈和改进意见
2 stars 1 forks source link

onDisconnect 使用问题 #72

Open gitbong opened 7 years ago

gitbong commented 7 years ago

使用场景

  1. 往root_channel节点里push对象A,并监听child_added事件
  2. 在child_added事件回调里,获取对象A的key值(该key值野狗自动分配)
  3. 获取key值后,添加onDisconnect监听,预期可以修改该key值下对象A的_id属性(root_channel/key值/_id)

我遇到的问题如下,望解答

  1. onDisconnect添加后,断线逻辑立即执行
  2. 断线修改的数据并不是root_channel/key值/_id,而是求改了其他key值的_id
ROOT = 'root_channel'

Random = (min, max)->
    sp = max - min
    return min + Math.round(Math.random() * sp)

class Connection
    uid: -1
    channelPath: -1
    constructor: (project, readyFn) ->
        _this = @
        wilddog.initializeApp({
            authDomain: project + ".wilddog.com",
            syncURL: "https://" + project + ".wilddogio.com"
        })

        @ref = wilddog.sync().ref()
        @ref.once("value", (snapshot)->
            if snapshot.val() is null
                _this.ref.set({
                    author: 'gitbong@qq.com'
                    "root_channel": {

                    }
                })

            auth = wilddog.auth()
            if auth && auth.uid
                console.log(auth.uid)
            else
                auth.signInAnonymously((error, authData)->
                    if (error)
                        console.error(error)
                    else
                        console.log (authData.uid)
                        _this.uid = authData.uid
                        if typeof readyFn is 'function' then readyFn()
                )
        )

    ###
        新建频道
    ###

    channelId: -1
    newChannel: (fn)->
        if @channelId != -1
            console.warn 'You have already inited channel, the channelId is ' + @channelId
            return;

        _this = @
        id = Date.now() + Math.floor(Math.random() * 100)
        _ref = wilddog.sync().ref(ROOT)
        _ref.push {
            _id: id
            users: [{uid: _this.uid, isAdm: 1, online: 1}]
        }
        console.log id
        _ref.on('child_added', (data)->
            key = data.key()
            val = data.val()
            if val._id is id
                _this.channelId = key
                _this.channelPath = ROOT + '/' + _this.channelId
                console.log('channelPath:', _this.channelPath)

                disconnectRef = wilddog.sync().ref(ROOT + '/' + key + '/_id')
                disconnectRef.onDisconnect().set('I disconnected!')
                .then(->
                    console.info('disconnect operation has been executed.')
                )
                .catch(->
                    console.info('disconnect operation is failed.')
                )

                if typeof fn is 'function' then fn()
                return

        )

window.Connection = Connection
stackOverMind commented 7 years ago

现在还不知道问题出在哪里,不过有几点需要注意:

1.

disconnectRef.onDisconnect().set('I disconnected!')
                .then(->
                    console.info('disconnect operation has been executed.')
                )

then 执行并不意味着数据已经修改了,而仅仅意味着“这个操作已经委托给服务端了”。

2.

如何获取当前端的在线状态?

wilddog.sync().ref('.info/connected').on('value',function(snapshot){
  if(snapshot.val() == true){
    //当前在线
  }
  else{
  }
})

3.

可能跟数据设计有关,可以只设置一个在线状态位,而不是在每个channel上都设置一个吧

现在来看,这个例子有点长,能否写个更简单一点的

stackOverMind commented 7 years ago

另外,我测试了这个接口的功能,是OK的,测试的版本是2.3.10 不知道你用的版本是什么

我只用了几行代码测试

wilddog.sync().ref('disconnected').onDisconnect().set('disconnect')
wilddog.sync().ref('disconnected2').onDisconnect().set('disconnect')
wilddog.sync().ref('disconnected3').onDisconnect().set('disconnect')

结果都符合预期