wechaty/plugin-contrib

timeout when get room payload

jcai opened this issue · 7 comments

huan commented

Yes! If we can not get the room payload, which means the room does not exist (from the program perspective), it will not be able to work anymore.

Please feel free to submit a PR to improve it to be more robust if you have any good idea, thanks!

jcai commented

I think we should use a recursive method to fetch all rooms.I had implemented the feature in Scala-wechaty. see https://github.com/wechaty/scala-wechaty/blob/3119df7f5bcb244bc8f9e6c5c4cd52faa04cf435/wechaty/src/main/scala/wechaty/plugins/RoomConnector.scala#L48

huan commented

Thanks for the sharing! Will take a look into it later.

chs97 commented

maybe, shold waiting for all rooms to be ready, then send a message?
example code like :

return function OneToManyRoomConnectorPlugin (wechaty: Wechaty) {
    log.verbose('WechatyPluginContrib', 'OneToManyRoomConnectorPlugin(%s) installing ...', wechaty)

    let manyRoomList : Room[]

    function waitingRoom(id: string): Promise<Room> {
        return new Promise((resolve, reject) => {
            const room = wechaty.Room.load(id)
            if (room.isReady()) {
                return resolve(room)
            }
            const timeoutTimer = setTimeout(() => {
                finish()
                return reject('timeout')
            }, timeout);
            const readyTimer = setInterval(() => {
                if (room.isReady()) {
                    finish()
                    return resolve(room)
                }
            }, 100)
            const finish = () => {
                clearTimeout(timeoutTimer)
                clearInterval(readyTimer)
            }
        })
    }

    wechaty.once('message', async onceMsg => {
      log.verbose('WechatyPluginContrib', 'OneToManyRoomConnectorPlugin(%s) once(message) installing ...', wechaty)

      if (!manyRoomList) {
        const waitingRooms = config.many.map(id => waitingRoom(id)) // should make sure all rooms ready?
        manyRoomList = Promise.all(waitingRooms)  // await loadRoom(wechaty, config.many)
      }

      matchAndForward(onceMsg, manyRoomList)
      wechaty.on('message', message => matchAndForward(message, manyRoomList))
    })
  }
huan commented

@chs97 When we need to say in a room, that room does not require to be ready-ed.

The following code should work without any problem:

const room = wechaty.Room.load('your_room_id@chatroom')
// The following code will work without any problem no matter whether the room is ready-ed or not
await room.say('hello')
chs97 commented

@chs97 When we need to say in a room, that room does not require to be ready-ed.

The following code should work without any problem:

const room = wechaty.Room.load('your_room_id@chatroom')
// The following code will work without any problem no matter whether the room is ready-ed or not
await room.say('hello')

if room payload can't get successful, the message will send fail? and then, the roomConnector can't work actually? waiting for all rooms ready is a good choice?

huan commented

@chs97

No. Whether the message can be sent successfully does not depend on the payload.

You can send a message successfully without load any payload because the underlying Puppet API of the messageSend() only needs the room-id.