做一个轻量级可扩展的 Node.js 游戏服务器框架,有没有人感兴趣
做一个轻量级可扩展的 Node.js 游戏服务器框架,有没有人感兴趣
我知道 Pomelo 功能很强大,不过感觉过重了,上手不方便,有没人有兴趣一个做一个新的服务器框架,我大概列了下必要的功能,第一版将以卡牌,社交游戏为服务对象,但是架构设计将能缩放到实时性要求更高的 mmo.
Network Connection
- support different message flows such as request / response, push, broadcast
- protocol agnostic, support socket and web-socket by default
- user can change protocol with simple configuration
- other protocols such as http can be added lately without design change
Distribution
- dynamically increase / decrease server processes based on server load without the prior knowledge which server process will be bottlenecked
- In debug mode, automatically switch to module dependency for convenience
- default routing rule is least loaded
- routing rules are piped and customizable
- only need manually start the master server which will start the other servers in turn
Database solution
- default database is Mongodb to utilize its built-in memory cache, sharding for high read / write performance and scalability
Easy to use
- clearly documented
- user can start to use in one hour
- Source code obfuscation
做一个轻量级可扩展的 Node.js 游戏服务器框架,有没有人感兴趣
我知道 Pomelo 功能很强大,不过感觉过重了,上手不方便。有没有人有兴趣一起做一个新的服务器框架?我大概列了下必要的功能,第一版将以卡牌、社交游戏为服务对象,但架构设计将能扩展到实时性要求更高的 MMO。
Network Connection
我们需要支持不同消息流,例如请求/响应、推送、广播。协议无关,支持默认的 Socket 和 WebSocket。用户可以通过简单的配置更改协议,并且可以在不改变设计的情况下添加其他协议(如 HTTP)。
示例代码:
const net = require('net');
const WebSocket = require('ws');
class GameServer {
constructor(port) {
this.port = port;
this.server = null;
}
async start() {
this.server = new WebSocket.Server({ port: this.port });
this.server.on('connection', (socket) => {
console.log('Client connected');
socket.on('message', (data) => {
console.log(`Received: ${data}`);
});
});
this.server.on('listening', () => {
console.log(`Server started on port ${this.port}`);
});
}
}
// 使用示例
const gameServer = new GameServer(8080);
gameServer.start();
Distribution
需要能够动态增加或减少服务器进程,以适应不同的负载情况。在调试模式下,自动切换到模块依赖以方便调试。默认路由规则是最少负载的服务器。路由规则可以进行管道化并自定义。只需要手动启动主服务器,它会依次启动其他服务器。
示例代码:
const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
const numCPUs = os.cpus().length;
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
});
} else {
// Worker process
require('./worker'); // 这里加载实际的 worker 逻辑
}
Database Solution
默认数据库为 MongoDB,利用其内置内存缓存、分片等功能以实现高读写性能和可扩展性。
示例代码:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/game-db', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const cardSchema = new mongoose.Schema({
name: String,
type: String
});
const Card = mongoose.model('Card', cardSchema);
async function createCard(name, type) {
const card = new Card({ name, type });
await card.save();
console.log('Card created');
}
createCard('Dragon', 'Monster');
Easy to Use
确保文档清晰明了,用户能够在一小时内开始使用。源代码不应被混淆,以便于理解和调试。
通过这些功能的设计与实现,我们可以创建一个既轻量又可扩展的游戏服务器框架,适用于多种类型的游戏。如果您有兴趣参与开发,请留言告诉我!
给楼主编辑了一下, 说真的, 为什么不用中文写啊?
可以一试,我有兴趣,目前手头也正在做一个卡牌游戏。 node.js + redis + mongodb + mysql
有些东西写成中文别扭,然后开源的话是希望国外的开发者也参与进来,所以用英文较好
这个数据库方案略复杂啊
pomelo 不重吧…只是约定比较多… …熟悉了,那套约定…其实,开发起来挺顺手的…
不错啊
要如何分工呢?
不错,有兴趣~
我也在自己写卡牌 就简单的mysql + express 楼主啥时候开始写 一起研究
要实现一个轻量级且可扩展的 Node.js 游戏服务器框架,可以考虑使用 Koa 或 Express 搭建基础服务器,并结合 WebSocket 进行通信。下面是一个简单的示例代码,展示如何搭建这样一个框架:
示例代码
const Koa = require('koa');
const http = require('http');
const SocketIO = require('socket.io');
const app = new Koa();
const server = http.createServer(app.callback());
const io = new SocketIO(server);
// 支持不同类型的消息流(请求/响应、推送、广播)
io.on('connection', (socket) => {
console.log('a user connected');
// 请求/响应模式
socket.on('request', (data, callback) => {
const response = `Received: ${data}`;
callback(response);
});
// 推送消息
setInterval(() => {
socket.emit('push', { message: 'Hello World' });
}, 5000);
// 广播消息
socket.on('broadcast', (data) => {
io.emit('broadcast', data);
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
server.listen(3000, () => {
console.log('Server listening on port 3000');
});
解释
-
网络连接:
- 使用
SocketIO
来支持不同的消息流(请求/响应、推送、广播)。 - 默认支持 WebSocket 和 HTTP 协议。
- 用户可以通过简单配置更改协议。
- 使用
-
分布处理:
- 在生产环境中,可以通过进程管理工具如 PM2 动态增加或减少服务器进程。
- 调试模式下,自动切换到模块依赖。
-
数据库解决方案:
- 默认使用 MongoDB,利用其内存缓存和分片机制,以提高读写性能和可扩展性。
-
易用性:
- 提供清晰的文档,使用户能够在一小时内开始使用。
- 通过 Koa 和 SocketIO 提供简洁的 API。
如果你有兴趣参与这样的项目,可以贡献代码、提供建议或者测试框架的不同部分。这样不仅可以帮助开发社区,也能提升自己的技能。