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


Golang WSContext.Conn方法代码示例

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


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

示例1: SubscribeGamePhase

func SubscribeGamePhase(c common.WSContext) error {
	base64DecodedId, err := base64.URLEncoding.DecodeString(c.Match()[1])
	if err != nil {
		return err
	}
	game := &Game{Id: base64DecodedId}
	if err = c.DB().Get(game); err != nil {
		return err
	}
	phaseOrdinal, err := strconv.Atoi(c.Match()[2])
	if err != nil {
		return err
	}
	members, err := game.Members(c.DB())
	if err != nil {
		return err
	}
	member := members.Get(c.Principal())
	isMember := member != nil
	if !game.Private || isMember {
		state, err := game.ToStateWithPhaseOrdinal(c.DB(), members, member, phaseOrdinal)
		if err != nil {
			return err
		}
		return websocket.JSON.Send(c.Conn(), gosubs.Message{
			Type: gosubs.FetchType,
			Object: &gosubs.Object{
				URI:  c.Match()[0],
				Data: state,
			},
		})
	}
	return nil
}
开发者ID:arlm,项目名称:diplicity,代码行数:34,代码来源:subscriptions.go

示例2: subscribeOthers

func subscribeOthers(c common.WSContext, filter kol.QFilter, preLimiter func(source Games) (result Games), postLimiter func(source GameStates) (result GameStates)) error {
	if c.Principal() == "" {
		return websocket.JSON.Send(c.Conn(), gosubs.Message{
			Type: gosubs.FetchType,
			Object: &gosubs.Object{
				URI: c.Match()[0],
			},
		})
	}
	s := c.Pack().New(c.Match()[0])
	s.Query = s.DB().Query().Where(filter)
	s.Call = func(i interface{}, op string) error {
		games := i.([]*Game)
		if preLimiter != nil {
			games = ([]*Game)(preLimiter(Games(games)))
		}
		states := GameStates{}
		isMember := false
		me := &user.User{Id: kol.Id(c.Principal())}
		if err := c.DB().Get(me); err != nil {
			return err
		}
		for _, game := range games {
			if !game.Disallows(me) {
				members, err := game.Members(c.DB())
				if err != nil {
					return err
				}
				if disallows, err := members.Disallows(c.DB(), me); err != nil {
					return err
				} else if !disallows {
					isMember = members.Contains(c.Principal())
					if !isMember {
						state, err := game.ToState(c.DB(), members, nil)
						if err != nil {
							return err
						}
						states = append(states, state)
					}
				}
			}
		}
		if op == gosubs.FetchType || len(states) > 0 {
			if postLimiter != nil {
				states = postLimiter(states)
			}
			return s.Send(states, op)
		}
		return nil
	}
	return s.Subscribe(&Game{})
}
开发者ID:arlm,项目名称:diplicity,代码行数:52,代码来源:subscriptions.go

示例3: SubscribeMine

func SubscribeMine(c common.WSContext) error {
	if c.Principal() == "" {
		return websocket.JSON.Send(c.Conn(), gosubs.Message{
			Type: gosubs.FetchType,
			Object: &gosubs.Object{
				URI: c.Match()[0],
			},
		})
	}
	s := c.Pack().New(c.Match()[0])
	s.Query = s.DB().Query().Where(kol.Equals{"UserId", kol.Id(c.Principal())})
	s.Call = func(i interface{}, op string) (err error) {
		members := i.([]*Member)
		var ep time.Duration
		ep, err = epoch.Get(c.DB())
		if err != nil {
			return
		}
		states := GameStates{}
		for _, member := range members {
			if op == gosubs.DeleteType {
				states = append(states, GameState{
					Game:    &Game{Id: member.GameId},
					Members: []MemberState{MemberState{Member: member}},
				})
			} else {
				game := &Game{Id: member.GameId}
				if err = s.DB().Get(game); err != nil {
					return
				}
				var gameMembers Members
				if gameMembers, err = game.Members(c.DB()); err != nil {
					return
				}
				var state GameState
				if state, err = game.ToState(c.DB(), gameMembers, member); err != nil {
					return
				}
				states = append(states, state)
			}
		}
		if op == gosubs.FetchType || len(states) > 0 {
			states = states.SortAndLimit(func(a, b GameState) bool {
				urgencyA := time.Hour * 24 * 365
				urgencyB := time.Hour * 24 * 365
				switch a.State {
				case common.GameStateStarted:
					_, phase, err := a.Game.Phase(c.DB(), 0)
					if err == nil {
						urgencyA = phase.Deadline - ep
					}
				case common.GameStateCreated:
					urgencyA -= 1
				}
				switch b.State {
				case common.GameStateStarted:
					_, phase, err := b.Game.Phase(c.DB(), 0)
					if err == nil {
						urgencyB = phase.Deadline - ep
					}
				case common.GameStateCreated:
					urgencyB -= 1
				}
				if urgencyA != urgencyB {
					return urgencyA < urgencyB
				}
				return a.CreatedAt.Before(b.CreatedAt)
			}, 1024*16)
			return s.Send(states, op)
		}
		return nil
	}
	return s.Subscribe(&Member{})
}
开发者ID:arlm,项目名称:diplicity,代码行数:74,代码来源:subscriptions.go


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