2016-07-24 22 views
3

我一直試圖讓我的頭繞着Django的頻道,我無法讓我的消息被髮送到我的websocket。django頻道message.reply_channel沒有屬性發送

這裏是我的consumers.py

import logging 
from django.contrib.sites.models import Site 
from django.utils import timezone 
from channels import Group 
from .models import * 
import json 

def send_free(message): 
    try: 
     pi = PInformation.objects.get(
      pk=message.content.get('pk'), 
     ) 
    except Parkplatzinformationen.DoesNotExist: 
     logging.error("PI not found!") 
     return 

    try: 
     message.reply_channel.send({ 
     "text": 1, 
    }) 
    except: 
     logging.exception('Problem sending %s' % (pi.name)) 

routing.py

from channels.routing import route 

from RESTAPI.consumers import send_free 

channel_routing = [ 
    route('send-free',send_free), 
] 

,我發現了錯誤AttributeError: 'NoneType' object has no attribute 'send'。它確實獲得了PInformation對象,所以它確實工作了一點。我在保存對象後立即調用它。

你能給我一些提示嗎? The Getting Started guide就像我嘗試使用它。

回答

1

我假設你在呼喚從你的觀點是這樣"send-free" ...

Channel('send-free').send({'message': 'your message'}) 

然後send_free沒有message.reply_channel ...

換句話說,一旦WebSocket packet is sent to us by a client則消息採取reply_channel屬性。這將用於回覆消息給客戶...(對於前端也許)

那麼你真的想發送消息...?然後再次發送使用消費者...

+0

是的你是正確的我打電話發送免費的方式,你描述它。但是,我不明白重複這個過程的意思。 – dowu

+0

@dowu重複'Channel(「cons」)。send(dict)'並且我編輯了答案.. –

+0

好吧,我叫'Channel(「otherchanel」)。send(dict)'而不是'message.reply_channel。再次發送?是不是「otherchannel」應該被鏈接到一個函數,所以我堅持相同的問題? – dowu

相關問題