hooks icon indicating copy to clipboard operation
hooks copied to clipboard

useWebSocket的onOpen问题

Open songrunyu926 opened this issue 2 years ago • 9 comments

在调用onOpen方法时,使用sendMessage失败 发现在onOpen中readyState 状态还是 connecting 0的状态

songrunyu926 avatar Aug 16 '22 02:08 songrunyu926

Hi, songrunyu926.

It seems that this issue is a bit vague and lacks some necessary information.

看起来这条 issue 描述得有些模糊,缺少一些必要的信息。

github-actions[bot] avatar Aug 16 '22 11:08 github-actions[bot]

有 demo 么?

crazylxr avatar Aug 16 '22 11:08 crazylxr

// 在onOpen方法中直接使用sendMessage会有问题 加上定时就好了

const { readyState, sendMessage, latestMessage, connect } = useWebSocket(url, {
    onOpen(event, instance) {
      console.log(readyState)  // 0
      sendMessage(JSON.stringify({ socketType: 0, message: 'keep-alive' }))
      setInterval(() => {
        sendMessage(JSON.stringify({ socketType: 0, message: 'keep-alive' }))
      }, 15000)
    },
    manual: true,
  })

songrunyu926 avatar Aug 31 '22 01:08 songrunyu926

麻烦给一个在线运行的 demo 呢,比如 codesandbox

crazylxr avatar Oct 18 '22 05:10 crazylxr

Since the issue was labeled with needs more info, but no response in 3 days. This issue will be closed. If you have any questions, you can comment and reply. 由于该 issue 被标记为需要更多信息,却 3 天未收到回应。现关闭 issue,若有任何问题,可评论回复。

github-actions[bot] avatar Nov 23 '22 00:11 github-actions[bot]

我也遇到这个问题 image

image

xmsz-stu avatar Oct 24 '23 01:10 xmsz-stu

这个问题我也遇到了,在首次连接socket成功后,调用disconnect方法断开连接,然后调用connect方法重新连接。此时,onpen回调中的readyState为0。可能是该hook中的某个异步环节的处理出现了问题。

aioros2016 avatar Mar 06 '24 06:03 aioros2016

2022年就提出来了 到现在也没改
明显是个bug 状态还是0的时候就触发了 onOpen了

haobarry avatar Apr 10 '24 03:04 haobarry