I am using Django 2.0.5, channels 2.1.1, aiohttp 3.1.3

two issues: (1) everything works for a while and then the channels 
websocket dies. Error is 'No handler for message type...'.  (2) the 
background `await t1` call appears to be blocking and it appears that any 
attempt to trigger closeConnection does not work. Would appreciate 
help/suggestions with these issues! Code below:

*consumer.py*:
rom channels.generic.websocket import AsyncWebsocketConsumer
import json


class myConsumer(AsyncWebsocketConsumer):

    async def connect(self):
        await self.channel_layer.group_add('myGroup', self.channel_name)
        await self.accept()

    async def disconnect(self, closeCode):
        await self.channel_layer.group_discard('myGroup', self.channel_name)

    async def receive(self, text_data):
        eventData = json.loads(text_data)
        if eventData['monitorToggle'] == 'on':
            print('on to background worker.')
            await self.channel_layer.send('toggle',{"type": 
"openConnection"})
        elif eventData['monitorToggle'] == 'off':
            print('off to background worker.')
            await self.channel_layer.send('toggle',{"type": 
"closeConnection"})

    async def myConsumerUpdate(self, data):
        await self.send(text_data=json.dumps(data['updateTime'))

*backgroundConsumer.py*
from channels.consumer import AsyncConsumer
from channels.layers import get_channel_layer
import asyncio
import aiohttp
import json
from mystuff.models import Info
import datetime


class myConsumer(AsyncConsumer):
    turnOnListener = True
    channelLayer = get_channel_layer()

    async def listen(self):
        infoList = Info.objects.filter(active=True)
        subInfoList = []
        for i in infoList:
            subInfoList.append(sometText'+str(i))
        async with aiohttp.ClientSession() as session:
            async with 
session.ws_connect('...streamerURL/socket.io/?EIO=3&transport=websocket', 
ssl=False) as ws:
                await ws.send_str(str(subInfoList))
                async for msg in ws:
                    if msg.type == aiohttp.WSMsgType.TEXT:
                        if msg.data.startswith('2'):
                            await ws.send_str('3')
                        else:
                            await self.parseListenData(msg)

    async def parseListenData(self, msg):
            # some code in here, including call to datetime to parse the msg
            updateMsg = {'type': 'myConsumerUpdate',
                         'updateTime': str(updateTime),
                         }
            print('built update msg and now sending to group')
            await self.channelLayer.group_send('marketsGroup', updateMsg)

    async def openConnection(self, event):
        if(self.turnOnListener):
            print('adding group')
            await self.channelLayer.group_add(
                'myGroup',
                self.channel_name,
            )
            print('turned on listener')
            self.turnOnListener = False
            loop = asyncio.get_event_loop()
            t1 = loop.create_task(self.listen())
            print('created t1')
            await t1
            print('awaiting t1') # Does not print until *consumer.py* 
crashes
        else:
            print('already turned on listener')

    async def closeConnection(self, event):
        print('in close connection') #Never gets here for some reason due 
to `await t1` blocking...
        if(not self.turnOnListener):
            print('turn it off')

-- 
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/ba76ddb6-eaad-4caf-8547-1473e15a6d9d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to