Как извлечь информацию из event.data в django каналах - PullRequest
2 голосов
/ 23 января 2020

Здравствуйте, у меня возникли проблемы при попытке извлечь и перенести данные из моего бэкэнда в мой интерфейс.

Вот мой код.

<script type="text/javascript">
var loc = window.location
var wsStart = 'ws://'
if (loc.protocol == 'https'){
  wsStart = 'wss://'
}
var endpoint = wsStart + loc.host + loc.pathname
var socket = new WebSocket(endpoint)
socket.onmessage = function(e){
  var x = e.data;
  console.log("message",e);
  badgeCreate();
  console.log(x.message);
  alert(x);

}
socket.onopen = function(e){  
  console.log("open",e)
  console.log(e.error)
}
socket.onerror = function(e){
  console.log("error",e)
}
socket.onclose = function(e){
  console.log("close",e)
}    
</script>

это важный бит моих потребителей. Py:

    async def user_notification (self, event):
        close_old_connections()
        print('user noti gets called')
        await self.send_json({
            'event': 'notification',
            'data': {
                'event_type': 'notification',
                'notification_pk': event['notification_pk'],
                'link': event['link'],
                'date_created': event['date_created'],
                'message': event['message'],
            }
        })
        print(event)

Это напечатано в моем окне предупреждения:

{"event": "notification", "data": {"event_type": "notification", "notification_pk": 64, "link": "test", "date_created": "2020-01-23 05:27:08", "message": "wqe"}}

Изменение x=e.data на x= e.data.message приводит к тому, что оно не определено. Кто-нибудь знает, что происходит?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...