项目地址:
**https://github.com/dwg255/fish**
说明:go语言 游戏,go捕鱼,高性能游戏服务端
golang开发的服务端编程简单,执行高效,有效利用多核资源,游戏server端为golang典型的应用场景之一。项目大量使用goroutine及channel,命名及注释清晰。
主要依赖包:
- github.com/astaxie/beego 主要用到其logs包,监听http端口及处理请求使用golang自带的包 net/http
- github.com/gorilla/websocket websocket连接及收发消息
项目目录说明:
- common 公共包,*包含全局配置、生成全部牌型的方法、计算牌型以及客户端协议常量*
- api/thrift 定义游戏服务器和账号服务器rpc通讯协议
- conf 定义账号、大厅、游戏服务器的redis等配置,以及出鱼的路劲配置文件`*监听的端口、日志级别、日志路径、存储路径等*`
- tools 公共工具,包括aes加密算法,thrift rpc调用工具,snowflake生成工具
- account `*账号中心服务*` 通过thrift rpc 提供账号注册,认证等服务
- main 入口目录
- common
- service
- client 捕鱼客户端静态资源目录
- hall 捕鱼大厅
- main 入口目录
- common
- controllers 控制器目录
- router 路由目录
- game * `*处理游戏逻辑*`
- main 入口目录
- common
- controllers 控制器目录
- router 路由目录
- service 逻辑目录
```go
func init() { //生成pid文件,保存pid
pidFileName := "pid"
fileInfo, err := os.Stat(pidFileName)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(pidFileName, os.ModePerm)
fileInfo, _ = os.Stat(pidFileName)
}
}
if fileInfo.IsDir() {
pid := os.Getpid()
pidFile, err := os.OpenFile(pidFileName+"/landlord.pid", os.O_RDWR|os.O_CREATE, 0766)
if err != nil {
logs.Error("open pidFile [%s] error :%v", pidFileName, err)
return
}
err = pidFile.Truncate(0) //清空数据
_, err = io.WriteString(pidFile, strconv.Itoa(pid))
if err != nil {
logs.Error("write pid error :%v", err)
}
err = pidFile.Close()
if err != nil {
logs.Error("close pid file err: %v", err)
}
} else {
logs.Error("pidFile [%s] is exists and not dir", pidFileName)
}
}
```
*更多代码详见 GitHub* -service -room.go 文件内包含压测代码,已通过压测并稳定运行半年左右。
运行步骤:
1.下载源码:
```
git clone https://github.com/dwg255/fish
```
2.编译:
```
cd fish\
go build -o account.exe account\main\main.go account\main\init.go account\main\config.go
go build -o hall.exe hall\main\main.go hall\main\init.go hall\main\config.go
go build -o fish.exe game\main\main.go game\main\init.go game\main\config.go
```
3.解压客户端:
tar -zxvf fish.tar.gz /var/www/html/client/fish
4.配置nginx:
```
server {
listen 80;
server_name fish.com;
charset utf8;
index index.html index.htm;
location /qq {
add_header Access-Control-Allow-Origin *;
proxy_set_header X-Target $request_uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9000;
}
location / {
root /var/www/html/client/fish;
add_header Access-Control-Allow-Origin *;
expires 7d;
}
}
```
```
配置文件位置 /common/conf 内含redis配置和qq第三方登录配置,请自行修改。
```
5.在线示例:
http://fish.blzz.shop
------
License
This project is released under the terms of the MIT license. See [LICENSE](LICENSE) for more
information or see https://opensource.org/licenses/MIT.
------
5.运行截图:
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/1.jpg)
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/2.jpg)
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/3.jpg)
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/4.jpg)
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/5.jpg)
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/6.jpg)
![img](https://raw.githubusercontent.com/dwg255/fish/master/client/qg_副本.jpg)
有疑问加站长微信联系(非本文作者))