本文整理汇总了Golang中github.com/mconintet/kiwi.MessageSender.SendWholeBytes方法的典型用法代码示例。如果您正苦于以下问题:Golang MessageSender.SendWholeBytes方法的具体用法?Golang MessageSender.SendWholeBytes怎么用?Golang MessageSender.SendWholeBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/mconintet/kiwi.MessageSender
的用法示例。
在下文中一共展示了MessageSender.SendWholeBytes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handDelBuddy
func handDelBuddy(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error {
uid, err := j.Get("uid").Int()
if err != nil {
return ErrDeformedRequest
}
token, err := j.Get("token").String()
if err != nil {
return ErrDeformedRequest
}
buddyID, err := j.Get("buddyID").String()
if err != nil {
return ErrDeformedRequest
}
db, err := sql.Open("mysql", dbConnInfo)
if err != nil {
return err
}
defer db.Close()
// validate request info
stmtOut, err := db.Prepare("SELECT COUNT(*) FROM user WHERE id=? AND token=?")
if err != nil {
return err
}
defer stmtOut.Close()
var count int
err = stmtOut.QueryRow(uid, token).Scan(&count)
if err != nil {
return err
}
if count == 0 {
return ErrIllegalRequest
}
// delete relation
stmtUpe, err := db.Prepare("DELETE FROM user_buddy WHERE user=? AND buddy=?")
if err != nil {
return err
}
defer stmtUpe.Close()
_, err = stmtUpe.Exec(uid, buddyID)
if err != nil {
return errors.New(fmt.Sprintf("cannot delete relation user %d buddy:%d err: %s", uid, buddyID, err.Error()))
}
retMsg := simplejson.New()
retMsg.Set("action", "del-buddy-return")
retMsg.Set("errCode", ErrNone)
retMsg.Set("errMsg", ErrCodeMsg[ErrNone])
byts, _ := retMsg.MarshalJSON()
s.SendWholeBytes(byts, false)
return nil
}
示例2: 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
}
示例3: sendNeedsLogin
func sendNeedsLogin(action string, s kiwi.MessageSender) {
retMsg := simplejson.New()
retMsg.Set("action", action)
retMsg.Set("errCode", ErrInvalidToken)
retMsg.Set("errMsg", ErrCodeMsg[ErrInvalidToken])
byts, _ := retMsg.MarshalJSON()
s.SendWholeBytes(byts, false)
}
示例4: sendUnreachable
func sendUnreachable(msgID int, s kiwi.MessageSender) error {
retMsg := simplejson.New()
retMsg.Set("action", "send-message-return")
retMsg.Set("msgID", msgID)
retMsg.Set("errCode", ErrCodeUnreachable)
retMsg.Set("errMsg", ErrCodeMsg[ErrCodeUnreachable])
byts, _ := retMsg.MarshalJSON()
_, err := s.SendWholeBytes(byts, false)
return err
}
示例5: handleLogout
func handleLogout(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
}
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("logout-return", s)
return nil
}
// remove connId from uid-connId map
uidConnIdMap.delUid(uid)
// logout user
logoutUser(uid)
retMsg := simplejson.New()
retMsg.Set("action", "logout-return")
retMsg.Set("errCode", ErrNone)
retMsg.Set("errMsg", ErrCodeMsg[ErrNone])
byts, _ := retMsg.MarshalJSON()
s.SendWholeBytes(byts, false)
return nil
}
示例6: 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
}
示例7: 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
}
示例8: handleGetUserInfo
func handleGetUserInfo(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
}
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("get-user-info-return", s)
return nil
}
stmtOut, err := db.Prepare("SELECT nickname,avatar FROM user WHERE id=?")
if err != nil {
return err
}
defer stmtOut.Close()
var nickname sql.NullString
var avatar []byte
err = stmtOut.QueryRow(uid).Scan(&nickname, &avatar)
if err != nil {
if err == sql.ErrNoRows {
retMsg := simplejson.New()
retMsg.Set("action", "get-user-info-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
}
retMsg := simplejson.New()
retMsg.Set("action", "get-user-info-return")
retMsg.Set("uid", uid)
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
}
示例9: handAddBuddy
func handAddBuddy(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error {
uid, err := j.Get("uid").Int()
if err != nil {
return ErrDeformedRequest
}
token, err := j.Get("token").String()
if err != nil {
return ErrDeformedRequest
}
buddyID, err := j.Get("buddyID").Int()
if err != nil {
return ErrDeformedRequest
}
db, err := sql.Open("mysql", dbConnInfo)
if err != nil {
return err
}
defer db.Close()
// validate request info
stmtOut, err := db.Prepare("SELECT COUNT(*) FROM user WHERE id=? AND token=?")
if err != nil {
return err
}
var count int
err = stmtOut.QueryRow(uid, token).Scan(&count)
if err != nil {
return err
}
stmtOut.Close()
if count == 0 {
return ErrIllegalRequest
}
// check if buddy is exists or not
stmtOut, err = db.Prepare("SELECT COUNT(*) FROM user WHERE id=?")
if err != nil {
return err
}
defer stmtOut.Close()
err = stmtOut.QueryRow(buddyID).Scan(&count)
if err != nil {
return err
}
if count == 0 {
retMsg := simplejson.New()
retMsg.Set("action", "add-buddy-return")
retMsg.Set("errCode", ErrBuddyNotFound)
retMsg.Set("errMsg", ErrCodeMsg[ErrBuddyNotFound])
byts, _ := retMsg.MarshalJSON()
s.SendWholeBytes(byts, false)
return nil
}
// check if relation is exists or not
stmtOut, err = db.Prepare("SELECT COUNT(*) FROM user_buddy WHERE user=? AND buddy=?")
if err != nil {
return err
}
defer stmtOut.Close()
err = stmtOut.QueryRow(uid, buddyID).Scan(&count)
if err != nil {
return err
}
if count == 1 {
retMsg := simplejson.New()
retMsg.Set("action", "add-buddy-return")
retMsg.Set("errCode", ErrBuddyAlreadyExists)
retMsg.Set("errMsg", ErrCodeMsg[ErrBuddyAlreadyExists])
byts, _ := retMsg.MarshalJSON()
s.SendWholeBytes(byts, false)
return nil
}
stmtIns, err := db.Prepare("INSERT INTO user_buddy (user, buddy) VALUES(?, ?)")
if err != nil {
return err
}
defer stmtIns.Close()
_, err = stmtIns.Exec(uid, buddyID)
if err != nil {
return err
}
// become friends each other
_, err = stmtIns.Exec(buddyID, uid)
if err != nil {
return err
//.........这里部分代码省略.........
示例10: handGetBuddies
func handGetBuddies(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
}
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("get-buddies-return", s)
return nil
}
// get buddies
stmtOut, err := db.Prepare(`SELECT U.id, U.nickname, U.network, U.avatar
FROM user AS U JOIN user_buddy as B ON U.id=B.buddy WHERE B.user=?`)
if err != nil {
return err
}
defer stmtOut.Close()
rows, err := stmtOut.Query(uid)
if err != nil {
return err
}
buddies := []*simplejson.Json{}
for rows.Next() {
var uid int
var nickname sql.NullString
var network sql.NullInt64
var avatar sql.RawBytes
err = rows.Scan(&uid, &nickname, &network, &avatar)
if err != nil {
return err
}
buddy := simplejson.New()
buddy.Set("uid", uid)
buddy.Set("nickname", nickname.String)
buddy.Set("network", network.Int64)
buddy.Set("avatar", encodeAvatar(avatar))
buddies = append(buddies, buddy)
}
retMsg := simplejson.New()
retMsg.Set("action", "get-buddies-return")
retMsg.Set("buddies", buddies)
retMsg.Set("errCode", ErrNone)
retMsg.Set("errMsg", ErrCodeMsg[ErrNone])
byts, _ := retMsg.MarshalJSON()
s.SendWholeBytes(byts, false)
return nil
}