代码之家  ›  专栏  ›  技术社区  ›  William

使用带EC2的Django通道时出现Websocket错误

  •  1
  • William  · 技术社区  · 7 年前

    我正在尝试在Django中构建一个使用Django频道的应用程序。我正在部署弹性豆茎。我的理解是,应用程序负载平衡器支持websocket,我可以将websocket流量路由到适当的端口。websocket在我的本地主机8000上工作。我正在使用免费层Redis实验室作为通道层。

    我遵循了这个教程。 https://blog.mangoforbreakfast.com/2017/02/13/django-channels-on-aws-elastic-beanstalk-using-an-alb/#comment-43

    $ sudo /usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf status
    Daphne                           RUNNING   pid 4240, uptime 0:05:51
    Worker:Worker_00                 RUNNING   pid 4242, uptime 0:05:51
    Worker:Worker_01                 RUNNING   pid 4243, uptime 0:05:51
    Worker:Worker_02                 RUNNING   pid 4244, uptime 0:05:51
    Worker:Worker_03                 RUNNING   pid 4245, uptime 0:05:51
    httpd                            RUNNING   pid 4248, uptime 0:05:51
    

    我的daphne.out。日志和workers.out。原木看起来不错。

    $ cat daphne.out.log 
    2017-07-20 21:41:43,693 INFO     Starting server at tcp:port=5000:interface=0.0.0.0, channel layer mysite.asgi:channel_layer.
    2017-07-20 21:41:43,693 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
    2017-07-20 21:41:43,694 INFO     Using busy-loop synchronous mode on channel layer
    2017-07-20 21:41:43,694 INFO     Listening on endpoint tcp:port=5000:interface=0.0.0.0
    
    $ cat workers.out.log 
    2017-07-20 21:41:44,114 - INFO - runworker - Using single-threaded worker.
    2017-07-20 21:41:44,120 - INFO - runworker - Using single-threaded worker.
    2017-07-20 21:41:44,121 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
    2017-07-20 21:41:44,121 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
    2017-07-20 21:41:44,126 - INFO - runworker - Using single-threaded worker.
    2017-07-20 21:41:44,126 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
    2017-07-20 21:41:44,126 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
    2017-07-20 21:41:44,127 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
    2017-07-20 21:41:44,127 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
    2017-07-20 21:41:44,133 - INFO - runworker - Using single-threaded worker.
    2017-07-20 21:41:44,136 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
    2017-07-20 21:41:44,136 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
    

    从这里,我更新了安全组设置,配置了负载平衡器,并测试了我的网页。我使用了/table/*而不是/ws/作为重新路由的路径,因为这些页面需要WebSocket。

    到“ws://示例”的WebSocket连接。com/table/1/'失败:WebSocket握手期间出错:意外响应代码:404

    如果我使用/table/*我会得到这个错误。

    ...
    2017-07-20 21:41:44,136 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
    Not Found: /ws/
    Not Found: /ws/
    Not Found: /ws/
    Not Found: /ws/
    Not Found: /ws/
    Not Found: /ws/
    

    如果我使用/table/*路径,我的日志中会出现很长的错误。

    2017-07-21 01:22:05,270 - ERROR - worker - Error processing message with consumer deal.consumers.ws_connect:
    Traceback (most recent call last):
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 193, in _get_session
        return self._session_cache
    AttributeError: 'SessionStore' object has no attribute '_session_cache'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
        self.connect()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
        self.connection = self.get_new_connection(conn_params)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
        connection = Database.connect(**conn_params)
      File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
        conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
    
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/channels/worker.py", line 119, in run
        consumer(message, **kwargs)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/channels/sessions.py", line 220, in inner
        result = func(message, *args, **kwargs)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/channels/auth.py", line 71, in inner
        message.user = auth.get_user(fake_request)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 167, in get_user
        user_id = _get_user_session_key(request)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 59, in _get_user_session_key
        return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 48, in __getitem__
        return self._session[key]
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 198, in _get_session
        self._session_cache = self.load()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/db.py", line 33, in load
        expire_date__gt=timezone.now()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 381, in get
        num = len(clone)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 240, in __len__
        self._fetch_all()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 1074, in _fetch_all
        self._result_cache = list(self.iterator())
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 52, in __iter__
        results = compiler.execute_sql()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
        cursor = self.connection.cursor()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 231, in cursor
        cursor = self.make_debug_cursor(self._cursor())
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 204, in _cursor
        self.ensure_connection()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
        self.connect()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
        self.connect()
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
        self.connection = self.get_new_connection(conn_params)
      File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
        connection = Database.connect(**conn_params)
      File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
        conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
    

    不出所料,当我尝试使用WebSocket时,会出现以下错误:

    WebSocket已处于关闭或关闭状态。

    知道我做错了什么吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   William    7 年前

    [program:Daphne]
    environment=PATH="/opt/python/run/venv/bin"
    **command=/bin/bash -c "source /opt/python/current/env && /opt/python/run/venv/bin/daphne -b 0.0.0.0 -p 5000 mysite.asgi:channel_layer"**
    directory=/opt/python/current/app
    autostart=true
    autorestart=true
    redirect_stderr=true
    stdout_logfile=/tmp/daphne.out.log
    
    [program:Worker]
    environment=PATH="/opt/python/run/venv/bin"
    **command=/bin/bash -c "source /opt/python/current/env && python manage.py runworker"**
    directory=/opt/python/current/app
    process_name=%(program_name)s_%(process_num)02d
    numprocs=4
    autostart=true
    autorestart=true
    redirect_stderr=true
    stdout_logfile=/tmp/workers.out.log