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