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


Golang MessageSender.GetConn方法代码示例

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


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

示例1: handleCheckToken

func handleCheckToken(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error {
	uid, err := j.Get("uid").Int64()
	if err != nil {
		return ErrDeformedRequest
	}

	token, err := j.Get("token").String()
	if err != nil {
		return ErrDeformedRequest
	}

	network, err := j.Get("network").Int()
	if err != nil {
		return ErrDeformedRequest
	}

	db, err := sql.Open("mysql", dbConnInfo)
	if err != nil {
		return err
	}
	defer db.Close()

	// valid user and his token
	tokenIsValid, err := checkToken(uid, token)
	if err != nil {
		return err
	}

	if !tokenIsValid {
		sendNeedsLogin("check-token-return", s)
		return nil
	}

	// associate new conn id with user
	uidConnIdMap.add(uid, s.GetConn().ID)

	// update network
	stmtUpe, err := db.Prepare("UPDATE user SET network=? WHERE id=?")
	if err != nil {
		return err
	}
	defer stmtUpe.Close()

	_, err = stmtUpe.Exec(network, uid)
	if err != nil {
		return errors.New(fmt.Sprintf("cannot update conn id of user %d err: %s", uid, err.Error()))
	}

	retMsg := simplejson.New()
	retMsg.Set("action", "check-token-return")
	retMsg.Set("errCode", ErrNone)
	retMsg.Set("errMsg", ErrCodeMsg[ErrNone])

	byts, _ := retMsg.MarshalJSON()
	s.SendWholeBytes(byts, false)
	return nil
}
开发者ID:overcls,项目名称:KiwiChat-Server,代码行数:57,代码来源:checkToken.go

示例2: handleLogin

func handleLogin(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error {
	email, err := j.Get("email").String()
	if err != nil {
		return ErrDeformedRequest
	}

	password, err := j.Get("password").String()
	if err != nil {
		return ErrDeformedRequest
	}

	network, err := j.Get("network").Int()
	if err != nil {
		return ErrDeformedRequest
	}

	db, err := sql.Open("mysql", dbConnInfo)
	if err != nil {
		return err
	}
	defer db.Close()

	// validate login info
	stmtOut, err := db.Prepare("SELECT id,nickname,avatar FROM user WHERE email=? AND password=?")
	if err != nil {
		return err
	}
	defer stmtOut.Close()

	var uid int64
	var nickname sql.NullString
	var avatar []byte
	err = stmtOut.QueryRow(email, makePassword(password)).Scan(&uid, &nickname, &avatar)
	if err != nil {
		if err == sql.ErrNoRows {
			retMsg := simplejson.New()
			retMsg.Set("action", "login-return")
			retMsg.Set("uid", -1)
			retMsg.Set("token", "")
			retMsg.Set("errCode", ErrInvalidLoginInfo)
			retMsg.Set("errMsg", ErrCodeMsg[ErrInvalidLoginInfo])

			byts, _ := retMsg.MarshalJSON()
			s.SendWholeBytes(byts, false)
			return nil
		}
		return err
	}

	// associate new conn id with user
	uidConnIdMap.add(uid, s.GetConn().ID)

	// update token/network
	token := makeToken()
	stmtUpe, err := db.Prepare("UPDATE user SET token=?,network=? WHERE id=?")
	if err != nil {
		return err
	}
	defer stmtUpe.Close()

	_, err = stmtUpe.Exec(token, network, uid)
	if err != nil {
		return errors.New(fmt.Sprintf("cannot update token of user %d err: %s", uid, err.Error()))
	}

	retMsg := simplejson.New()
	retMsg.Set("action", "login-return")
	retMsg.Set("uid", uid)
	retMsg.Set("token", token)
	retMsg.Set("nickname", nickname.String)
	retMsg.Set("avatar", encodeAvatar(avatar))
	retMsg.Set("errCode", ErrNone)
	retMsg.Set("errMsg", ErrCodeMsg[ErrNone])

	byts, _ := retMsg.MarshalJSON()
	s.SendWholeBytes(byts, false)
	return nil
}
开发者ID:overcls,项目名称:KiwiChat-Server,代码行数:78,代码来源:login.go

示例3: handRegister

func handRegister(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error {
	email, err := j.Get("email").String()
	if err != nil {
		return ErrDeformedRequest
	}

	nickname, err := j.Get("nickname").String()
	if err != nil {
		return ErrDeformedRequest
	}

	password, err := j.Get("password").String()
	if err != nil {
		return ErrDeformedRequest
	}

	db, err := sql.Open("mysql", dbConnInfo)
	if err != nil {
		return err
	}
	defer db.Close()

	// validate login info
	stmtOut, err := db.Prepare("SELECT COUNT(*) FROM user WHERE email=?")
	if err != nil {
		return err
	}
	defer stmtOut.Close()

	var count int
	err = stmtOut.QueryRow(email).Scan(&count)
	if err != nil {
		return err
	}

	if count != 0 {
		retMsg := simplejson.New()
		retMsg.Set("action", "register-return")
		retMsg.Set("uid", -1)
		retMsg.Set("token", "")
		retMsg.Set("errCode", ErrEmailAlreadyExists)
		retMsg.Set("errMsg", ErrCodeMsg[ErrEmailAlreadyExists])

		byts, _ := retMsg.MarshalJSON()
		s.SendWholeBytes(byts, false)
		return nil
	}

	// make a default avatar with random color
	avatar := makeDefaultAvatar()

	stmtIns, err := db.Prepare("INSERT INTO user (email, password, nickname, token, avatar) VALUES(?, ?, ?, ?, ?)")
	if err != nil {
		return err
	}
	defer stmtIns.Close()

	token := makeToken()
	res, err := stmtIns.Exec(email, makePassword(password), nickname, token, avatar)
	if err != nil {
		return err
	}

	uid, _ := res.LastInsertId()

	// associate new conn id with user
	uidConnIdMap.add(uid, s.GetConn().ID)

	retMsg := simplejson.New()
	retMsg.Set("action", "register-return")
	retMsg.Set("uid", uid)
	retMsg.Set("nickname", nickname)
	retMsg.Set("avatar", encodeAvatar(avatar))
	retMsg.Set("token", token)
	retMsg.Set("errCode", ErrNone)
	retMsg.Set("errMsg", ErrCodeMsg[ErrNone])

	byts, _ := retMsg.MarshalJSON()
	s.SendWholeBytes(byts, false)
	return nil
}
开发者ID:overcls,项目名称:KiwiChat-Server,代码行数:81,代码来源:register.go

示例4: handleSendMessage

func handleSendMessage(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error {
	token, err := j.Get("token").String()
	if err != nil {
		return ErrDeformedRequest
	}

	from, err := j.Get("from").Int64()
	if err != nil {
		return ErrDeformedRequest
	}

	to, err := j.Get("to").Int64()
	if err != nil {
		return ErrDeformedRequest
	}

	msgID, err := j.Get("msgID").Int()
	if err != nil {
		return ErrDeformedRequest
	}

	msgType, err := j.Get("msgType").Int()
	if err != nil {
		return ErrDeformedRequest
	}

	msgContent, err := j.Get("msgContent").String()
	if err != nil {
		return ErrDeformedRequest
	}

	// valid user and his token
	tokenIsValid, err := checkToken(from, token)
	if err != nil {
		return err
	}

	if !tokenIsValid {
		sendNeedsLogin("send-message-return", s)
		return nil
	}

	// retrieve buddy's conn id
	buddyConnId, ok := uidConnIdMap.getConnId(to)
	if !ok {
		sendUnreachable(msgID, s)
		return nil
	}

	conn, ok := s.GetConn().Server.ConnPool.Get(buddyConnId)
	if !ok {
		sendUnreachable(msgID, s)
		return nil
	}

	msg := simplejson.New()
	msg.Set("action", "new-message")
	msg.Set("from", from)
	msg.Set("msgType", msgType)
	msg.Set("msgContent", msgContent)
	msg.Set("errCode", ErrNone)
	msg.Set("errMsg", ErrCodeMsg[ErrNone])

	sender := &kiwi.DefaultMessageSender{}
	sender.SetConn(conn)
	byts, _ := msg.MarshalJSON()
	_, err = sender.SendWholeBytes(byts, false)

	if err != nil {
		sendUnreachable(msgID, s)
	}
	return nil
}
开发者ID:overcls,项目名称:KiwiChat-Server,代码行数:73,代码来源:sendMessage.go


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