0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RaspberryPiでwebsocket起動時に3セッション立ち上がる

Last updated at Posted at 2020-03-24

概要

簡単に言うと

  • Raspberry Pi 3 Model B+ 上で
  • Djangoでwebsocket通信する用にchannelsを導入したところ
  • server起動時に3セッション起動する問題が発生
  • 原因は起動時のIPを0.0.0.0にしていた

詳細

Macでは問題なかった

httpサーバ件websocketできるアプリケーションをpythonのDjangoで開発しており、手元のmacでは問題なく動いていました

こんな感じ

% python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 24, 2020 - 11:05:01
Django version 3.0.4, using settings 'mysite.settings'
Starting ASGI/Channels version 2.4.0 development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /ws [127.0.0.1:61445]
WebSocket CONNECT /ws [127.0.0.1:61445]

Raspberry Piで起動

Raspberry Pi上で起動するとこんな感じ

$ python manage.py runserver 0.0.0.0:3000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 24, 2020 - 11:07:05
Django version 3.0.4, using settings 'mysite.settings'
Starting ASGI/Channels version 2.4.0 development server at http://0.0.0.0:3000/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /ws [192.168.0.17:60036]
WebSocket HANDSHAKING /ws [127.0.0.1:42472]
WebSocket CONNECT /ws [192.168.0.17:60036]
WebSocket CONNECT /ws [127.0.0.1:42472]
WebSocket HANDSHAKING /ws [192.168.0.8:61638]
WebSocket CONNECT /ws [192.168.0.8:61638]

192.168.0.17 はRaspberryPiのIP

何故か3セッション起動する。
debugして情報を見るとこの3セットあった。

host client
192.168.0.17:3000 192.168.0.17:60036
192.168.0.17:3000 192.168.0.8:61638
0.0.0.0:3000 127.0.0.1:42472

192.168.0.170.0.0.0127.0.0.1 は分かるとして192.168.0.8 がどこから出てきたのか。。。

謎のセッションを確認

一応動くか見てみた

% wscat -c ws://192.168.0.8:3000/ws
Connected (press CTRL+C to quit)
>

動いた。。。(ヤメテ

解決方法

runserver で起動時に、 0.0.0.0 をやめて localshost にした

$ python manage.py runserver localhost:3000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 24, 2020 - 11:25:24
Django version 3.0.4, using settings 'mysite.settings'
Starting ASGI/Channels version 2.4.0 development server at http://localhost:3000/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /ws [127.0.0.1:43018]
WebSocket CONNECT /ws [127.0.0.1:43018]

127.0.0.1でも同じように解決した

結論

localhostのつもりで0.0.0.0指定するのは辞めよう
ここが参考になりました。127.0.0.1とlocalhostと0.0.0.0の違い

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?