23
29

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.

Django を WebSocket サーバにする

Last updated at Posted at 2018-02-02

Websocket サーバが立つと次のようなことが出来ます。

送信元クライアント
http://127.0.0.1:8000/sample/publish?msg=おはようございます。
websocket_feb0201.png

受信クライアント
wscat --connect http://127.0.0.1:8000/ws
websocket_feb0202.png

Arch Linux に wscat をインストールするには

yay -S wscat

Django の設定
参考にしたページ
Django で WebSocket によるサーバ Push
Getting Started with Channels

使用した Django のバージョン

>>> import django
>>> django.get_version()
'2.0.2'

channels のインストール

pip install django channels

Django のプロジェクトの作成

django-admin startproject ws
cd ws
python manage.py startapp sample

Django の動作確認

python manage.py runserver

ブラウザーで、http://127.0.0.1:8000/ にアクセスする
websocket_feb0203.png

Django の基本が動くことを確認したら、WebSocket サーバに改造する

ws/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
'sample',
'channels',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

APPEND_SLASH = False

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "sample.routing.channel_routing",
    },
ws/urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^sample/', include('sample.urls')),
    url(r'^admin/', admin.site.urls),
]
sample/consumers.py
from channels import Group

# WS が繋がってきたら sample というグループに参加させる
def ws_add(message):
    message.reply_channel.send({"accept": True})
    Group("sample").add(message.reply_channel)


# WS が切れたら sample というグループから外させる
def ws_disconnect(message):
    Group("sample").discard(message.reply_channel)
sample/routing.py
from channels.routing import route

# 先ほど作った sample/consumers.py から引っ張ってきてるだけ
from sample.consumers import ws_add, ws_disconnect

channel_routing = [
    # path を指定する 127.0.0.1:8000/ws に繋げるという感じ
    route("websocket.connect", ws_add, path = r'^/ws$'),
    route("websocket.disconnect", ws_disconnect),
]
sample/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    # 127.0.0.1:8000/sample/publish でメッセージを送れるようにする
    url(r'^publish$', views.publish, name='publish'),
]
sample/vies.py
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

from channels import Group

def publish(request):
    msg = request.GET.get('msg', 'null')
    Group("sample").send({"text": msg})

    return HttpResponse("Published!")

そしてサーバを実行

pyrhon manager.py runserver

受信の待機

wscat --connect http://127.0.0.1:8000/ws

ブラウザーでメッセージを送る

http://127.0.0.1:8000/sample/publish?msg=おはようございます。
23
29
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
23
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?