package controllers

import (
	"annual-lottery/utils"
	"net/http"

	"github.com/astaxie/beego"
)

var lpdata = utils.NewMsgQueue()

// LongPoll 长轮询
func (c *BaseController) LongPoll() {
	mth := c.Ctx.Input.Method()

	// id := c.GetString(":id")

	if mth == http.MethodGet {
		select {
		case dt := <-lpdata.Queue():
			beego.Info("发送状态数据: ", dt)
			c.ResponseJson(dt)
			return
		case <-c.Ctx.ResponseWriter.ResponseWriter.(http.CloseNotifier).CloseNotify():
			c.ResponseJson("nil")
			return
		}
	} else {
		msg := c.GetString("message")
		beego.Info("获取到状态数据: ", msg)
		go lpdata.Push(msg)
		c.ResponseJson("")
	}

	return
}
