douzao1119 2016-07-30 21:22
浏览 38

如何在Go中制作类似于Facebook中通知的通知系统

I want to make notification system in my project which is has chat module.To be more specific, when I send message receiver receive notification. I have already made notification system and its working. My problem is that when user open second tab my notification system doesn't work fluently.

There is my code

func GetNotification(w http.ResponseWriter, r *http.Request,id ,timestamp int) {
    LastMessageTime := -1
    conn, err := upgrader.Upgrade(w, r, nil)

    if err != nil {
        beego.Error(err)
        return
}


wakeupNotification(id)

for {
    select {

    case notificationTime := <-MessageNotification[strconv.Itoa(id)] :

        if notificationTime > LastMessageTime {
            LastMessageTime = notificationTime

        //MessageNotification[strconv.Itoa(id)] <-notificationTime

        chat_services,err := models.GetChatsNotifications(id,timestamp)

        if err != nil{
            return
        }

        not := notification{Status:true,MessageList:chat_services}

        conn.WriteJSON(not)
        }
    }
}


}


func SetNotification(id int,timeStamp int){
    go func(){
        wakeupNotification(id)
        MessageNotification[strconv.Itoa(id)] <- timeStamp

    }()
}

func wakeupNotification(id int){
    _,ok := MessageNotification[strconv.Itoa(id)]
    if !ok {
        MessageNotification[strconv.Itoa(id)] = make(chan int)
    }
}

func SleptNotification(id int){
    wakeupNotification(id)
    <-MessageNotification[strconv.Itoa(id)]
}

Also i have controller to trigger

    func (c *ChatController) Notification() {

    chat.GetNotification(c.Ctx.ResponseWriter,c.Ctx.Request,1,1)
}


func (c *ChatController) DeleteNotification(){

    chat.SleptNotification(1)

    c.Data["json"] = "readed"
    c.ServeJSON();


}

func (c *ChatController) Websocket(){

    //odalari olusturup mesajlasma bolumu yapilacak
    chat.SetNotification(1,int(time.Now().Unix()))

    c.Data["json"] = "test to trigger chan"
    c.ServeJSON();

}

My problem is notificationTime := <-MessageNotification[strconv.Itoa(id)] work one time and don't work again.So code never triggered again in browser second tab. Do you have any idea ?

Code work fluently. Sorry for English. Thanks for your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题