LoginSignup
2
5

More than 5 years have passed since last update.

websocketで複数通信時に起きた問題

Last updated at Posted at 2017-07-12

server.jsと複数のindex.htmlで通信

1人でもブラウザを閉じるとサーバーが閉じてしまう
(起動したnode server.jsが終了する)
→以後通信できなくなる。。。

解決方法

server.jsの自動再起動(永続化)

田中さん直伝
サーバー側にforeverを導入

$ npm install -g forever
$ forever server.jsで起動する

HTMLの自動リロード化

foreverと合わせて、ブラウザ側のindex.htmlにサーバーと通信が切れた時のイベントを設定

index.html
    ws.onclose = function () {
      console.log('closed');
      window.location.reload();
      };

こうすることで、

→だれかがブラウザを閉じる
→サーバーが切断される
→自動的にserver.jsを再起動
→切断されたのを検知したhtmlが再リロード

で無事に通信を続けることができる

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5