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, 
AsyncWebsocketConsumerimport json, pdbfrom 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 AuthMiddlewareStackfrom channels.routing import 
ProtocolTypeRouter, URLRouterfrom 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c48106b2-ba65-4664-8634-eafd9116ab5c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to