Skip to content

Commit

Permalink
msgbox
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaonanln committed Oct 25, 2017
1 parent bfe9eca commit 46502cb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions examples/test_client/ClientEntity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/post"
"github.com/xiaonanln/goworld/ext/msgbox"
"github.com/xiaonanln/typeconv"
)

Expand Down Expand Up @@ -346,6 +347,10 @@ func (e *clientEntity) DoTestMsgbox() {
e.CallServer("TestMsgbox")
}

func (e *clientEntity) OnRecvMsgboxMsg(msg msgbox.Msg) {
e.notifyThingDone("DoTestMsgbox")
}

func (e *clientEntity) onAccountCreated() {
post.Post(func() {
username := e.owner.username()
Expand Down
20 changes: 14 additions & 6 deletions examples/test_game/Avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ type Avatar struct {
msgbox.Msgbox
}

// OnCreated is called when avatar is created
func (a *Avatar) OnCreated() {
a.Entity.OnCreated()

func (a *Avatar) OnAttrsReady() {
gwlog.Infof("Avatar %s is ready: client=%s, mails=%d", a, a.GetClient(), a.Attrs.GetMapAttr("mails").Size())
a.setDefaultAttrs()
a.Msgbox.SetMsgHandler(a.handleMsgboxMsg)
}

gwlog.Infof("Avatar %s on created: client=%s, mails=%d", a, a.GetClient(), a.Attrs.GetMapAttr("mails").Size())
// OnCreated is called when avatar is created
func (a *Avatar) OnCreated() {

a.SetFilterProp("spaceKind", strconv.Itoa(int(a.GetInt("spaceKind"))))
a.SetFilterProp("level", strconv.Itoa(int(a.GetInt("level"))))
Expand Down Expand Up @@ -234,6 +235,13 @@ func (a *Avatar) OnPublish(subject string, content string) {
}

func (a *Avatar) TestMsgbox_Client() {
a.Msgbox.Send(a.ID, 1)
a.Msgbox.Send(a.ID, msgbox.Msg{
"sender": a.ID,
"content": "hello",
})
a.Msgbox.Recv()
}

func (a *Avatar) handleMsgboxMsg(msg msgbox.Msg) {
a.CallClient("OnRecvMsgboxMsg", msg)
}
6 changes: 6 additions & 0 deletions ext/msgbox/msgbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/gwutils"
"github.com/xiaonanln/goworld/engine/kvdb"
"github.com/xiaonanln/goworld/engine/kvdb/types"
"github.com/xiaonanln/goworld/engine/netutil"
Expand Down Expand Up @@ -144,6 +145,11 @@ func (mb *Msgbox) Recv() {
func (mb *Msgbox) MsgboxOnRecvMsg(beginMsgId int64, endMsgId int64, msgs []Msg) {
gwlog.Infof("%s: MsgBox.OnRecvMsg: %d -> %d: msgs %v", mb.Entity, beginMsgId, endMsgId, msgs)
mb.Attrs.SetInt(_LastMsgboxMsgIdAttrKey, endMsgId)
for _, msg := range msgs {
gwutils.RunPanicless(func() {
mb.msghandler(msg)
})
}
}

func (mb *Msgbox) SetMsgHandler(handler func(msg Msg)) {
Expand Down

0 comments on commit 46502cb

Please sign in to comment.