django channels Connection Errors only when tried with User_login

137 views
Skip to first unread message

왕형준

unread,
Nov 7, 2018, 2:46:29 PM11/7/18
to Django users

Running channels with daphne.

django version: 2.0.4

asgiref==2.2.0

asgi-redis==1.4.3

channels==2.1.5

channels-redis==2.3.1

I'm trying to connect websoket. When I tried to connect with Anonymous user(without user login), it works well. But, when there is user, it doesn't work well.

The one point that I get by using pdb.set_trace() is that there is self.scope['cookies']['sessionid'] when I tried with user login. But there is no result in self.scope['cookies']['sessionid'] when there is no user(Anonymous user)

I thought that it might be related to AuthMiddlewareStack and I looked into the source code. But, I cannot find the source of the problem.

Can you help me? Below are my codes.

  1. settings.py

    
    ASGI_APPLICATION = 'businessproject.routing.application'

    CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': { "hosts": [('127.0.0.1', 6379)], }, }, }

  2. models.py


class MyUser(AbstractUser):
    galaxy_num=models.IntegerField(default=1)
    onoff=models.IntegerField(default=1, null=True)

def __str__(self):
    return self.username

  1. consumers.py


from channels.generic.websocket import WebsocketConsumer, AsyncWebsocketConsumer
import json, pdb
from asgiref.sync import async_to_sync

class TestConsumer(AsyncWebsocketConsumer):

async def connect(self):
    # Join room group
    self.group_name="likes"

    await self.channel_layer.group_add(
        self.group_name,
        self.channel_name
    )

    await self.accept()

async def disconnect(self, close_code):

    await self.channel_layer.group_discard(
        self.group_name,
        self.channel_name
    )

async  def receive(self, text_data):
    text_data_json = json.loads(text_data)
    message = text_data_json['message']

    await self.channel_layer.group_send(
        self.group_name,
        {
            'type': 'like_message',
            'message': message
        }
    )

# Receive message from room group
async def like_message(self, event):
    message = "%s님이 게시물을 좋아합니다."%event['message']

    # Send message to WebSocket
    await self.send(text_data=json.dumps({
        'message': message
    }))

  1. routing.py

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from cebula import routing


application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            routing.websocket_urlpatterns
        )
    ),
})

this is the console result

WebSocket connection to ws://127.0.0.1:8000/ws/test/

failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

If you need more information, please tell me.

Reply all
Reply to author
Forward
0 new messages