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

チャットルーム作成機能を実装した #29

Merged
merged 9 commits into from
Oct 27, 2022

Conversation

ryo-manba
Copy link
Owner

@ryo-manba ryo-manba commented Oct 25, 2022

概要

  • chatroomを作成できるようにした
  • とりあえず名前だけ受け付けるようにしている
  • 作成に成功したチャットルームをリストで表示している
  • chat.serviceをCRUD操作に対応させていますが、現状Createしか行いません。(今後使う予定です)

関連issue

その他

  • 細かい実装は別issueでやります

テスト方法

# docker起動
docker compose up -d

# migrateする
npx prisma migrate dev

# DBの状態を確認する
npx prisma studio

サーバーを起動して localhost:3000/chatにアクセス

demo

動画でprismaに登録されたidが13, 14なのは事前にテストしてたから

2022-10-25.16.45.47.mov

@ryo-manba ryo-manba self-assigned this Oct 25, 2022
@ryo-manba ryo-manba changed the title [WIP]チャットルーム作成機能を実装した チャットルーム作成機能を実装した Oct 26, 2022
@ryo-manba ryo-manba changed the title チャットルーム作成機能を実装した [WIP]チャットルーム作成機能を実装した Oct 26, 2022
@rakushoo
Copy link
Collaborator

確認しました。

別のマージによりconflictがあるようなので、その対応後にも確認しますが、front/backendを起動させたときの報告です。

backend:
yarn & yarn start:dev でサーバーは起動しました。

frontend:
細かいのですが、yarn && yarn build && yarn start では、下記のような socket.io-client のパスが見当たらないようなエラーが出ていました。yarn run dev では起動できてます。

shogo@[0:29:27]:~/work/development/42Tokyo/ft_transcnendence/add-create-chat-room/frontend% yarn && yarn build && yarn start
yarn install v1.22.19
$ typesync || :
/bin/sh: typesync: command not found
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning "@emotion/react > @emotion/babel-plugin@11.10.2" has unmet peer dependency "@babel/core@^7.0.0".
warning "@emotion/react > @emotion/babel-plugin > @babel/plugin-syntax-jsx@7.18.6" has unmet peer dependency "@babel/core@^7.0.0-0".
[4/4] 🔨 Building fresh packages...
$ cd .. && husky install .config/.husky
husky - Git hooks installed
✨ Done in 2.29s.
yarn run v1.22.19
$ next build
info - Linting and checking validity of types .warn - The Next.js plugin was not detected in your ESLint configuration. See https://nextjs.org/docs/basic-features/eslint#migrating-existing-config

Failed to compile.

./pages/chat/index.tsx
2:20 Error: Unable to resolve path to module 'socket.io-client'. import/no-unresolved
12:7 Error: Unsafe assignment of an any value. @typescript-eslint/no-unsafe-assignment
12:16 Error: Unsafe call of an any typed value. @typescript-eslint/no-unsafe-call
23:3 Error: Unsafe member access .emit on an any value. @typescript-eslint/no-unsafe-member-access
23:3 Error: Unsafe call of an any typed value. @typescript-eslint/no-unsafe-call
36:5 Error: Unsafe member access .on on an any value. @typescript-eslint/no-unsafe-member-access
36:5 Error: Unsafe call of an any typed value. @typescript-eslint/no-unsafe-call
42:7 Error: Unsafe member access .off on an any value. @typescript-eslint/no-unsafe-member-access
42:7 Error: Unsafe call of an any typed value. @typescript-eslint/no-unsafe-call

info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@ryo-manba ryo-manba changed the title [WIP]チャットルーム作成機能を実装した チャットルーム作成機能を実装した Oct 27, 2022
Copy link
Collaborator

@yuta-fujimoto yuta-fujimoto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動作に関してはきちんと確認できました!ありがとうございます。


@WebSocketServer() server: Server;

private logger: Logger = new Logger('ChatGateway');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これbackend全体で統一して使ったほうが見栄えよさそうですね

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに全体で統一してもいいかもですね。
でもnewするときに名前を指定できるから、モジュールごととかでもいいのかな。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

導入するかどうかを調査するissue作っときます

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ドキュメントで各サービスごとにインスタンス化することを推奨していたので、今の感じでいきましょう
#43

});

return () => {
socket.off('room:created');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これってつけてる理由を調べてみてどっちでも良さそうだな、と思ったんですが何かつけてる理由ありますか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実はあります。

開発環境だとstrict modeが効いているので、複数回レンダリングされます。
useEffectにsocket.onを書いても、レンダリングされた回数イベントハンドラーが適用されるので、
offがないと、本来1回しか呼ばれてほしくないイベントが複数呼ばれることになってしまいます。

具体的な例をあげると、チャットルームを作成するたび、同じものが2回listに追加されるという挙動になるはずです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考

the listeners must be removed in the cleanup step, in order to prevent multiple event registrations

https://socket.io/how-to/use-with-react-hooks

Copy link
Collaborator

@yuta-fujimoto yuta-fujimoto Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、理解しました。ありがとうございます!!

@rakushoo
Copy link
Collaborator

リポジトリを作成し直して、手順通りで、Chatroomの作成が確認できました。
ビルドが通らなかった根本の原因は不明です。。
プルリク自体の内容は問題ないと思いますのでマージします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

チャットルーム作成機能の実装
3 participants