美文网首页彬哥Go语言笔记Golang语言社区
彬哥笔记 --13 Go语言 游戏服务器网关功能简单介绍 实例

彬哥笔记 --13 Go语言 游戏服务器网关功能简单介绍 实例

作者: Golang语言社区 | 来源:发表于2018-12-30 12:29 被阅读21次

          大家好,我是彬哥,本节给大家讲下go语言服务器游戏网关相关,抛砖引玉了,并且本节会议社区开源游戏服务器框架LollipopGo (框架持续更新中)网关处理的为例子

    游戏网关服务器:
    1. 游戏网关服务器可以作为客户端与game server的隔离作用
    2. 消息解析
    3. 与客户端保持连接,作为广播作用
    4. 消息合法性验证
    5. 转发消息到业务服务,针对不同的客户端消息做分发到相应的服务处理
    6. 流量限制,消息分流作用
    7. 版本验证等
    8. 可扩展性,动态拓展

    LollipopGo的网络配置,后期全部通过shell脚本控制端口及服务器类型

    package main
    
    import (
        "LollipopGo/LollipopGo"
        LollipopGoconf "LollipopGo/LollipopGo/conf"
        _ "LollipopGo/LollipopGo/match"
        "LollipopGo/conf"
        "glog-master"
        "net/http"
        _ "net/http/pprof"
        "os"
    
        "code.google.com/p/go.net/websocket"
    )
    
    func init() {
        // 加载配置
        LollipopGoconf.LogLevel = conf.Server.LogLevel
        LollipopGoconf.LogPath = conf.Server.LogPath
        LollipopGoconf.LogFlag = conf.LogFlag
        LollipopGoconf.ConsolePort = conf.Server.ConsolePort
        LollipopGoconf.ProfilePath = conf.Server.ProfilePath
        // 启动所有的版本
        LollipopGo.Run()
    }
    
    func main() {
        // os.Args[0] == 执行文件的名字
        // os.Args[1] == 第一个参数
        // os.Args[2] == 类型 Client -websocket-> GW -websocket/rpc-> GS -websocket/rpc-> DB
        glog.Info(os.Args[:])
        if len(os.Args[:]) < 3 {
            panic("参数小于2个!!! 例如:xxx.exe +【端口】+【服务器类型】")
            return
        }
        strport := "8888"
        strServerType := "GW"
        strServerType_GW := "GW"
        strServerType_GS := "GS"
        strServerType_DB := "DB"
        strServerType_DT := "DT"
        strServerType_GM := "GM"
        strServerType_GL := "GL"
        strServerType_Snake := "Snake"
        if len(os.Args) > 1 {
            strport = os.Args[1]
            strServerType = os.Args[2]
        }
    
        glog.Info(strport)
        glog.Info(strServerType)
        glog.Info(strServerType_GW)
    
        if "GW" == strServerType {
            glog.Info("Golang语言社区  gw")
            strServerType_GW = strServerType
        }
        glog.Info("Golang语言社区")
        glog.Flush()
        if strServerType == strServerType_GW {
            http.Handle("/GolangLtd", websocket.Handler(wwwGolangLtd))
            if err := http.ListenAndServe(":"+strport, nil); err != nil {
                glog.Error("网络错误", err)
                return
            }
        } else if strServerType == strServerType_GS {
            strport = "8889"
            go GameServerINIT()
            http.Handle("/GolangLtdGS", websocket.Handler(wwwGolangLtd))
            if err := http.ListenAndServe(":"+strport, nil); err != nil {
                glog.Error("网络错误", err)
                return
            }
        } else if strServerType == strServerType_DB {
            strport = "8890"
            MainListener(strport)
        } else if strServerType == strServerType_DT {
            strport = "8891"
            http.HandleFunc("/GolangLtdDT", IndexHandler)
            http.ListenAndServe(":"+strport, nil)
        } else if strServerType == strServerType_GM {
            strport = "8892"
            http.HandleFunc("/GolangLtdGM", IndexHandlerGM)
            http.ListenAndServe(":"+strport, nil)
        } else if strServerType == strServerType_Snake {
            strport = "8893"
            http.Handle("/GolangLtdSnake", websocket.Handler(wwwGolangLtd))
            if err := http.ListenAndServe(":"+strport, nil); err != nil {
                glog.Error("网络错误", err)
                return
            }
        } else if strServerType == strServerType_GL {
            strport = "8894"
            http.Handle("/GolangLtdGL", websocket.Handler(wwwGolangLtd))
            if err := http.ListenAndServe(":"+strport, nil); err != nil {
                glog.Error("网络错误", err)
                return
            }
        }
        panic("【服务器类型】不存在")
    }
    
    
    消息处理

          每天坚持学习1小时Go语言,大家加油,我是彬哥,下期见!如果文章中不同观点、意见请文章下留言或者关注下方订阅号反馈!


    社区交流群:221273219
    Golang语言社区论坛 :
    www.Golang.Ltd
    LollipopGo游戏服务器地址:
    https://github.com/Golangltd/LollipopGo
    社区视频课程课件GIT地址:
    https://github.com/Golangltd/codeclass


    Golang语言社区

    相关文章

      网友评论

        本文标题:彬哥笔记 --13 Go语言 游戏服务器网关功能简单介绍 实例

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