-
Notifications
You must be signed in to change notification settings - Fork 0
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
PongのフロントエンドのNextでの実装 #23
Conversation
frontend/pages/pong/canvas.tsx
Outdated
interface Player { | ||
height: number; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
型を拡張しない場合は typeを使ってみてもいいかもしれません。
理由としてはinterfaceが簡単に型を拡張できてしまうので、予期せぬバグを生む可能性があるからです。
// こんな感じ
type Player = {
heght: number;
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!ここ、モヤモヤしてたところなので、参考リンク、勉強になりました。修正しておきます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryo-manba こちら、先程修正をプッシュしました。お手すきの際に確認よろしくお願いします。
// import Head from 'next/head'; | ||
// import Image from 'next/image'; | ||
// import styles from '../styles/Home.module.css'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー、これリンターに怒られるやつですよね。
pre-commit導入前に出したやつだったので、対応漏れてました。すみません。。
frontend/pages/pong/index.tsx
Outdated
@@ -0,0 +1,12 @@ | |||
import type { NextPage } from 'next'; | |||
import Canvas from './Canvas'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
小文字にしないとbuild時に怒られました。ファイル名と合わせる必要があるかもです
'./Canvas' -> './canvas';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、ファイル名の方を大文字スタートに変えたつもりだったのですが、変更漏れしてました。修正します。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryo-manba こちらも、先程修正をプッシュしました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
すいません、これ、なぜかまたファイル名の変更、反映されてませんでした。。。
index.tsxの方を修正してプッシュし直しました。
@ryo-manba 早速の確認、ありがとうございます!サーバーのログを見ると、サーバー立ち上げた途端に20近くのアクセスが3000番ポートに来ているようなので、VSCodeを再起動、またはMac自体を再起動してみて、もう一度試してもらえないでしょうか?それでもうまく動かなければ、ブラウザーのdeveloper toolのコンソールの画面のスクショもアップロードして頂けるとありがたいです。 |
@takashi247
スクショが悪かったです。何度かリロードしてたので、アクセスが多いように見えてます。 |
確認しました。 backend_nodejs: shogo@[22:18:12]:~/work/development/42Tokyo/ft_transcnendence/pong-frontend/backend_nodejs% yarn && node server.js Error: Cannot find module '/Users/shogo/work/development/42Tokyo/ft_transcnendence/pong-frontend/backend_nodejs/server.js' |
node server.jsで自分は動かせました。 |
@rakushoo 確認、ありがとうございます!
先程追加でプッシュした分がローカルにプルで来ていないのではないかと思います。
ログを見る限り、yarnは成功しているようなので、node server.jsだけで動くかやってみてもらえないでしょうか? |
barMove: (move: number) => void; | ||
}; | ||
|
||
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このtypeの引数っていままで知らなくて、統合して通信するデータの型が増えたときにそれを使うべきなのかわかってないです。使う理由はc++のインターフェースみたいにその実装を強制できるという点なんでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使う理由はc++のインターフェースみたいにその実装を強制できるという点なんでしょうか?
使う理由としては、私も同じ理解です。事前にemitとonで情報を送受信する型を指定しておくことで、フロントエンドとバックエンドのAPI的なものを決めて実装できる、というのが嬉しいポイントなのではないかと思っています。
私は公式ページを参考に実装したのですが、ここでも、サーバーサイドとクライアントサイドで同じ型を使うことで、実装のずれが起きないようにしてます。
なので、どこかのタイミングでフロントエンドとバックエンドで使う型を揃えて、別ファイルに定義して、フロントエンド、バックエンド双方で、その型定義をimportして使う、という感じにできると、TypeScriptの効能を最大限活かしながらSocket.ioを使っている感が出せるのではないかと、書いていて思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます、共通で定義できるのは確かに大きいですね。
socketやnext対応など参考になりました。
特に修正箇所化などは見当たりませんでしたが、こうらくさんも見ていらっしゃるのでマージは待っておきます |
@yuta-fujimoto 確認、ありがとうございます!
私も先程ゆうたさんのプルリクも一通り確認させて頂きました。まだGatewaysの理解が完全ではないですが、私もかなりスムーズにつなぎ合わせられそうな印象を持ちました。
という感じになるかな、と思っています。
同意です。Socket周りの型定義については先程コメントバックしましたが、一応、私の実装の方では丁寧に書いては見ましたが、今回のプロジェクトはそんな巨大プロジェクトでもないので、別にリンターに怒られないならいちいち型定義を書かなくてもいいかなぁ、とも思ってます。 |
backend_nodejs は起動できました。ありがとうございます。 frontend ですが、canvasの小文字大文字の件は解消しました。 shogo@[23:24:47]:~/work/development/42Tokyo/ft_transcnendence/pong-frontend2/frontend% yarn && yarn build && yarn start pong-frontend
See https://nextjs.org/docs/messages/page-without-valid-component for more info. error Command failed with exit code 1. |
追加情報ですが、エラーログの最後に参照しろ、とあった、 http://localhost:3000/ http://localhost:3000/game/home http://localhost:3000/pong |
@takashi247 つなげる形で大丈夫とのことでしたが一応、僕のほうは無駄に |
@yuta-fujimoto |
PC再起動を行い、backend側で、Palyerのログが出ない状態から始めて確認したところ、 |
概要
backend_nodjs
というディレクトリを作って、一旦 Node.js で簡易的に動くサーバーを作っています受入条件
下記の手順で、ローカルでもちゃんと動くか確認
frontend
ディレクトリでyarn && yarn build && yarn start
backend_nodejs
ディレクトリでyarn && node server.js
ゲームの描画の流れに関するざっくり解説
参考URL
備考
close #13