上周刚好遇到这个问题。
周末在家里按网上的方案测试了一下。
希望下周进展顺利~~
URL:
http://blog.csdn.net/u012210379/article/details/72912056
package controllers import ( "beegows/models" "fmt" "log" "time" "github.com/astaxie/beego" // "github.com/astaxie/beego/toolbox" "github.com/gorilla/websocket" ) type MyWebSocketController struct { beego.Controller } var upgrader = websocket.Upgrader{} func (c *MyWebSocketController) Get() { ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil) if err != nil { log.Fatal(err) } //defer ws.Close() clients[ws] = true /* for { time.Sleep(time.Second * 3) msg := models.Message{Message: "这是向页面发送的数据" + time.Now().Format("2006-01-02 15:04:05")} broadcast <- msg } */ for { time.Sleep(time.Second * 3) var msg models.Message // Read in a new message as JSON and map it to a Message object err := ws.ReadJSON(&msg) if err != nil { log.Printf("页面可能断开啦 ws.ReadJSON error: %v", err) delete(clients, ws) break } else { fmt.Println("接受到从页面上反馈回来的信息 ", msg.Message) } broadcast <- msg } }
时间: 2024-10-22 15:16:44