U3D测试链接LollipopGo游戏服务器登录服-2

作者: Golang语言社区 | 来源:发表于2019-04-15 14:04 被阅读3次

    原文地址:http://www.byteedu.com/forum.php?mod=viewthread&tid=565&page=1&extra=#pid799

    一. 如何查找协议
    Login_DT_http.go:

    package main
    
    import (
            _ "LollipopGo/LollipopGo/player"
            "Proto"
            "Proto/Proto2"
            "encoding/base64"
            "encoding/json"
            "fmt"
            "net/http"
            "net/rpc/jsonrpc"
            "strconv"
    )
    
    /*
      登录服务器:
      http://127.0.0.1:8891/GolangLtdDT?Protocol=8&Protocol2=1
      47.107.125.75 == run.babaliuliu.com
    */
    
    func IndexHandler(w http.ResponseWriter, req *http.Request) {
    
            if req.Method == "GET" {
                    w.Header().Set("Access-Control-Allow-Origin", "*")
                    req.ParseForm()
                    defer func() { // 必须要先声明defer,否则不能捕获到panic异常
                            if err := recover(); err != nil {
                                    fmt.Println("%s", err)
    
                                    req.Body.Close()
                            }
                    }()
                    Protocol, bProtocol := req.Form["Protocol"]
                    Protocol2, bProtocol2 := req.Form["Protocol2"]
    
                    if bProtocol && bProtocol2 {
                            // 主协议判断
                            if Protocol[0] == strconv.Itoa(Proto.G_GameLogin_Proto) {
                                    // 子协议判断
                                    switch Protocol2[0] {
                                    case strconv.Itoa(Proto2.C2GL_GameLoginProto2):
                                            // DB server 获取 验证信息  rpc 操作
                                            //------------------------------------------------------
                                            // 暂时不解析用户名和密码 --> 后面独立出来再增加!!!
                                            data := DB_rpc_()
                                            b, _ := json.Marshal(data)
                                            fmt.Fprint(w, base64.StdEncoding.EncodeToString(b))
                                            //------------------------------------------------------
                                            return
                                    default:
                                            fmt.Fprintln(w, "88902")
                                            return
                                    }
                            }
                            fmt.Fprintln(w, "88904")
                            return
                    }
                    // 服务器获取通信方式错误 --> 8890 + 1
                    fmt.Fprintln(w, "88901")
                    return
            }
    }
    
    // jsonrpc 数据处理
    func DB_rpc_() interface{} {
            // 链接DB操作
            client, err := jsonrpc.Dial("tcp", service)
            if err != nil {
                    fmt.Println("dial error:", err)
            }
            args := Args{1, 2}
            var reply Proto2.GL2C_GameLogin
            // 同步调用
            // err = client.Call("Arith.Muliply", args, &reply)
            // if err != nil {
            //         fmt.Println("Arith.Muliply call error:", err)
            // }
            // 异步调用
            divCall := client.Go("Arith.Muliply", args, &reply, nil)
            replyCall := <-divCall.Done // will be equal to divCall
            fmt.Println(replyCall.Reply)
            // 返回的数据
            fmt.Println("the arith.mutiply is :", reply)
            return reply
    }
    
    
    image image image

    协议字段:

    package Proto2
    
    import (
            "LollipopGo/LollipopGo/conf"
            "LollipopGo/LollipopGo/player"
    )
    
    // G_GameLogin_Proto 的子协议
    // 属于HTTP 与 DBserver 进行通信
    // 获取到登录正确的信息后,token 返回给网关server
    const (
            INIT_GameLogin       = iota
            C2GL_GameLoginProto2 // C2GL_GameLoginProto2 == 1      client -->登录请求
            GL2C_GameLoginProto2 // GL2C_GameLoginProto2 == 2      返回数据
    )
    
    //------------------------------------------------------------------------------
    // C2GL_GameLoginProto2
    // 客户端请求协议
    type C2GL_GameLogin struct {
            Protocol  int    // 主协议
            Protocol2 int    // 子协议
            LoginName string // 登录名字
            LoginPW   string // 登录密码
            Timestamp int    // 时间戳
    }
    
    // GL2C_GameLoginProto2
    // 登录服务器返回给客户端协议
    type GL2C_GameLogin struct {
            Protocol    int                          // 主协议
            Protocol2   int                          // 子协议
            Tocken      string                       // server 验证加密数据
            PlayerST    *player.PlayerSt             // 玩家的结构
            GateWayST   *player.GateWayList          // 大厅链接地址
            GameList    map[string]*conf.GameList    // 游戏列表
            GameListNew map[string]*conf.GameListNew // 游戏列表New
            BannerList  map[string]*conf.Banner      // 广告列表
    }
    
    //------------------------------------------------------------------------------
    
    

    同学们这下知道了怎么查找协议了吧!下节课我们就开始实际编码了

    相关文章

      网友评论

        本文标题:U3D测试链接LollipopGo游戏服务器登录服-2

        本文链接:https://www.haomeiwen.com/subject/susuwqtx.html