本文整理汇总了Golang中server/network.Conn.SetUserData方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.SetUserData方法的具体用法?Golang Conn.SetUserData怎么用?Golang Conn.SetUserData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类server/network.Conn
的用法示例。
在下文中一共展示了Conn.SetUserData方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: register
/*
* register creates a new client, assigns it an id and returns it
*/
func (reg *ClientRegistry) register(client *network.Conn) uint32 {
var clientId uint32
// protect:
// - increment the next available id
// - client map write
reg.mutex.Lock()
// we have a new client, assign him an id.
clientId = reg.allocId()
reg.clients[clientId] = client
// record the client id inside the connection, this is needed for later
// retriving the clientId when we just have a connection
clientData := ClientData{
Id: clientId,
Name: "",
Joined: false,
}
client.SetUserData(clientData)
// Note: for now we just stupidly increment the next available id.
// We will have other problems to solve before this overflows...
reg.mutex.Unlock()
log.WithFields(log.Fields{
"client": clientData,
"addr": client.GetRawConn().RemoteAddr(),
}).Info("Accepted a new client")
return clientId
}
示例2: Join
func (reg *ClientRegistry) Join(join messages.Join, c *network.Conn) bool {
clientData := c.GetUserData().(ClientData)
log.WithFields(log.Fields{"name": join.Name, "clientData": clientData}).Info("Received JOIN from client")
// client already JOINED?
if clientData.Joined {
reg.Leave("Joined already received", c)
return false
}
// name length condition
if len(join.Name) < 3 {
reg.Leave("Name is too short", c)
return false
}
// name already taken?
nameTaken := false
playerNames := make(map[uint32]string)
// compute the list of joined players, populating the STAY message and
// checking if name is taken as well
reg.ForEach(func(cd ClientData) bool {
nameTaken = cd.Name == join.Name
playerNames[cd.Id] = cd.Name
// stop iteration if name is taken
return !nameTaken
})
if nameTaken {
reg.Leave("Name is already taken", c)
return false
}
// create and send STAY to the new client
stay := messages.Stay{Id: clientData.Id, Players: playerNames}
err := c.AsyncSendPacket(messages.New(messages.StayId, stay), time.Second)
if err != nil {
// handle error in case we couldn't send the STAY message
log.WithError(err).Error("Couldn't send STAY message to the new client")
reg.Leave("Couldn't finish handshaking", c)
return false
}
// fill a JOINED message
joined := &messages.Joined{
Id: clientData.Id,
Name: join.Name,
Type: join.Type,
}
log.WithField("joined", joined).Info("Tell to the world this client has joined")
reg.Broadcast(messages.New(messages.JoinedId, joined))
// at this point we consider the client as accepted
clientData.Joined = true
clientData.Name = join.Name
c.SetUserData(clientData)
return true
}