{"id":341383,"date":"2022-11-17T21:00:10","date_gmt":"2022-11-17T21:00:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=341383"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=341383","title":{"rendered":"<span>\u041a\u0430\u043a \u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c Django \u0438 Websockets<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043d\u0430 \u0443\u043c \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u0441\u043e\u043a\u0435\u0442\u044b. \u041d\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043c\u043d\u043e\u0439 \u0433\u0430\u0439\u0434\u043e\u0432 \u043f\u043e \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u0435, \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e, \u0438 \u0441 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0443 \u0438\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u0440\u043e\u0441\u0438\u0434\u0435\u0432 \u043f\u0430\u0440\u0443-\u0442\u0440\u043e\u0439\u043a\u0443 \u0447\u0430\u0441\u043e\u0432 \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u0430\u0437\u043b \u0438\u0437 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u0442\u0430\u0442\u0435\u0439 \u0441 \u0425\u0430\u0431\u0440\u0430 \u0438 \u043f\u0430\u0440\u044b \u0432\u0438\u0434\u0435\u043e\u0440\u043e\u043b\u0438\u043a\u043e\u0432 \u043e\u0442 \u043c\u043e\u0438\u0445 \u043a\u043e\u043b\u043b\u0435\u0433 \u0438\u0437 \u0418\u043d\u0434\u0438\u0438.  <\/p>\n<h2>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c Django,  \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0432\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u043f\u0438\u043b\u0438\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0441\u043e\u043a\u0435\u0442\u044b.  <\/p>\n<h4>Python<\/h4>\n<blockquote>\n<p><em>pip install channels<\/em><\/p>\n<\/blockquote>\n<blockquote>\n<p><em>pip install channels-redis<\/em><\/p>\n<\/blockquote>\n<blockquote>\n<p><em>pip install daphne<\/em><\/p>\n<\/blockquote>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 daphne \u043f\u0438\u043f \u0432\u044b\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u043e C++, \u0442\u043e <a href=\"https:\/\/visualstudio.microsoft.com\/ru\/visual-cpp-build-tools\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c BuildTools<\/a> \u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c &#171;\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 C++&#187;. \u0420\u0435\u0431\u0443\u0442\u0430\u0435\u043c \u043a\u043e\u043c\u043f \u0438 \u0441\u043d\u043e\u0432\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c pip install.  <\/p>\n<h4>\u0428\u0430\u0433 \u21161<\/h4>\n<p>\u0412 \u0444\u0430\u0439\u043b <code>settings.py<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b(\u041f\u0440\u0438\u0447\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 &#8212; daphne \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0441\u043f\u0438\u0441\u043a\u0430):<\/p>\n<pre><code class=\"python\">INSTALLED_APPS = [   'daphne',   'channels',   ... ]<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0436\u0435 \u0444\u0430\u0439\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c:<\/p>\n<pre><code class=\"python\">WSGI_APPLICATION = 'yourapp.wsgi.application'<\/code><\/pre>\n<p>\u043d\u0430:<\/p>\n<pre><code class=\"python\">ASGI_APPLICATION = 'yourapp.asgi.application'<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 Channel layers \u0432 <code>settings.py<\/code>, \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0433\u0434\u0435 \u0443 \u0432\u0430\u0441 \u0441\u0442\u043e\u0438\u0442 Redis, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 <a href=\"https:\/\/channels.readthedocs.io\/en\/stable\/topics\/channel_layers.html?highlight=redis#redis-channel-layer\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>. \u0414\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0442\u0430\u043a\u043e\u0439 \u0441\u0435\u0442\u0430\u043f:<\/p>\n<pre><code class=\"python\">CHANNELS_LAYERS = {   'default': {     'BACKEND': 'channels.layers.InMemoryChannelLayer'   } } <\/code><\/pre>\n<h4>\u0428\u0430\u0433 \u21162<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b <code>consumers.py<\/code> \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0441 Class Based Views \u0432 Django \u0442\u043e Consumers \u0432\u0430\u043c \u043f\u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a:<\/p>\n<pre><code class=\"python\">from channels.consumer import AsyncConsumer   YourConsumer(AsyncConsumer):      async def websocket_connect(self, event):         await self.send({\"type\": \"websocket.accept\"})      async def websocket_recieve(self, text_data):         await self.send({             \"type\": \"websocket.send\",             \"text\": \"Hello from Django socket\"         })      async def websocket_disconnect(self, event):         pass<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 <code>YourConsumer<\/code> \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0441\u0432\u043e\u0439 \u0442\u0438\u043f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0441\u043e\u043a\u0435\u0442\u044b. \u041e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 CBV<\/p>\n<h4>\u0428\u0430\u0433 \u21163<\/h4>\n<p>\u0418\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u0430 <code>asgi.py<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"python\">from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter from channels.routing import URLRouter  from django.core.asgi import get_asgi_application from django.urls import path   import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourapp.settings')   from yourapp.consumers import YourConsumer   django_asgi_app = get_asgi_application()  application = ProtocolTypeRouter({     'http': django_asgi_app,     'websocket': AuthMiddlewareStack(         URLRouter([             path('ws', YourConsumer.as_asgi())         ])     ) })<\/code><\/pre>\n<p>\u0412 <code>URLRouter<\/code> \u043c\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043f\u0443\u0442\u044c, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d Consumer. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u043b\u044f <strong>127.0.0.1<\/strong> \u044d\u0442\u043e &#8212; <strong>ws:\/\/127.0.0.1:8000\/ws<\/strong>. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e <code>urls.py<\/code>, \u043a\u0441\u0442\u0430\u0442\u0438 \u043f\u0440\u0438 \u0436\u0435\u043b\u0430\u043d\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432\u0441\u0435 \u043f\u0443\u0442\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <code>routers.py<\/code> \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e\u0442\u043e\u043c \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e.<\/p>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c<\/h4>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043b\u043e\u043a\u0430\u043b\u043a\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u043e\u043a\u0435\u0442\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0447\u0435\u0440\u0435\u0437 Js:<\/p>\n<pre><code class=\"javascript\">const socket = new WebSocket('ws:\/\/127.0.0.1:8000\/ws');  socket.onopen = function(e) {   socket.send(JSON.stringify({     message: 'Hello from Js client'   })); };  socket.onmessage = function(event) {   try {     console.log(event);   } catch (e) {     console.log('Error:', e.message);   } };<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6d3\/fa9\/f8b\/6d3fa9f8b2bf066a0b1b657f2f3b875e.png\" alt=\"\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u0438\u0434\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b\u0438 \u0432 consumers.py\" title=\"\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u0438\u0434\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b\u0438 \u0432 consumers.py\" width=\"909\" height=\"366\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6d3\/fa9\/f8b\/6d3fa9f8b2bf066a0b1b657f2f3b875e.png\"\/><figcaption>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u0438\u0434\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b\u0438 \u0432 consumers.py<\/figcaption><\/figure>\n<h4>\u0421\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p><a href=\"https:\/\/channels.readthedocs.io\/en\/stable\/\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/django\/channels\" rel=\"noopener noreferrer nofollow\">\u0413\u0438\u0442\u0445\u0430\u0431<\/a><\/p>\n<p><a href=\"https:\/\/t.me\/sssilencer\" rel=\"noopener noreferrer nofollow\">\u041c\u043e\u0439 \u0442\u0433<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/700100\/\"> https:\/\/habr.com\/ru\/post\/700100\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043d\u0430 \u0443\u043c \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u0441\u043e\u043a\u0435\u0442\u044b. \u041d\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043c\u043d\u043e\u0439 \u0433\u0430\u0439\u0434\u043e\u0432 \u043f\u043e \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u0435, \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e, \u0438 \u0441 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0443 \u0438\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u0440\u043e\u0441\u0438\u0434\u0435\u0432 \u043f\u0430\u0440\u0443-\u0442\u0440\u043e\u0439\u043a\u0443 \u0447\u0430\u0441\u043e\u0432 \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u0430\u0437\u043b \u0438\u0437 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u0442\u0430\u0442\u0435\u0439 \u0441 \u0425\u0430\u0431\u0440\u0430 \u0438 \u043f\u0430\u0440\u044b \u0432\u0438\u0434\u0435\u043e\u0440\u043e\u043b\u0438\u043a\u043e\u0432 \u043e\u0442 \u043c\u043e\u0438\u0445 \u043a\u043e\u043b\u043b\u0435\u0433 \u0438\u0437 \u0418\u043d\u0434\u0438\u0438.  <\/p>\n<h2>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c Django,  \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0432\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u043f\u0438\u043b\u0438\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0441\u043e\u043a\u0435\u0442\u044b.  <\/p>\n<h4>Python<\/h4>\n<blockquote>\n<p><em>pip install channels<\/em><\/p>\n<\/blockquote>\n<blockquote>\n<p><em>pip install channels-redis<\/em><\/p>\n<\/blockquote>\n<blockquote>\n<p><em>pip install daphne<\/em><\/p>\n<\/blockquote>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 daphne \u043f\u0438\u043f \u0432\u044b\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u043e C++, \u0442\u043e <a href=\"https:\/\/visualstudio.microsoft.com\/ru\/visual-cpp-build-tools\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c BuildTools<\/a> \u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c &#171;\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 C++&#187;. \u0420\u0435\u0431\u0443\u0442\u0430\u0435\u043c \u043a\u043e\u043c\u043f \u0438 \u0441\u043d\u043e\u0432\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c pip install.  <\/p>\n<h4>\u0428\u0430\u0433 \u21161<\/h4>\n<p>\u0412 \u0444\u0430\u0439\u043b <code>settings.py<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b(\u041f\u0440\u0438\u0447\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 &#8212; daphne \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0441\u043f\u0438\u0441\u043a\u0430):<\/p>\n<pre><code class=\"python\">INSTALLED_APPS = [   'daphne',   'channels',   ... ]<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0436\u0435 \u0444\u0430\u0439\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c:<\/p>\n<pre><code class=\"python\">WSGI_APPLICATION = 'yourapp.wsgi.application'<\/code><\/pre>\n<p>\u043d\u0430:<\/p>\n<pre><code class=\"python\">ASGI_APPLICATION = 'yourapp.asgi.application'<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 Channel layers \u0432 <code>settings.py<\/code>, \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0433\u0434\u0435 \u0443 \u0432\u0430\u0441 \u0441\u0442\u043e\u0438\u0442 Redis, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 <a href=\"https:\/\/channels.readthedocs.io\/en\/stable\/topics\/channel_layers.html?highlight=redis#redis-channel-layer\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>. \u0414\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0442\u0430\u043a\u043e\u0439 \u0441\u0435\u0442\u0430\u043f:<\/p>\n<pre><code class=\"python\">CHANNELS_LAYERS = {   'default': {     'BACKEND': 'channels.layers.InMemoryChannelLayer'   } } <\/code><\/pre>\n<h4>\u0428\u0430\u0433 \u21162<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b <code>consumers.py<\/code> \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0441 Class Based Views \u0432 Django \u0442\u043e Consumers \u0432\u0430\u043c \u043f\u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a:<\/p>\n<pre><code class=\"python\">from channels.consumer import AsyncConsumer   YourConsumer(AsyncConsumer):      async def websocket_connect(self, event):         await self.send({\"type\": \"websocket.accept\"})      async def websocket_recieve(self, text_data):         await self.send({             \"type\": \"websocket.send\",             \"text\": \"Hello from Django socket\"         })      async def websocket_disconnect(self, event):         pass<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 <code>YourConsumer<\/code> \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0441\u0432\u043e\u0439 \u0442\u0438\u043f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0441\u043e\u043a\u0435\u0442\u044b. \u041e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 CBV<\/p>\n<h4>\u0428\u0430\u0433 \u21163<\/h4>\n<p>\u0418\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u0430 <code>asgi.py<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"python\">from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter from channels.routing import URLRouter  from django.core.asgi import get_asgi_application from django.urls import path   import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourapp.settings')   from yourapp.consumers import YourConsumer   django_asgi_app = get_asgi_application()  application = ProtocolTypeRouter({     'http': django_asgi_app,     'websocket': AuthMiddlewareStack(         URLRouter([             path('ws', YourConsumer.as_asgi())         ])     ) })<\/code><\/pre>\n<p>\u0412 <code>URLRouter<\/code> \u043c\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043f\u0443\u0442\u044c, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d Consumer. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u043b\u044f <strong>127.0.0.1<\/strong> \u044d\u0442\u043e &#8212; <strong>ws:\/\/127.0.0.1:8000\/ws<\/strong>. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e <code>urls.py<\/code>, \u043a\u0441\u0442\u0430\u0442\u0438 \u043f\u0440\u0438 \u0436\u0435\u043b\u0430\u043d\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432\u0441\u0435 \u043f\u0443\u0442\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <code>routers.py<\/code> \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e\u0442\u043e\u043c \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e.<\/p>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c<\/h4>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043b\u043e\u043a\u0430\u043b\u043a\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u043e\u043a\u0435\u0442\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0447\u0435\u0440\u0435\u0437 Js:<\/p>\n<pre><code class=\"javascript\">const socket = new WebSocket('ws:\/\/127.0.0.1:8000\/ws');  socket.onopen = function(e) {   socket.send(JSON.stringify({     message: 'Hello from Js client'   })); };  socket.onmessage = function(event) {   try {     console.log(event);   } catch (e) {     console.log('Error:', e.message);   } };<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u0438\u0434\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b\u0438 \u0432 consumers.py<\/figcaption><\/figure>\n<h4>\u0421\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p><a href=\"https:\/\/channels.readthedocs.io\/en\/stable\/\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/django\/channels\" rel=\"noopener noreferrer nofollow\">\u0413\u0438\u0442\u0445\u0430\u0431<\/a><\/p>\n<p><a href=\"https:\/\/t.me\/sssilencer\" rel=\"noopener noreferrer nofollow\">\u041c\u043e\u0439 \u0442\u0433<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/700100\/\"> https:\/\/habr.com\/ru\/post\/700100\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-341383","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341383"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341383\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}