Skip to content
/ tea Public
forked from k4s/tea

go网关游戏服务器分布式开发框架

Notifications You must be signed in to change notification settings

xiaolia47/tea

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tea 游戏服务器框架


Tea 的基础模块来自于 Leaf,由于想做成更加简单易用的GameFrame,所以就有了Tea.

交流QQ群:376389675

Tea :

  • 像开发Web一样简单去开发Game.
  • 每个用户走在单个goroutine上,更加适合多核支持并发处理.

Tea 的框架:

┌-----| |---| |---| |---------------| |---| |---| |------┐
|     | |   | |   | |     Gate      | |   | |   | |      |
|  ┌--| |---| |---| |---------------| |---| |---| |--┐   |
|  |  |a|   |a|   |a|               |a|   |a|   |a|  |   |
|  |  |g|   |g|   |g|   TCP Server  |g|   |g|   |g|  |   |
|  |  |e|   |e|   |e|      or       |e|   |e|   |e|  |   |
|  |  |n|   |n|   |n|   WebSocket   |n|   |n|   |n|  |   |
|  |  |t|   |t|   |t|               |t|   |t|   |t|  |   |
|  └--| |---| |---| |---------------| |---| |---| |--┘   |
|  ┌--| |---| |---| |---------------| |---| |---| |--┐   |
|  |  | |   | |   | |  Msg Process  | |   | |   | |  |   |
|  └--| |---| |---| |---------------| |---| |---| |--┘   |
└-----| |---| |---| |---------------| |---| |---| |------┘
┌-----| |---| |---| |---------------| |---| |---| |------┐
|                 Goroutine Deal with Msg                |
└--------------------------------------------------------┘

基于Tea的开发:

  1. 获取tea server base:
git clone https://github.com/k4s/teaserver
  1. 配置conf目录,选择一种msg协议作为通讯协议,编写对应的msg
Protocol  = "json"
  1. game编写对应msg的处理函数.
func MsgHello(msg interface{}, agent network.InAgent) {
	m := msg.(*ms.Hello)
	fmt.Println("Hello,", m.Name)
	hi := &ms.Hello{Name: "kas", Age: 10}
	agent.WriteMsg(hi)
}
  1. router做路由映射.
process.Processor.SetHandler(&msg.Hello{}, game.MsgHello)
  1. 如果需要在agent新建或者关闭执行函数,在game/agent里面编写对应的函数.
  1. 新建agent:
func initAgent(agent network.ExAgent) {
	fmt.Println("InitFunc")
}
  1. 关闭agent:
func closeAgent(agent network.ExAgent) {
	fmt.Println("CloseFunc")
}
  1. 执行你的程序:
cd teaserver
go run main.go

客户端测试

json:

package main

import (
    "encoding/binary"
    "net"
)

func main() {
    conn, err := net.Dial("tcp", "127.0.0.1:8080")
    if err != nil {
        panic(err)
    }

    // Hello 消息(JSON 格式)
    data := []byte(`{
        "Hello": {
            "Name": "tea"
        }
    }`)

    // len + data
    m := make([]byte, 2+len(data))

    // 默认使用大端序
    binary.BigEndian.PutUint16(m, uint16(len(data)))

    copy(m[2:], data)

    // 发送消息
    conn.Write(m)
}

protobuf:

package main

import (
    "encoding/binary"
    "net"
    "fmt"

    "github.com/k4s/teaServer/msg"
    "github.com/golang/protobuf/proto"
)

func main() {
    conn, err := net.Dial("tcp", "127.0.0.1:8080")
    if err != nil {
        panic(err)
    }

    login := &msg.Login{
       Name:"kas",
       Password:"123456",
   }
   // 进行编码
    data, err := proto.Marshal(login)
    if err != nil {
        panic( err)
    }
    m := make([]byte, 4+len(data))
    // // 默认使用大端序
    binary.BigEndian.PutUint16(m, uint16(2+len(data)))
    binary.BigEndian.PutUint16(m[2:], uint16(0))
    copy(m[4:], data)

    // 发送消息
    conn.Write(m)
    // 接受消息
    var buf = make([]byte, 32)
    n, err := conn.Read(buf)
    if err != nil {
        fmt.Println("read error:", err)
    } else {
        fmt.Printf("read % bytes, content is %s\n", n, string(buf[:n]))
    }
    readlogin := &msg.Login{}
    err = proto.Unmarshal(buf[4:n],readlogin)
    if err != nil{
        fmt.Println(err)
    }
    fmt.Println("readlogin:",readlogin)

}

About

go网关游戏服务器分布式开发框架

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%