搭建一个咸鱼之王h5小游戏

4/8/2025 咸鱼之王

# 环境配置

最好是centos7.9,其余版本的服务器未测试。然后为了测试是否可行,我用的和视频一样的流程,创建了一个宝塔然后安装的服务。

需要安装如下应用:

  • MySQL 5.7.44
  • Redis 7.2.7
  • PHP 7.2.33
  • Nginx 1.24.0
  • MongoDB 4.0.10

以及需要下载服务端https://pan.baidu.com/s/1-KQlIGViAYPPsWgiq8VTSQ 提取码: 8qyq并且需要开放以下端口82,85,10001,8889,8002

下载服务端后将文件上传到服务器,然后解压,把home目录的文件夹都放到home,www的放到www。

然后安装openresty。

cd /home/proj
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
tar -zxvf openresty-1.19.3.1.tar.gz
mv openresty-1.19.3.1 openresty
cd openresty
sudo yum install -y pcre-devel openssl-devel
./configure
make
make install
1
2
3
4
5
6
7
8
9

之后设置一下环境变量。

echo "openresty环境变量"
echo "PATH=/usr/local/openresty/bin:/usr/local/openresty/nginx/sbin:$PATH
export  LC_ALL=en_US.UTF-8
export PATH" >> ~/.bash_profile
source ~/.bash_profile
1
2
3
4
5

# 安装&启动

# 导入数据

修改数据库的root密码,改为123456,理论上用别的也可以,但是需要去替换程序里的密码,我就没换,不开放3306端口即可。执行sql下的sanguo.sql把数据导入。

mysql -uroot -p123456
create database sanguo DEFAULT CHARACTER SET utf8;
flush privileges; #
use sanguo
source /home/sql/sanguo.sql
exit;
1
2
3
4
5
6

# 替换ip

替换一下文件里的ip。

#!/bin/bash

# 定义要替换的旧IP和新IP
OLD_IP="192.168.200.99"
NEW_IP="localhost"

# 文件列表
FILES=(
    "/home/proj/common/settings.lus"
    "/home/proj/goserver/admin/assets/web/.env.development"
    "/home/proj/goserver/admin/assets/web/dist/js/app.3d18e251.js"
    "/home/proj/goserver/admin/assets/web/dist/js/highlight.js.0adc4c0f.js"
    "/home/proj/goserver/agent_admin/agent_assets/web/.env.development"
    "/home/proj/goserver/agent_admin/agent_assets/web/dist/js/app.bfd4e5a3.js"
    "/home/proj/goserver/agent_admin/agent_assets/web/dist/js/highlight.js.0adc4c0f.js"
    "/home/proj/pyapi/wxpay/app.py"
    "/www/wwwroot/game/assets/resources/import/24/241bc4ec-587f-41fa-a24d-e904c5f906c6.9f804.json"
    "/www/wwwroot/game/assets/main/index.7ab56.js"
)

# 遍历文件并替换IP地址
for file in "${FILES[@]}"; do
    if [ -f "$file" ]; then
        echo "正在处理: $file"
        sed -i "s/$OLD_IP/$NEW_IP/g" "$file"
    else
        echo "文件不存在: $file"
    fi
done

echo "替换完成!"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

然后需要改一下/www/wwwroot/game/assets/main/index.7ab56.js里的websocket的ip。

image

改成这样,自己的ip即可

"ws://"+c.gameIp+":"+c.gamePort+"/wsgame"
"ws://127.0.0.1":"+c.gamePort+"/wsgame"
1
2

替换完ip后就可以创建站点了,需要创建两个站点分别是游戏页面和后台页面。

  • 后台:IP或域名加上85端口,网站目录:/home/proj/goserver/admin/assets/web/dist
  • 游戏:IP或域名加上82端口,网站目录:www/wwwroot/game

# 启动游戏

启动游戏

cd /home/proj/apiserver
nginx -p `pwd` -c conf/nginx.conf
cd /home/proj/skynet
sh getkey.sh
1
2
3
4

将生成的key覆盖/home/proj/skynet/key.txt的内容

启动登录服务./skynet ../loginserver/prodconfig,启动游戏服务./skynet ../gameserver/prodconfig,启动管理后台服务cd /home/proj/goserver && nohup ./mygo &

然后进入后台开服,再进入游戏即可,登录后台管理页面admin/admin然后进入游戏管理->区服配置菜单,添加大区然后点开服选择开服数量保存即可。

image

然后进入游戏,访问ip:82即可

image

# 配置安卓

安卓端需要自己反编译APK然后修改文件中的192.168.200.66改为自己的ip即可使用

# 参考文献:

视频:https://www.youtube.com/watch?v=nsw6Hjon9pM 文档:https://www.4awl.net/2985.html