打字稿:类型“套接字”上不存在属性“离开”

时间:2020-05-31 13:12:24

标签: reactjs typescript socket.io

我想用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}`);
}

该如何解决?

1 个答案:

答案 0 :(得分:1)

leaveon回调函数:

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