更换过滤器有相同的过滤器也有相同的光标吗?

时间:2018-06-09 11:14:37

标签: javascript rethinkdb

代码:

const WebSocket = require('ws')
const r = require('rethinkdb')

async function start () {
  try {
    /**
     * Connect database
     */
    const conn = await r.connect()
    conn.use('app')

    /**
     * Create socket server
     */
    const wss = new WebSocket.Server({ port: 8082 })
    wss.on('connection', (client) => {
      /**
       * Client setting subscribe rethinkdb data
       * WARNING : doesn't validate payload
       */
      client.on('message', (payload) => {
        const filterData = JSON.parse(payload)

        /**
         * Send all data base on filter
         *

        /**
         * Subscribe rethink data base on message request
         * Send all data base on update
         */
        r.table('foo')
          .changes({
            'includeTypes': true,
            'includeInitial': true,
            "squash": true // Squash many changes into one event
          })
          .run(conn, async (err, cursor) => {
            if (err) {
              throw err
            }

            client.cursor = cursor
            cursor.each((err, row) => {
              if (err) {
                throw err
              }

              /**
               * Check if client is connected
               * Else close cursor
               *
               * Also useful for check if connect is dead
               */
              if (client.readyState !== client.OPEN) {
                client.terminate()
                client.cursor.close()
                return
              }

              client.send(JSON.stringify(row))
            })

            /**
             * Close stream when :
             * Client disconnect
             * Client send new message
             */

            client.on('message', () => {
              cursor.close()
            })
          })
      })
    })
  } catch (err) {
    console.log(err)
  }
}

start()

错误

    ReqlDriverError: Cursor is closed.
    at ReqlDriverError.ReqlError [as constructor] (d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\errors.js:23:13)
    at new ReqlDriverError (d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\errors.js:68:50)
    at Feed.IterableResult._promptNext (d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\cursor.js:128:10)
    at d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\cursor.js:200:22
    at tryCatcher (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\util.js:26:23)
    at Function.Promise.fromNode (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\promise.js:168:30)
    at Feed.<anonymous> (d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\cursor.js:203:20)
    at Feed.<anonymous> (d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\util.js:43:16)
    at d:\project\vietnam-traffic-map\websocket\node_modules\rethinkdb\cursor.js:262:24
    at tryCatcher (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\util.js:26:23)
    at Promise.successAdapter (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\nodeify.js:23:30)
    at Promise._settlePromiseAt (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\promise.js:582:21)
    at Promise._settlePromises (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\promise.js:700:14)
    at Async._drainQueue (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\async.js:123:16)
    at Async._drainQueues (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\async.js:133:10)
    at Immediate.Async.drainQueues (d:\project\vietnam-traffic-map\websocket\node_modules\bluebird\js\main\async.js:15:14)

重现问题:

  • 连接一个
  • 再次连接(之前的连接已关闭,但事件未被关闭)
  • 更改rethinkdb中的数据(此时我在if语句中创建了一个断点)
  • 关闭第一个连接 - &gt;启动关闭光标并停止执行下一步
  • 第二个还没有关闭
  • 错误:光标已关闭

问题: changefeeds有相同的过滤器也有相同的光标吗?每个websocket客户端都应该有自己的changefeed游标。为什么我关闭第一个插座,然后第二个接近呢?

1 个答案:

答案 0 :(得分:0)

更改此

if (client.readyState !== client.OPEN) {
    client.terminate()
    client.cursor.close()
    return
}

client.send(JSON.stringify(row))

进入这个:

if (client.readyState === client.OPEN) {
  client.send(JSON.stringify(row))
} else {
  await cursor.close()
  client.terminate()
} 

解决我的问题。我仍然得到错误: ReqlDriverError:光标已关闭。在try catch ...