Uncaught TypeError: Illegal invocation
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- nextjs, react, typescript
调研方向
先从所提供的 Next.js 客户端复现中的 io() 调用和 connectMessageSocket 入口点开始,然后单独确认将 Socket 实例包装在 Valtio 代理中是否会导致调用错误。在 token 最初不可用的情况下复现经过身份验证的连接流程,并验证在提供 token 后能够成功连接。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
It produces Uncaught TypeError: Illegal invocation error when calling io()
context: I'm working in a react project where valtio is used for state manamement.
To Reproduce
Socket.IO server version: 4.8.1
Server
Using Nest.js for server. The server is working fine.
Socket.IO client version: 4.8.1
Client
// (next.js projec) the valtio store
'use client';
import {proxy, ref} from "valtio";
import {devtools, useProxy} from "valtio/utils";
import {IS_PRODUCTION_MODE, isServer} from "@/util/constants";
import {io, Socket} from "socket.io-client";
import {endpoints} from "@/util/urls";
type MsgModule<Actions = any> = {
socket: Socket;
connecting: boolean;
actions: Actions
}
type Data = {
message: MsgModule;
}
export const socketStore = proxy<Data>({
message: {
socket: io(endpoints.api.baseUrl + '/message', {
auth: {
token: ''
},
autoConnect: false
}),
connecting: false,
actions: ref({
abc: () => {}
}),
},
});
// socketStore.message.
export const useStoreSockMsg = () => useProxy(socketStore.message);
devtools(socketStore, {
name: 'socket',
enabled: !isServer && !IS_PRODUCTION_MODE,
});
export const socketActions = {
connectMessageSocket(accessToken: string) {
if (socketStore.message.socket.connected || socketStore.message.connecting) return;
console.log('proceed to socket connection with token', accessToken)
socketStore.message.socket = io(
endpoints.api.baseUrl + '/message',
{
auth: {
token: accessToken,
},
autoConnect: false,
}
);
// Listen for connection events
socketStore.message.socket.on('connect', () => {
console.log('socket is connected')
});
socketStore.message.socket.on('disconnect', () => {
console.log('socket is disconnected')
});
// Handle auth errors (e.g., expired token)
socketStore.message.socket.on('connect_error', async (err) => {
console.log('socket connection err: ', err.message)
// if (err.message === 'Unauthorized') {
// retry connection with updated token ...
// }
});
socketStore.message.socket.connect();
},
// Disconnect and cleanup
disconnectSocket: () => {
if (socketStore.message.socket) {
socketStore.message.socket.disconnect();
}
},
};
// the component
function MessageSocket() {
const userStore = useStoreUser();
const sockStore = useStoreSockMsg();
useEffect(() => {
if (sockStore.socket.connected || !userStore.at) return; // at is access token
socketActions.connectMessageSocket(userStore.at); // calling this producting the err: Uncaught TypeError: Illegal invocation
return () => socketActions.disconnectSocket();
}, [userStore.at, sockStore.socket.connected]);
useEffect(() => {
console.log('sock connected?', sockStore.socket.connected)
}, [sockStore.socket.connected]);
// ...
}
Expected behavior
Obviously expectation was to successfully connect, The official docs has a guide on how to use with react but that doesn't covers how to use with authentication when the token becomes available later.
Platform:
- Device: A Desktop PC
- OS: Ubuntu 24.04
- 主要语言
- TypeScript
- 星标
- 63.2k
- 派生
- 10.2k
- 平均合并
- 6 天 4 小时
- 30 天内合并 PR
- 6
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
socketio/socket.io 的其他 Issue
-
[engine.io-client] The Node.js cookie jar lets Expires override Max-Age可能已有人在做 @Ayush701-code-zm 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
socketio/socket.io#5590 · 1 条评论 ·
维护者通常 2 天内回复
-
Raise Engine.IO minimum to 6.6.10 for published security fixes可能已有人在做 @harshit-exe-dev 于 29 天前认领。 未关闭
难度 1/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
Can't set response headers in Server.handleUpgrade可能已有人在做 @MsfPablo 于 90 天前认领。 未关闭question
难度 2/5 1-3 小时 新手友好度 72/100
socketio/socket.io#5497 · 2 条评论 ·
维护者通常 2 天内回复
-
WebTransport ignores query parameters in URL可能已有人在做 @cyphercodes 于 178 天前认领。 未关闭needs major release question
难度 2/5 1-3 小时 新手友好度 68/100
socketio/socket.io#5496 · 3 条评论 ·
维护者通常 2 天内回复
-
[engine.io-client] close() called in an upgrade listener does not close the socket可能已有人在做 @adityajai119 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 20/100
维护者通常 2 天内回复
查看 socketio/socket.io 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 67/100
outline/outline#14529 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1-3 小时 新手友好度 86/100
g1331/phantom-circuit#93 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
compiler
难度 2/5 1-3 小时 新手友好度 74/100
HerringtonDarkholme/hd-lang#1699 ·
维护者通常 1 天内回复