当前位置: 首页>>代码示例>>Golang>>正文


Golang LongpollManager.SubscriptionHandler方法代码示例

本文整理汇总了Golang中github.com/jcuga/golongpoll.LongpollManager.SubscriptionHandler方法的典型用法代码示例。如果您正苦于以下问题:Golang LongpollManager.SubscriptionHandler方法的具体用法?Golang LongpollManager.SubscriptionHandler怎么用?Golang LongpollManager.SubscriptionHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jcuga/golongpoll.LongpollManager的用法示例。


在下文中一共展示了LongpollManager.SubscriptionHandler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getEventSubscriptionHandler

// Creates a closure function that is used as an http handler for browsers to
// subscribe to events via longpolling.
// Notice how we're wrapping LongpollManager.SubscriptionHandler in order to
// add our own logic and validation.
func getEventSubscriptionHandler(manager *golongpoll.LongpollManager) func(w http.ResponseWriter, r *http.Request) {
	// Creates closure that captures the LongpollManager
	// Wraps the manager.SubscriptionHandler with a layer of dummy access control validation
	return func(w http.ResponseWriter, r *http.Request) {
		category := r.URL.Query().Get("category")
		user := r.URL.Query().Get("user")
		// NOTE: real user authentication should be used in the real world!

		// Dummy user access control in the event the client is requesting
		// a user's private activity stream:
		if category == "larry_actions" && user != "larry" {
			w.WriteHeader(http.StatusForbidden)
			w.Write([]byte("You're not Larry."))
			return
		}
		if category == "moe_actions" && user != "moe" {
			w.WriteHeader(http.StatusForbidden)
			w.Write([]byte("You're not Moe."))
			return
		}
		if category == "curly_actions" && user != "curly" {
			w.WriteHeader(http.StatusForbidden)
			w.Write([]byte("You're not Curly."))
			return
		}

		// Only allow supported subscription categories:
		if category != "public_actions" && category != "larry_actions" &&
			category != "moe_actions" && category != "curly_actions" {
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Subscription channel does not exist."))
			return
		}

		// Client is either requesting the public stream, or a private
		// stream that they're allowed to see.
		// Go ahead and let the subscription happen:
		manager.SubscriptionHandler(w, r)
	}
}
开发者ID:jcuga,项目名称:golongpoll,代码行数:44,代码来源:advanced.go


注:本文中的github.com/jcuga/golongpoll.LongpollManager.SubscriptionHandler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。