Skip to content

🐛 socketの接続・遮断前にlistenerを登録しないといけなかった #24

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

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions browser/websocket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ export function makeSocket() {
*/
export async function connect(socket: Socket): Promise<void> {
if (socket.connected) return;
socket.connect();

return await new Promise<void>((resolve) =>
const waiting = new Promise<void>((resolve) =>
socket.once("connect", () => resolve())
);
socket.connect();
await waiting;
}

/** websocketを切断する
Expand All @@ -25,14 +26,15 @@ export async function connect(socket: Socket): Promise<void> {
*/
export async function disconnect(socket: Socket): Promise<void> {
if (socket.disconnected) return;
socket.disconnect();

return await new Promise<void>((resolve) => {
const waiting = new Promise<void>((resolve) => {
const onDisconnect = (reason: Socket.DisconnectReason) => {
if (reason !== "io client disconnect") return;
resolve();
socket.off("disconnect", onDisconnect);
};
socket.on("disconnect", onDisconnect);
});
socket.disconnect();
await waiting;
}