Golang Iris below the WebSocket

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.