• 6.3 使用Zinx-V0.6完成应用程序

    6.3 使用Zinx-V0.6完成应用程序

    Server.go

    1. package main
    2. import (
    3. "fmt"
    4. "zinx/ziface"
    5. "zinx/znet"
    6. )
    7. //ping test 自定义路由
    8. type PingRouter struct {
    9. znet.BaseRouter
    10. }
    11. //Ping Handle
    12. func (this *PingRouter) Handle(request ziface.IRequest) {
    13. fmt.Println("Call PingRouter Handle")
    14. //先读取客户端的数据,再回写ping...ping...ping
    15. fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
    16. err := request.GetConnection().SendMsg(0, []byte("ping...ping...ping"))
    17. if err != nil {
    18. fmt.Println(err)
    19. }
    20. }
    21. //HelloZinxRouter Handle
    22. type HelloZinxRouter struct {
    23. znet.BaseRouter
    24. }
    25. func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
    26. fmt.Println("Call HelloZinxRouter Handle")
    27. //先读取客户端的数据,再回写ping...ping...ping
    28. fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
    29. err := request.GetConnection().SendMsg(1, []byte("Hello Zinx Router V0.6"))
    30. if err != nil {
    31. fmt.Println(err)
    32. }
    33. }
    34. func main() {
    35. //创建一个server句柄
    36. s := znet.NewServer()
    37. //配置路由
    38. s.AddRouter(0, &PingRouter{})
    39. s.AddRouter(1, &HelloZinxRouter{})
    40. //开启服务
    41. s.Serve()
    42. }

    Server端设置了2个路由,一个是MsgId为0的消息会执行PingRouter{}重写的Handle()方法,一个是MsgId为1的消息会执行HelloZinxRouter{}重写的Handle()方法。

    我们现在写两个客户端,分别发送0消息和1消息来进行测试Zinx是否能够处理2个不同的消息业务。

    Client0.go

    1. package main
    2. import (
    3. "fmt"
    4. "io"
    5. "net"
    6. "time"
    7. "zinx/znet"
    8. )
    9. /*
    10. 模拟客户端
    11. */
    12. func main() {
    13. fmt.Println("Client Test ... start")
    14. //3秒之后发起测试请求,给服务端开启服务的机会
    15. time.Sleep(3 * time.Second)
    16. conn,err := net.Dial("tcp", "127.0.0.1:7777")
    17. if err != nil {
    18. fmt.Println("client start err, exit!")
    19. return
    20. }
    21. for {
    22. //发封包message消息
    23. dp := znet.NewDataPack()
    24. msg, _ := dp.Pack(znet.NewMsgPackage(0,[]byte("Zinx V0.6 Client0 Test Message")))
    25. _, err := conn.Write(msg)
    26. if err !=nil {
    27. fmt.Println("write error err ", err)
    28. return
    29. }
    30. //先读出流中的head部分
    31. headData := make([]byte, dp.GetHeadLen())
    32. _, err = io.ReadFull(conn, headData) //ReadFull 会把msg填充满为止
    33. if err != nil {
    34. fmt.Println("read head error")
    35. break
    36. }
    37. //将headData字节流 拆包到msg中
    38. msgHead, err := dp.Unpack(headData)
    39. if err != nil {
    40. fmt.Println("server unpack err:", err)
    41. return
    42. }
    43. if msgHead.GetDataLen() > 0 {
    44. //msg 是有data数据的,需要再次读取data数据
    45. msg := msgHead.(*znet.Message)
    46. msg.Data = make([]byte, msg.GetDataLen())
    47. //根据dataLen从io中读取字节流
    48. _, err := io.ReadFull(conn, msg.Data)
    49. if err != nil {
    50. fmt.Println("server unpack data err:", err)
    51. return
    52. }
    53. fmt.Println("==> Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data))
    54. }
    55. time.Sleep(1*time.Second)
    56. }
    57. }

    Client1.go

    1. package main
    2. import (
    3. "fmt"
    4. "io"
    5. "net"
    6. "time"
    7. "zinx/znet"
    8. )
    9. /*
    10. 模拟客户端
    11. */
    12. func main() {
    13. fmt.Println("Client Test ... start")
    14. //3秒之后发起测试请求,给服务端开启服务的机会
    15. time.Sleep(3 * time.Second)
    16. conn,err := net.Dial("tcp", "127.0.0.1:7777")
    17. if err != nil {
    18. fmt.Println("client start err, exit!")
    19. return
    20. }
    21. for {
    22. //发封包message消息
    23. dp := znet.NewDataPack()
    24. msg, _ := dp.Pack(znet.NewMsgPackage(1,[]byte("Zinx V0.6 Client1 Test Message")))
    25. _, err := conn.Write(msg)
    26. if err !=nil {
    27. fmt.Println("write error err ", err)
    28. return
    29. }
    30. //先读出流中的head部分
    31. headData := make([]byte, dp.GetHeadLen())
    32. _, err = io.ReadFull(conn, headData) //ReadFull 会把msg填充满为止
    33. if err != nil {
    34. fmt.Println("read head error")
    35. break
    36. }
    37. //将headData字节流 拆包到msg中
    38. msgHead, err := dp.Unpack(headData)
    39. if err != nil {
    40. fmt.Println("server unpack err:", err)
    41. return
    42. }
    43. if msgHead.GetDataLen() > 0 {
    44. //msg 是有data数据的,需要再次读取data数据
    45. msg := msgHead.(*znet.Message)
    46. msg.Data = make([]byte, msg.GetDataLen())
    47. //根据dataLen从io中读取字节流
    48. _, err := io.ReadFull(conn, msg.Data)
    49. if err != nil {
    50. fmt.Println("server unpack data err:", err)
    51. return
    52. }
    53. fmt.Println("==> Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data))
    54. }
    55. time.Sleep(1*time.Second)
    56. }
    57. }

    分别执行服务端和两个客户端

    1. $go run Server.go
    1. $go run Client0.go
    1. $go run Client1.go

    服务端显示结果

    1. $ go run Server.go
    2. Add api msgId = 0
    3. Add api msgId = 1
    4. [START] Server name: zinx v-0.6 demoApp,listenner at IP: 127.0.0.1, Port 7777 is starting
    5. [Zinx] Version: V0.4, MaxConn: 3, MaxPacketSize: 4096
    6. start Zinx server zinx v-0.6 demoApp succ, now listenning...
    7. Reader Goroutine is running
    8. Call PingRouter Handle
    9. recv from client : msgId= 0 , data= Zinx V0.6 Client0 Test Message
    10. Reader Goroutine is running
    11. Call HelloZinxRouter Handle
    12. recv from client : msgId= 1 , data= Zinx V0.6 Client1 Test Message
    13. Call PingRouter Handle
    14. recv from client : msgId= 0 , data= Zinx V0.6 Client0 Test Message
    15. Call HelloZinxRouter Handle
    16. recv from client : msgId= 1 , data= Zinx V0.6 Client1 Test Message
    17. Call PingRouter Handle
    18. recv from client : msgId= 0 , data= Zinx V0.6 Client0 Test Message
    19. Call HelloZinxRouter Handle
    20. recv from client : msgId= 1 , data= Zinx V0.6 Client1 Test Message

    客户端0显示结果

    1. $ go run Client0.go
    2. Client Test ... start
    3. ==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
    4. ==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
    5. ==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping

    客户端1显示结果

    1. $ go run Client1.go
    2. Client Test ... start
    3. ==> Recv Msg: ID= 1 , len= 22 , data= Hello Zinx Router V0.6
    4. ==> Recv Msg: ID= 1 , len= 22 , data= Hello Zinx Router V0.6
    5. ==> Recv Msg: ID= 1 , len= 22 , data= Hello Zinx Router V0.6