當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Session.SetName方法代碼示例

本文整理匯總了Golang中euphoria/io/heim/proto.Session.SetName方法的典型用法代碼示例。如果您正苦於以下問題:Golang Session.SetName方法的具體用法?Golang Session.SetName怎麽用?Golang Session.SetName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在euphoria/io/heim/proto.Session的用法示例。


在下文中一共展示了Session.SetName方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: join

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

	bannedAgentCols, err := allColumns(b.DbMap, BannedAgent{}, "")
	if err != nil {
		return "", err
	}

	bannedIPCols, err := allColumns(b.DbMap, BannedIP{}, "")
	if err != nil {
		return "", err
	}

	t, err := b.DbMap.Begin()
	if err != nil {
		return "", err
	}

	// Check for agent ID bans.
	agentBans, err := t.Select(
		BannedAgent{},
		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(), rb.RoomName)
	if err != nil {
		rollback(ctx, t)
		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, 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.
//.........這裏部分代碼省略.........
開發者ID:logan,項目名稱:heim,代碼行數:101,代碼來源:backend.go


注:本文中的euphoria/io/heim/proto.Session.SetName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。