您的位置:首页 > 其它

leaf开源服务器第一节-分析项目结构

2017-11-07 16:21 246 查看
leaf开源游戏服务器源码

首先,leaf开源服务器-大多数网上文章介绍都是关于游戏服务器的;其实总体框架来说,做H5聊天服务器也可以的;同时leaf总体设计来说,个人认为此框架不仅仅适合游戏。

项目入口


 

main.go

package main

import (

        "server/conf"

        "server/game"

        "server/gate"

        "server/login"

        "github.com/name5566/leaf"

        lconf "github.com/name5566/leaf/conf"

)

func main() {

        // 加载配置

        lconf.LogLevel = conf.Server.LogLevel

        lconf.LogPath = conf.Server.LogPath

        lconf.LogFlag = conf.LogFlag

        lconf.ConsolePort = conf.Server.ConsolePort

        lconf.ProfilePath = conf.Server.ProfilePath

        // 运行

        leaf.Run(

                game.Module,

                gate.Module,

                login.Module,

        )

}

复制代码
从主函数分析
// 加载配置

lconf.LogLevel = conf.Server.LogLevel

lconf.LogPath = conf.Server.LogPath

lconf.LogFlag = conf.LogFlag

lconf.ConsolePort = conf.Server.ConsolePort

lconf.ProfilePath = conf.Server.ProfilePath

复制代码
加载配置文件,配置文件在哪里?


 

json.go 源码

package conf

import (

        "encoding/json"

        "io/ioutil"

        "github.com/name5566/leaf/log"

)

// 服务器结构

var Server struct {

        LogLevel    string

        LogPath     string

        WSAddr      string

        CertFile    string

        KeyFile     string

        TCPAddr     string

        MaxConnNum  int

        ConsolePort int

        ProfilePath string

}

// 加载服务器配置

func init() {

        data, err := ioutil.ReadFile("conf/server.json")

        if err != nil {

                log.Fatal("%v", err)

        }

        err = json.Unmarshal(data, &Server)

        if err != nil {

                log.Fatal("%v", err)

        }

}

复制代码
从源码中可以看到 配置文件是加载conf/server.json的json的文件


 

配置文件的json结构

// 服务器结构

var Server struct {

        LogLevel    string

        LogPath     string

        WSAddr      string

        CertFile    string

        KeyFile     string

        TCPAddr     string

        MaxConnNum  int

        ConsolePort int

        ProfilePath string

}

复制代码
先分析到这里,下节我们添加json文件,后启动server,测试下本地可以连接不。

原文地址 leaf开源服务器-分析1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: