Skip to content

Commit

Permalink
*: fix examples
Browse files Browse the repository at this point in the history
Signed-off-by: Lonng <heng@lonng.org>
  • Loading branch information
lonng committed Jun 30, 2019
1 parent efc1def commit d2d2397
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
34 changes: 17 additions & 17 deletions examples/demo/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ type (
}
)

func (stats *stats) outbound(s *session.Session, msg pipeline.Message) error {
func (stats *stats) outbound(s *session.Session, msg *pipeline.Message) error {
stats.outboundBytes += len(msg.Data)
return nil
}

func (stats *stats) inbound(s *session.Session, msg pipeline.Message) error {
func (stats *stats) inbound(s *session.Session, msg *pipeline.Message) error {
stats.inboundBytes += len(msg.Data)
return nil
}
Expand Down Expand Up @@ -134,28 +134,28 @@ func (mgr *RoomManager) Message(s *session.Session, msg *UserMessage) error {
}

func main() {
// override default serializer
nano.SetSerializer(json.NewSerializer())

// rewrite component and handler name
room := NewRoomManager()
nano.Register(room,
component.WithName("room"),
components := &component.Components{}
components.Register(
NewRoomManager(),
component.WithName("room"), // rewrite component and handler name
component.WithNameFunc(strings.ToLower),
)

// traffic stats
pipeline := pipeline.New()
pip := pipeline.New()
var stats = &stats{}
pipeline.Outbound().PushBack(stats.outbound)
pipeline.Inbound().PushBack(stats.inbound)
pip.Outbound().PushBack(stats.outbound)
pip.Inbound().PushBack(stats.inbound)

nano.EnableDebug()
log.SetFlags(log.LstdFlags | log.Llongfile)
nano.SetWSPath("/nano")

http.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir("web"))))

nano.SetCheckOriginFunc(func(_ *http.Request) bool { return true })
nano.ListenWS(":3250", nano.WithPipeline(pipeline))
nano.ListenWS(":3250",
nano.WithPipeline(pip),
nano.WithCheckOriginFunc(func(_ *http.Request) bool { return true }),
nano.WithWSPath("/nano"),
nano.WithDebugMode(),
nano.WithSerializer(json.NewSerializer()), // override default serializer
nano.WithComponents(components),
)
}
18 changes: 11 additions & 7 deletions examples/demo/tadpole/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/lonng/nano"
"github.com/lonng/nano/component"
"github.com/lonng/nano/examples/demo/tadpole/logic"
"github.com/lonng/nano/serialize/json"
"github.com/urfave/cli"
Expand Down Expand Up @@ -35,20 +36,23 @@ func main() {
}

func serve(ctx *cli.Context) error {
components := &component.Components{}
components.Register(logic.NewManager())
components.Register(logic.NewWorld())

// register all service
nano.Register(logic.NewManager())
nano.Register(logic.NewWorld())
nano.SetSerializer(json.NewSerializer())
options := []nano.Option{
nano.WithComponents(components),
nano.WithSerializer(json.NewSerializer()),
nano.WithCheckOriginFunc(func(_ *http.Request) bool { return true }),
}

//nano.EnableDebug()
log.SetFlags(log.LstdFlags | log.Llongfile)

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))

nano.SetCheckOriginFunc(func(_ *http.Request) bool { return true })

addr := ctx.String("addr")
nano.ListenWS(addr)

nano.ListenWS(addr, options...)
return nil
}
4 changes: 2 additions & 2 deletions mock/network_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func (s *networkEntitySuite) TestNetworkEntity(c *C) {
entity := mock.NewNetworkEntity()

c.Assert(entity.LastResponse(), IsNil)
c.Assert(entity.MID(), Equals, uint64(1))
c.Assert(entity.LastMid(), Equals, uint64(1))
c.Assert(entity.Response("hello"), IsNil)
c.Assert(entity.LastResponse().(string), Equals, "hello")

c.Assert(entity.FindResponseByMID(1), IsNil)
c.Assert(entity.ResponseMID(1, "test"), IsNil)
c.Assert(entity.ResponseMid(1, "test"), IsNil)
c.Assert(entity.FindResponseByMID(1).(string), Equals, "test")

c.Assert(entity.FindResponseByRoute("t.tt"), IsNil)
Expand Down
2 changes: 2 additions & 0 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

type (
Message = message.Message

Func func(s *session.Session, msg *message.Message) error

Pipeline interface {
Expand Down

0 comments on commit d2d2397

Please sign in to comment.