• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang proto.PresenceEvent函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中euphoria/io/heim/proto.PresenceEvent函数的典型用法代码示例。如果您正苦于以下问题:Golang PresenceEvent函数的具体用法?Golang PresenceEvent怎么用?Golang PresenceEvent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: part

func (b *Backend) part(ctx scope.Context, rb *RoomBinding, session proto.Session) error {
	t, err := b.DbMap.Begin()
	if err != nil {
		return err
	}

	_, err = t.Exec(
		"DELETE FROM presence WHERE room = $1 AND server_id = $2 AND server_era = $3 AND session_id = $4",
		rb.RoomName, b.desc.ID, b.desc.Era, session.ID())
	if err != nil {
		rollback(ctx, t)
		logging.Logger(ctx).Printf("failed to persist departure: %s", err)
		return err
	}

	// Broadcast a presence event.
	// TODO: make this an explicit action via the Room protocol, to support encryption
	event := proto.PresenceEvent(session.View(proto.Staff))
	if err := rb.broadcast(ctx, t, proto.PartEventType, event, session); err != nil {
		rollback(ctx, t)
		return err
	}

	if err := t.Commit(); err != nil {
		return err
	}

	b.Lock()
	if lm, ok := b.listeners[rb.RoomName]; ok {
		delete(lm, session.ID())
	}
	b.Unlock()

	return nil
}
开发者ID:logan,项目名称:heim,代码行数:35,代码来源:backend.go


示例2: Join

func (r *RoomBase) Join(ctx scope.Context, session proto.Session) (string, error) {
	client := &proto.Client{}
	if !client.FromContext(ctx) {
		return "", fmt.Errorf("client data not found in scope")
	}

	r.m.Lock()
	defer r.m.Unlock()

	if r.identities == nil {
		r.identities = map[proto.UserID]proto.Identity{}
	}
	if r.nicks == nil {
		r.nicks = map[proto.UserID]string{}
	}
	if r.live == nil {
		r.live = map[proto.UserID][]proto.Session{}
	}
	if r.clients == nil {
		r.clients = map[string]*proto.Client{}
	}

	ident := session.Identity()
	id := ident.ID()

	if banned, ok := r.agentBans[ident.ID()]; ok && banned.After(time.Now()) {
		return "", proto.ErrAccessDenied
	}

	if banned, ok := r.ipBans[client.IP]; ok && banned.After(time.Now()) {
		return "", proto.ErrAccessDenied
	}

	if _, ok := r.identities[id]; !ok {
		r.identities[id] = ident
	}

	r.live[id] = append(r.live[id], session)
	r.clients[session.ID()] = client

	event := proto.PresenceEvent(session.View(proto.Staff))
	return "virt:" + event.RealClientAddress, r.broadcast(ctx, proto.JoinType, &event, session)
}
开发者ID:logan,项目名称:heim,代码行数:43,代码来源:room.go


示例3: Part

func (r *RoomBase) Part(ctx scope.Context, session proto.Session) error {
	r.m.Lock()
	defer r.m.Unlock()

	ident := session.Identity()
	id := ident.ID()
	live := r.live[id]
	for i, s := range live {
		if s == session {
			copy(live[i:], live[i+1:])
			r.live[id] = live[:len(live)-1]
		}
	}
	if len(r.live[id]) == 0 {
		delete(r.live, id)
		delete(r.identities, id)
	}
	delete(r.clients, session.ID())
	event := proto.PresenceEvent(session.View(proto.Staff))
	return r.broadcast(ctx, proto.PartEventType, &event, session)
}
开发者ID:logan,项目名称:heim,代码行数:21,代码来源:room.go


示例4: part

func (b *Backend) part(ctx scope.Context, room *Room, session proto.Session) error {
	b.Lock()
	defer b.Unlock()

	if lm, ok := b.listeners[room.Name]; ok {
		delete(lm, session.ID())
	}

	_, err := b.DbMap.Exec(
		"DELETE FROM presence"+
			" WHERE room = $1 AND server_id = $2 AND server_era = $3 AND session_id = $4",
		room.Name, b.desc.ID, b.desc.Era, session.ID())
	if err != nil {
		backend.Logger(ctx).Printf("failed to persist departure: %s", err)
	}

	// Broadcast a presence event.
	// TODO: make this an explicit action via the Room protocol, to support encryption
	return b.broadcast(ctx, room,
		proto.PartEventType, proto.PresenceEvent(*session.View()), session)
}
开发者ID:rmasoni,项目名称:heim,代码行数:21,代码来源:backend.go


示例5: Join

func (r *memRoom) Join(ctx scope.Context, session proto.Session) error {
	client := &proto.Client{}
	if !client.FromContext(ctx) {
		return fmt.Errorf("client data not found in scope")
	}

	r.m.Lock()
	defer r.m.Unlock()

	if r.identities == nil {
		r.identities = map[proto.UserID]proto.Identity{}
	}
	if r.live == nil {
		r.live = map[proto.UserID][]proto.Session{}
	}
	if r.clients == nil {
		r.clients = map[string]*proto.Client{}
	}

	ident := session.Identity()
	id := ident.ID()

	if banned, ok := r.agentBans[ident.ID()]; ok && banned.After(time.Now()) {
		return proto.ErrAccessDenied
	}

	if banned, ok := r.ipBans[client.IP]; ok && banned.After(time.Now()) {
		return proto.ErrAccessDenied
	}

	if _, ok := r.identities[id]; !ok {
		r.identities[id] = ident
	}

	r.live[id] = append(r.live[id], session)
	r.clients[session.ID()] = client

	return r.broadcast(ctx, proto.JoinType,
		proto.PresenceEvent(*session.View()), session)
}
开发者ID:ArkaneMoose,项目名称:heim,代码行数:40,代码来源:room.go


示例6: join


//.........这里部分代码省略.........
		client.IP, rb.RoomName)
	if err != nil {
		rollback(ctx, t)
		return "", err
	}
	if len(ipBans) > 0 {
		logging.Logger(ctx).Printf("access denied to %s: %#v", client.IP, ipBans)
		if err := t.Rollback(); err != nil {
			return "", err
		}
		return "", proto.ErrAccessDenied
	}

	// Virtualize the session's client address.
	var row struct {
		Address string `db:"address"`
	}
	if err := t.SelectOne(&row, "SELECT virtualize_address($1, $2::inet) AS address", rb.RoomName, client.IP); err != nil {
		return "", err
	}
	virtualAddress := row.Address

	// Look up session's nick.
	nickRow, err := t.Get(Nick{}, string(session.Identity().ID()), rb.RoomName)
	if err != nil && err != sql.ErrNoRows {
		return "", err
	}
	if nickRow != nil {
		session.SetName(nickRow.(*Nick).Nick)
	}

	// Write to session log.
	// TODO: do proper upsert simulation
	entry := &SessionLog{
		SessionID: session.ID(),
		IP:        client.IP,
		Room:      rb.RoomName,
		UserAgent: client.UserAgent,
		Connected: client.Connected,
	}
	if _, err := t.Delete(entry); err != nil {
		if rerr := t.Rollback(); rerr != nil {
			logging.Logger(ctx).Printf("rollback error: %s", rerr)
		}
		return "", err
	}
	if err := t.Insert(entry); err != nil {
		if rerr := t.Rollback(); rerr != nil {
			logging.Logger(ctx).Printf("rollback error: %s", rerr)
		}
		return "", err
	}

	// Broadcast a presence event.
	// TODO: make this an explicit action via the Room protocol, to support encryption

	presence := &Presence{
		Room:      rb.RoomName,
		ServerID:  b.desc.ID,
		ServerEra: b.desc.Era,
		SessionID: session.ID(),
		Updated:   time.Now(),
	}
	sessionView := session.View(proto.Staff)
	sessionView.ClientAddress = virtualAddress
	err = presence.SetFact(&proto.Presence{
		SessionView:    sessionView,
		LastInteracted: presence.Updated,
	})
	if err != nil {
		rollback(ctx, t)
		return "", fmt.Errorf("presence marshal error: %s", err)
	}
	if err := t.Insert(presence); err != nil {
		return "", fmt.Errorf("presence insert error: %s", err)
	}

	b.Lock()
	// Add session to listeners.
	lm, ok := b.listeners[rb.RoomName]
	if !ok {
		lm = ListenerMap{}
		b.listeners[rb.RoomName] = lm
	}
	lm[session.ID()] = Listener{Session: session, Client: client}
	b.Unlock()

	event := proto.PresenceEvent(session.View(proto.Staff))
	event.ClientAddress = virtualAddress
	if err := rb.broadcast(ctx, t, proto.JoinEventType, event, session); err != nil {
		rollback(ctx, t)
		return "", err
	}

	if err := t.Commit(); err != nil {
		return "", err
	}

	return virtualAddress, nil
}
开发者ID:logan,项目名称:heim,代码行数:101,代码来源:backend.go


示例7: join


//.........这里部分代码省略.........
		fmt.Sprintf(
			"SELECT %s FROM banned_agent WHERE agent_id = $1 AND (room IS NULL OR room = $2) AND (expires IS NULL OR expires > NOW())",
			bannedAgentCols),
		session.Identity().ID().String(), room.Name)
	if err != nil {
		rb()
		return err
	}
	if len(agentBans) > 0 {
		logging.Logger(ctx).Printf("access denied to %s: %#v", session.Identity().ID(), agentBans)
		if err := t.Rollback(); err != nil {
			return err
		}
		return proto.ErrAccessDenied
	}

	// Check for IP bans.
	ipBans, err := t.Select(
		BannedIP{},
		fmt.Sprintf(
			"SELECT %s FROM banned_ip WHERE ip = $1 AND (room IS NULL OR room = $2) AND (expires IS NULL OR expires > NOW())",
			bannedIPCols),
		client.IP, room.Name)
	if err != nil {
		rb()
		return err
	}
	if len(ipBans) > 0 {
		logging.Logger(ctx).Printf("access denied to %s: %#v", client.IP, ipBans)
		if err := t.Rollback(); err != nil {
			return err
		}
		return proto.ErrAccessDenied
	}

	// Write to session log.
	// TODO: do proper upsert simulation
	entry := &SessionLog{
		SessionID: session.ID(),
		IP:        client.IP,
		Room:      room.Name,
		UserAgent: client.UserAgent,
		Connected: client.Connected,
	}
	if _, err := t.Delete(entry); err != nil {
		if rerr := t.Rollback(); rerr != nil {
			logging.Logger(ctx).Printf("rollback error: %s", rerr)
		}
		return err
	}
	if err := t.Insert(entry); err != nil {
		if rerr := t.Rollback(); rerr != nil {
			logging.Logger(ctx).Printf("rollback error: %s", rerr)
		}
		return err
	}

	// Broadcast a presence event.
	// TODO: make this an explicit action via the Room protocol, to support encryption

	presence := &Presence{
		Room:      room.Name,
		ServerID:  b.desc.ID,
		ServerEra: b.desc.Era,
		SessionID: session.ID(),
		Updated:   time.Now(),
	}
	err = presence.SetFact(&proto.Presence{
		SessionView:    *session.View(),
		LastInteracted: presence.Updated,
	})
	if err != nil {
		rb()
		return fmt.Errorf("presence marshal error: %s", err)
	}
	if err := t.Insert(presence); err != nil {
		return fmt.Errorf("presence insert error: %s", err)
	}

	b.Lock()
	// Add session to listeners.
	lm, ok := b.listeners[room.Name]
	if !ok {
		lm = ListenerMap{}
		b.listeners[room.Name] = lm
	}
	lm[session.ID()] = Listener{Session: session, Client: client}
	b.Unlock()

	if err := room.broadcast(ctx, t, proto.JoinEventType, proto.PresenceEvent(*session.View()), session); err != nil {
		rb()
		return err
	}

	if err := t.Commit(); err != nil {
		return err
	}

	return nil
}
开发者ID:bramvdbogaerde,项目名称:heim,代码行数:101,代码来源:backend.go


示例8: join


//.........这里部分代码省略.........
	if !client.FromContext(ctx) {
		return fmt.Errorf("client data not found in scope")
	}

	// Check for agent ID bans.
	agentBans, err := b.DbMap.Select(
		BannedAgent{},
		"SELECT agent_id, room, created, expires, room_reason, agent_reason, private_reason"+
			" FROM banned_agent"+
			" WHERE agent_id = $1 AND (room IS NULL OR room = $2)"+
			" AND (expires IS NULL OR expires > NOW())",
		session.Identity().ID().String(), room.Name)
	if err != nil {
		return err
	}
	if len(agentBans) > 0 {
		backend.Logger(ctx).Printf("access denied to %s: %#v", session.Identity().ID(), agentBans)
		return proto.ErrAccessDenied
	}

	// Check for IP bans.
	ipBans, err := b.DbMap.Select(
		BannedIP{},
		"SELECT ip, room, created, expires, reason FROM banned_ip"+
			" WHERE ip = $1 AND (room IS NULL OR room = $2)"+
			" AND (expires IS NULL OR expires > NOW())",
		client.IP, room.Name)
	if err != nil {
		return err
	}
	if len(ipBans) > 0 {
		backend.Logger(ctx).Printf("access denied to %s: %#v", client.IP, ipBans)
		return proto.ErrAccessDenied
	}

	// Write to session log.
	entry := &SessionLog{
		SessionID: session.ID(),
		IP:        client.IP,
		Room:      room.Name,
		UserAgent: client.UserAgent,
		Connected: client.Connected,
	}
	t, err := b.DbMap.Begin()
	if err != nil {
		return err
	}
	if _, err := t.Delete(entry); err != nil {
		if rerr := t.Rollback(); rerr != nil {
			backend.Logger(ctx).Printf("rollback error: %s", rerr)
		}
		return err
	}
	if err := t.Insert(entry); err != nil {
		if rerr := t.Rollback(); rerr != nil {
			backend.Logger(ctx).Printf("rollback error: %s", rerr)
		}
		return err
	}
	if err := t.Commit(); err != nil {
		return err
	}

	b.Lock()
	defer b.Unlock()

	// Add session to listeners.
	lm, ok := b.listeners[room.Name]
	if !ok {
		b.debug("registering listener for %s", room.Name)
		lm = ListenerMap{}
		b.listeners[room.Name] = lm
	}
	lm[session.ID()] = Listener{Session: session, Client: client}

	// Broadcast a presence event.
	// TODO: make this an explicit action via the Room protocol, to support encryption

	presence := &Presence{
		Room:      room.Name,
		ServerID:  b.desc.ID,
		ServerEra: b.desc.Era,
		SessionID: session.ID(),
		Updated:   time.Now(),
	}
	err = presence.SetFact(&proto.Presence{
		SessionView:    *session.View(),
		LastInteracted: presence.Updated,
	})
	if err != nil {
		return fmt.Errorf("presence marshal error: %s", err)
	}
	if err := b.DbMap.Insert(presence); err != nil {
		return fmt.Errorf("presence insert error: %s", err)
	}
	b.debug("joining session: %#v", session)
	b.debug(" -> %#v", session.View())
	return b.broadcast(ctx, room,
		proto.JoinEventType, proto.PresenceEvent(*session.View()), session)
}
开发者ID:rmasoni,项目名称:heim,代码行数:101,代码来源:backend.go



注:本文中的euphoria/io/heim/proto.PresenceEvent函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang proto.Account类代码示例发布时间:2022-05-24
下一篇:
Golang proto.MakeEvent函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap