Skip to main content

Command Palette

Search for a command to run...

Make a multiplayer card game - Episode 1 | Create a simple server and client from scratch

Published

In the coming weeks, I'll be updating a series of tutorials on making a multiplayer online card games base on the rule of the most popular Chinese card game “Doudizhu”, which means battle with landlord. I divide this series in many small part to make it easy for most friends. If you know a little javascript, all the better.

Parts including:

  • create a simple server and client
  • play cards in terminal with bots
  • communicate with server by protobuf
  • play cards in terminal with other players
  • create 2D graphical interface with Cocos Creator
  • create 2D graphical interface with React
  • create 3D graphical interface with Three.js
  • add physical lib ammo.js to add a tiny game
  • connect to Web3 world

open your terminal, input node -v, if something go wrong, please install node.js first https://nodejs.org.

directory structure : demo0-document-tree.jpg

create a server, server/index.js:

 const net = require('net');

const server = net.createServer((socket) => {
    socket.on('data', (data) => {
        handlerData(socket, data)
    });
});

function handlerData(socket, data) {
    send(socket, {'msg':'Welcome, friend.'});
}

function send(socket, data) {
    let bufferData = Buffer.from(JSON.stringify(data));
    socket.write(bufferData);
}

server.listen(8080, () => {
    console.log("server listening on 127.0.0.1:8080")
});

create client, client/index.js:

const net = require('net');
const socket = new net.Socket();

socket.connect({
    host: '127.0.0.1',
    port: 8080
}, onConnected);

socket.on('data', (buffer) => {
    decode(buffer);
})
socket.on('error', (buffer) => {
    console.log(buffer);
});

function onConnected() {
    startGame();
}

function decode(data) {
    console.log(JSON.parse(data).msg);
}

function request(data) {
    const bufferData = Buffer.from(JSON.stringify({ 'msg': data }));
    socket.write(bufferData);
}

function startGame() {
    request('Hello, world!')
}

run :

cd server

node index.js

cd client

node index.js

result:

demo0-cmd-result.png

This episode is end here, the complete demo can be found on https://github.com/lizhiyu-me/Make-a-multiplayer-card-game/tree/episode1

Any problems, discussion is welcome.

In the next section, we will add the logic of Doudizhu game, and you will have completed a playable game in your terminal.

Thanks for your reading.

More from this blog

如何将静态页面部署到Github Page,并绑定自定义域名

在仓库主页选择setting 点击pages 选择分支branch 输入自定义域名custom domain(设置好后点击save,系统将自动在项目根目录生成CNAME文件,文件内容为设置的当前域名) 在域名提供商处,添加DNS设置(以阿里云为例) 配置CNAME 配置IP,创建一个A类记录指向185.199.108.153 github将根据访问域名路由项目仓库(仓库名称为iddz.fun,并且根目录有CNAME文件) 最后,通过访问自定义域名,完成静态网站的部署...

Oct 4, 2023
如何将静态页面部署到Github Page,并绑定自定义域名

熟悉熟悉官方文档,逐步深入Babylon.js

文档的组织结构 通过前一篇文章的了解,我们会对Babylon.js有了一个大致的了解,整个文档主要是想带你逐步深入掌握这个Babylon.js所提供的所有内容。 内容主要分为一个概览和9个主要部分,这些部分包含章节,还有API详解和强大的文档和playground搜索功能。 - 1.Babylon.js特性 - Babylon.js是一个功能完备的游戏和渲染引擎,具有广泛的特性。这个部分将带你了解这些特性,并帮助你编码和使用它们。- 2.将Babylon.js添加到你的Web项目中 - 有多种...

Sep 25, 2023
熟悉熟悉官方文档,逐步深入Babylon.js

Lizhiyu's Blog

33 posts

Bringing the world closer together through play