Ai
1 Star 0 Fork 0

guubohan/nano

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
world.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Lonng 提交于 2018-12-11 06:14 +08:00 . fix imports due to github username change
package logic
import (
"fmt"
"log"
"github.com/google/uuid"
"github.com/lonng/nano"
"github.com/lonng/nano/component"
"github.com/lonng/nano/examples/demo/tadpole/logic/protocol"
"github.com/lonng/nano/session"
)
// World contains all tadpoles
type World struct {
component.Base
*nano.Group
}
// NewWorld returns a world instance
func NewWorld() *World {
return &World{
Group: nano.NewGroup(uuid.New().String()),
}
}
// Init initialize world component
func (w *World) Init() {
session.Lifetime.OnClosed(func(s *session.Session) {
w.Leave(s)
w.Broadcast("leave", &protocol.LeaveWorldResponse{ID: s.ID()})
log.Println(fmt.Sprintf("session count: %d", w.Count()))
})
}
// Enter was called when new guest enter
func (w *World) Enter(s *session.Session, msg []byte) error {
w.Add(s)
log.Println(fmt.Sprintf("session count: %d", w.Count()))
return s.Response(&protocol.EnterWorldResponse{ID: s.ID()})
}
// Update refresh tadpole's position
func (w *World) Update(s *session.Session, msg []byte) error {
return w.Broadcast("update", msg)
}
// Message handler was used to communicate with each other
func (w *World) Message(s *session.Session, msg *protocol.WorldMessage) error {
msg.ID = s.ID()
return w.Broadcast("message", msg)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guubohan/nano.git
git@gitee.com:guubohan/nano.git
guubohan
nano
nano
v0.4.0

搜索帮助