Recently to do the background active push: (Iris frame, the package has wesocket, just began to think directly to use, the result is not now post code, write about the pits encountered)
Func Main () {
App: = Iris. New ()
App. Adapt (Iris. Devlogger ())
App. Adapt (Httprouter. New ())
Create a WebSocket server
WS: = WebSocket. New (WebSocket. config{
Endpoint: "/pushmessage",
})
Ws. OnConnection (func (c websocket). Connection) {
C.join ("/push")
Go func () {
for {
var datas = <-models. Chandata (here is the hole that has been encountered how to take out here from the man function, in fact, as long as the direct Yong Brother Chananel in the line, push the shop that success in a chananel back on it can be,)
StringData: = datas. (string)
Conns: = ws. Getconnectionsbyroom ("/push")
For _, V: = range Conns {//Traverse all links, send (here to traverse all links, otherwise the first push will be pushed to the first link, the second push will be pushed to the second link, not all of them will be pushed)
ERR: = V.emitmessage ([]byte (StringData))
If err! = Nil {
V.disconnect ()
}
}
}
}()
})
App. ADAPT (
Ws
Sessions. New (sessions. config{
Cookie: "Mysessionid",
expires:0,
COOKIELENGTH:32,
Disablesubdomainpersistence:false,
}),
View. HTML ("./src/views", ". html"),
Cors. New (cors. options{
Allowedorigins: []string{"*"},
Allowcredentials:true,
}),
)
There is still a connection on the WebSocket will receive the push, do not connect will not receive, even if the link is still not received, (the project is not required to do, did not do, back to do the fill up)
Golang Iris below the WebSocket