Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useWebSocket的onOpen问题 #1817

Open
songrunyu926 opened this issue Aug 16, 2022 · 11 comments
Open

useWebSocket的onOpen问题 #1817

songrunyu926 opened this issue Aug 16, 2022 · 11 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@songrunyu926
Copy link

songrunyu926 commented Aug 16, 2022

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

@crazylxr crazylxr added the needs more info Needs more information to continue label Aug 16, 2022
@github-actions
Copy link

Hi, songrunyu926.

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

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

@crazylxr
Copy link
Collaborator

有 demo 么?

@songrunyu926
Copy link
Author

songrunyu926 commented Aug 31, 2022

// 在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,
  })

@crazylxr
Copy link
Collaborator

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

@github-actions
Copy link

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 github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2022
@xmsz-stu
Copy link

我也遇到这个问题
image

image

@liuyib liuyib added bug Something isn't working and removed needs more info Needs more information to continue labels Oct 24, 2023
@liuyib liuyib reopened this Oct 24, 2023
@aioros2016
Copy link

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

@haobarry
Copy link

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

@joket1999
Copy link

joket1999 commented Sep 19, 2024

我也遇到这个问题,连接断开后重新连接,readyState 为1 ,但是报错:
chunk-VGGCA2L5.js?v=27dc857d:16670 Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

代码如下:

const ReadyState = {
    Connecting: 0,
    Open: 1,
    Closing: 2,
    Closed: 3
};
const { readyState, sendMessage, latestMessage } = useWebSocket(url);

useEffect(() => {
	switch (readyState) {
		// 连接成功
		case ReadyState.Open:
			sendMessage('data');
			break;
		default:
			break;
	}
}, [readyState, sendMessage]);

@YaoJunhaoya
Copy link

我在onOpen( webSocket 连接成功回调)里调用sendMessage 提示我没有连接成功 确实是有问题,到现在还没解决

@crazylxr crazylxr added the help wanted Extra attention is needed label Jan 16, 2025
Copy link

Hello @songrunyu926. We totally like your proposal/feedback, welcome to send us a Pull Request for it. Please send your Pull Request to proper branch (feature branch for the new feature, master for bugfix and other changes), fill the Pull Request Template here, provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. We appreciate your effort in advance and looking forward to your contribution!

你好 @songrunyu926,我们完全同意你的提议/反馈,欢迎直接在此仓库 创建一个 Pull Request 来解决这个问题。请将 Pull Request 发到正确的分支(新特性发到 feature 分支,其他发到 master 分支),务必填写 Pull Request 内的预设模板,提供改动所需相应的 changelog、TypeScript 定义、测试用例、文档等,并确保 CI 通过,我们会尽快进行 Review,提前感谢和期待您的贡献。

giphy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

9 participants