从零开始搭建一个多人合作的web应用
in 工具 with 0 comment

从零开始搭建一个多人合作的web应用

in 工具 with 0 comment

前不久在v2上看到一个好玩的玩意儿,由于这个项目是使用go写的,
同时涉及到了一些lib库。我这里就用docker来安装了(主要还是C盘实在快装不下了2333~)

首先启动一个纯golang的docker容器,同时映射1989端口:
(把/data映射到物理机的/docker/gbdotlive_data是为了更方便的copy文件进去)

docker run --name godemo -it -v /docker/gbdotlive_data:/data -p 1989:1989 golang bash

首先下载git项目,我懒得装git就直接下压缩包了

root@bedf0426add5:/go# wget https://github.com/HFO4/gameboy.live/archive/master.zip
root@bedf0426add5:/go# mv master.zip /data
root@bedf0426add5:/# cd /data
root@bedf0426add5:/data#

# 当我们尝试解压的时候报错了,提示unzip这个命令没找到
root@bedf0426add5:/data# unzip master.zip -d gameboy
bash: unzip: command not found

# 用apt-get安装unzip
# 由于apt服务器在国外,所以下载会很慢。可以参考下面的文章切换apt源,这里就不展示了。
root@bedf0426add5:/data# apt-get update
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://deb.debian.org/debian buster InRelease [121 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [248 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7906 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [7856 B]
Fetched 7731 kB in 13min 45s (9371 B/s)
Reading package lists... Done
root@bedf0426add5:/data#
root@bedf0426add5:/data# apt-get install unzip
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  zip
The following NEW packages will be installed:
  unzip
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 172 kB of archives.
After this operation, 580 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 unzip amd64 6.0-23+deb10u1 [172 kB]
Fetched 172 kB in 6s (29.9 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package unzip.
(Reading database ... 15410 files and directories currently installed.)
Preparing to unpack .../unzip_6.0-23+deb10u1_amd64.deb ...
Unpacking unzip (6.0-23+deb10u1) ...
Setting up unzip (6.0-23+deb10u1) ...
Processing triggers for mime-support (3.62) ...
root@bedf0426add5:/data# unzip master.zip -d gameboy
root@bedf0426add5:/data# ls
gameboy master.zip
root@bedf0426add5:/data# cd gameboy
root@bedf0426add5:/data/gameboy#

切换apt-get源的方法: 传送门

ok,到这文件是下完了,预装一些需要的lib库。

# 安装过程的输出就不粘贴出来了,没啥意思。照着这个列表装就行了
# 有些lib库安装很慢,我这一套装完差不多花了一个多小时。
root@bedf0426add5:/data/gameboy# apt-get install libasound2-dev
root@bedf0426add5:/data/gameboy# apt-get install libgl1-mesa-dev
root@bedf0426add5:/data/gameboy# apt-get install libxcursor-dev
root@bedf0426add5:/data/gameboy# apt-get install libghc-x11-dev
root@bedf0426add5:/data/gameboy# apt-get install libxrandr-dev
root@bedf0426add5:/data/gameboy# apt-get install libxi-dev

一切安装完之后就可以正式编译了

root@bedf0426add5:/data/gameboy#  ls
gameboy.live-master
# 进入目录
root@bedf0426add5:/data/gameboy# cd gameboy.live-master
root@bedf0426add5:/data/gameboy/gameboy.live-master#
# 编译
root@bedf0426add5:/data/gameboy/gameboy.live-master# go build -o gbdotlive main.go

有时候会出现这样的结果:

root@bedf0426add5:/data/gameboy/gameboy.live-master# go build -o gbdotlive main.go
go: fyne.io/fyne@v1.0.1: Get "https://proxy.golang.org/fyne.io/fyne/@v/v1.0.1.mod": dial tcp 172.217.27.145:443: i/o timeout

# 不要慌,切换下镜像地址就行
root@bedf0426add5:/data/gameboy/gameboy.live-master# go env -w GOPROXY=https://goproxy.cn,direct
# 重新编译
root@bedf0426add5:/data/gameboy/gameboy.live-master# go build -o gbdotlive main.go
go: downloading fyne.io/fyne v1.0.1
go: downloading github.com/satori/go.uuid v1.2.0
go: downloading github.com/faiface/pixel v0.8.0
go: downloading github.com/faiface/beep v1.0.1
go: downloading github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946
go: downloading github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1
go: downloading github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
go: downloading github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7
go: downloading github.com/pkg/errors v0.8.1
go: downloading github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380
go: downloading golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f
go: downloading github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
go: downloading github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff
go: downloading github.com/srwiley/oksvg v0.0.0-20190105194046-ccbc7673cdf3
go: downloading github.com/srwiley/rasterx v0.0.0-20181219215540-696f7edb7a7e
go: downloading golang.org/x/net v0.0.0-20181220203305-927f97764cc3
go: downloading golang.org/x/text v0.3.0
go: downloading github.com/hajimehoshi/oto v0.3.1
root@bedf0426add5:/data/gameboy/gameboy.live-master# 

# 拷贝gbdotlive到/data目录下
root@bedf0426add5:/data/gameboy/gameboy.live-master# cp gbdotlive /data/gbdotlive
root@bedf0426add5:/data/gameboy/gameboy.live-master# cd /data
root@bedf0426add5:/data#

编译成功之后通过映射的/docker/gbdotlive_data目录导入gb游戏。

运行./gbdotlive -S -r "Pokemon.gb"命令,之后访问IP:1989/image就能看到效果啦。

root@bedf0426add5:/data# ./gbdotlive -S -r "Pokemon.gb"
2020/11/16 22:30:55 [Core] Loading rom file...
2020/11/16 22:30:55 [Core] 1048576 Bytes rom loaded
2020/11/16 22:30:55 [Core] Loading ram file...
2020/11/16 22:30:55 [Cartridge] Game title: POKEMON BLUE
2020/11/16 22:30:55 [Cartridge] CGB mode: false
2020/11/16 22:30:55 [Cartridge] Cartridge type: MBC3+RAM+BATTERY
2020/11/16 22:30:55 [Cartridge] ROM bank number: 64 (0KBytes)
2020/11/16 22:30:55 [Cartridge] RAM bank number: 0 (0KBytes)
2020/11/16 22:30:55 [Core] Start to initialize memory...
2020/11/16 22:30:55 [Memory] Load first 32KByte of rom data into memory
2020/11/16 22:30:55 [Core] Initialize CPU flags and registers
2020/11/16 22:30:55 [Display] Initialize static image display

图1

其他的命令使用方法可以参考作者的项目主页:HFO4/gameboy.live

另外分享一个自己写的VUE版h5操作界面

2.png

gameboy.zip

把其中192.168.0.2修改成自己的IP地址即可。

let app = new Vue({
    el: "#app",
    data: {
        host: "http://192.168.0.2:1989",
        screenUrl: "http://192.168.0.2:1989/image"
    },
    created () {
        let IntervalFunc = () => {
            this.screenUrl = 'http://192.168.0.2:1989/image?' + (new Date).getTime()
        }
        setInterval(IntervalFunc, 200)
    }
    ...
}

拓展:多人协作 - 在直播中玩游戏

之前看到了这个新闻 Twitch Plays Pokemon,讲一群网民在实况频道的聊天室输入游戏指令来控制实况中的游戏。

想了下好像也能做,然后写了一个demo

3.jpg

但是因为直播延迟有点久(大约有5-8秒)又没有粉丝,所以挂了两天也没出新手村(笑~

其他问题

有时候go build会出现报错,可以通过apt-file来查看未安装的依赖库

root@bedf0426add5:/data# sudo apt-get install libx11-dev
root@bedf0426add5:/data# sudo apt-get install apt-file
root@bedf0426add5:/data# sudo apt-file update

# 搜索报错的内容
root@bedf0426add5:/data# apt-file search XXXX.h
libxt-dev: usr/include/X11/Intrinsic.h

# 则安装libxt-dev库即可解决报错
sudo apt-get install libxt-dev

参考资料

ubuntu切换国内镜像源,加速apt-get下载速度
Golang设置国内代理
HFO4/gameboy.live: A basic gameboy emulator with terminal Cloud Gaming support
【解决】fatal error: X11/XXXX.h: No such file or directory

Comments are closed.