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

来源:互联网 发布:程序员修炼之道 mobi 编辑:程序博客网 时间:2024/05/21 11:00


leaf开源游戏服务器源码


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


项目入口
QQ截图20171107162241.png 

main.go
  1. package main

  2. import (
  3.         "server/conf"
  4.         "server/game"
  5.         "server/gate"
  6.         "server/login"

  7.         "github.com/name5566/leaf"
  8.         lconf "github.com/name5566/leaf/conf"
  9. )

  10. func main() {

  11.         // 加载配置
  12.         lconf.LogLevel = conf.Server.LogLevel
  13.         lconf.LogPath = conf.Server.LogPath
  14.         lconf.LogFlag = conf.LogFlag
  15.         lconf.ConsolePort = conf.Server.ConsolePort
  16.         lconf.ProfilePath = conf.Server.ProfilePath

  17.         // 运行
  18.         leaf.Run(
  19.                 game.Module,
  20.                 gate.Module,
  21.                 login.Module,
  22.         )
  23. }
复制代码
从主函数分析
// 加载配置
  1. lconf.LogLevel = conf.Server.LogLevel
  2. lconf.LogPath = conf.Server.LogPath
  3. lconf.LogFlag = conf.LogFlag
  4. lconf.ConsolePort = conf.Server.ConsolePort
  5. lconf.ProfilePath = conf.Server.ProfilePath
复制代码
加载配置文件,配置文件在哪里?
QQ截图20171107162506.png 

json.go 源码
  1. package conf

  2. import (
  3.         "encoding/json"
  4.         "io/ioutil"

  5.         "github.com/name5566/leaf/log"
  6. )

  7. // 服务器结构
  8. var Server struct {
  9.         LogLevel    string
  10.         LogPath     string
  11.         WSAddr      string
  12.         CertFile    string
  13.         KeyFile     string
  14.         TCPAddr     string
  15.         MaxConnNum  int
  16.         ConsolePort int
  17.         ProfilePath string
  18. }

  19. // 加载服务器配置
  20. func init() {
  21.         data, err := ioutil.ReadFile("conf/server.json")
  22.         if err != nil {
  23.                 log.Fatal("%v", err)
  24.         }
  25.         err = json.Unmarshal(data, &Server)
  26.         if err != nil {
  27.                 log.Fatal("%v", err)
  28.         }
  29. }
复制代码
从源码中可以看到 配置文件是加载conf/server.json的json的文件

QQ截图20171107162653.png 

配置文件的json结构
  1. // 服务器结构
  2. var Server struct {
  3.         LogLevel    string
  4.         LogPath     string
  5.         WSAddr      string
  6.         CertFile    string
  7.         KeyFile     string
  8.         TCPAddr     string
  9.         MaxConnNum  int
  10.         ConsolePort int
  11.         ProfilePath string
  12. }
复制代码

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


原文地址 leaf开源服务器-分析1

原创粉丝点击