我想用socket.leave(“ roomname”)在socket.io中留一个房间。打字稿声称
“类型“套接字”上不存在属性“离开””
。
这是我的代码:
import io from "socket.io-client";
// leave chat
export async function leaveChat(conversationId){
const socket: SocketIOClient.Socket = io(process.env.SERVER_URL);
socket.leave(`chat@${conversationId}`);
}
该如何解决?
答案 0 :(得分:1)
leave
是on
回调函数:
import io from "socket.io-client";
export async function leaveChat(conversationId){
const socket: SocketIOClient.Socket = io(process.env.SERVER_URL);
socket.on('connection', s => {
s.leave(`chat@${conversationId}`);
});
}
您可以查看有关文档:https://socket.io/docs/server-api/#socket-leave-room-callback