Open7
socket.io chato app
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
index.jsにhtmlを書き続けるのは、大変なのでindex.htmlファイルを準備する
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
server側がjson形式でメッセージを受け取れるようにする
socket.emit('',inpu.value)
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
//client
socket.emit('name',"ここにメッセージを入れる")
//server
io.on('connection',(socket)=>{
socket.on('name,(arg)=>{
console.log(arg)
})
})
このように通信する
'name'のところは、clientとserverは同じにする。これをつかって双方向通信のやり取りをしている
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
コールバック関数も返せる
ステータスも返せる
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
disconnectionしているときのイベントは見逃される
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
clustor-adapter : server間のイベント転送を可能にする
![maiamitorio](https://res.cloudinary.com/zenn/image/fetch/s--1EEl8Ly5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/50fa0904e2.jpeg)
npm install @socket.io/cluster-adapter
だと、
+ npm install @socket.io/cluster-adapter
+ ~~~~~~~
分配演算子 '@' を使用して式で変数を参照することはできません。コマンドの引数として使用できるのは '@socket' だけです。式で変数を参照するには、'$socket' を使用してください
。
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : SplattingNotPermitted
が発生する
解決策
npm install "@socket.io/cluster-adapter"
""を作る