Golongpoll 是 golang HTTP 的 longpolling 库,可以使构建 web pub-sub 更加容易。
基本用法:
<pre style="box-sizing: border-box; overflow: auto; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-stretch: normal; line-height: 1.45; padding: 16px; border-radius: 3px; word-wrap: normal; word-break: normal; color: rgb(51, 51, 51); background-color: rgb(247, 247, 247);" class="brush:cpp ;toolbar: true; auto-links: false;">import "github.com/jcuga/golongpoll"
// This launches a goroutine and creates channels for all the plumbing
manager, err := golongpoll.CreateManager()
// Expose events to browsers
// See subsection on how to interact with the subscription handler
http.HandleFunc("/events", manager.SubscriptionHandler)
http.ListenAndServe("127.0.0.1:8081", nil)
// Pass the manager around or create closures and publish:
manager.Publish("subscription-category", "Some data. Can be string or any obj convertable to JSON")
manager.Publish("different-category", "More data")</pre>