【发布时间】:2016-07-26 23:46:00
【问题描述】:
首先,我想做长轮询通知系统。具体来说,我会发起http请求,只有map channel是true才会返回response。
这是我使用的代码块:
var MessageNotification = make(map[string]chan bool, 10)
func GetNotification(id int, timestamp int) notification {
<-MessageNotification["1"]
var chat_services []*models.Chat_service
o := orm.NewOrm()
_, err := o.QueryTable("chat_service").Filter("Sender__id", id).RelatedSel().All(&chat_services)
if err != nil {
return notification{Status: false}
}
return notification{Status: true, MessageList: chat_services}
}
func SetNotification(id int) {
MessageNotification[strconv.Itoa(id)] <- true
}
这是控制器块:
func (c *ChatController) Notification() {
data := chat.GetNotification(1,0)
c.Data["json"] = data
c.ServeJSON()
}
func (c *ChatController) Websocket(){
chat.SetNotification(1)
c.Data["json"] = "test"
c.ServeJSON();
}
为测试创建的函数名称和变量。
没有发生错误。感谢您的帮助。
【问题讨论】:
-
你怎么打电话给
SetNotification?您的代码中没有循环,因此它可能是一个阻塞通道。你能再分享一些代码吗? -
我添加了代码。你认为我需要循环吗?
<-MessageNotification["1"]代码会让程序等待还是我错了? -
如何调用
Notification和Websocket函数? -
这些是控制器。我根据url使用beego框架和beego回调控制器函数。(例如localhost:8080/chatinfo)
-
您应该将beego标签添加到您的问题中。知道的人可能会提供帮助。