达达麻将网络通讯
目录
达达麻将服务器
1: express: node.js里面高效轻量级的web框架; 达达麻将采用http协议的webserver;
2: socket.io: 是node.js的网络模块,它把网络通讯都抽象成了事件。
3: account_server: 账号服务器,基于的通讯框架是express (1个)
4: hall_server:大厅服务器基于的通讯框架是express (2个)
5: mj_server: 游戏服务器,基于的通讯框架是socket.io/express服务服务(1);
6: 客户端调用API函数,来完成http请求;
express使用
1: 安装express模块;
npm install express
2: 基于express创建一个实例;
var express = require("express");
var app = express();
app.listen(8090);
//http协议的一种操作模式,可以使用它来发送数据
app.get("/login", function(req, res){
res.send("----ok----");
});
//启动node服务器express框架
node webserver.js
//访问测试,在浏览器中输入
http://127.0.0.1:8090/login
输出----ok----
3: 配置好webserver的端口;
var express = require("express");
var app = express();
app.listen(8090);
//http协议的一种操作模式,可以使用它来发送数据
app.get("/login", function(req, res){
res.send("----ok----");
//带参数的请求和服务器的接收输出
console.log(req.query);
});
4: 配置好静态地址;
5: 配置好http get服务;
6: 配置好http post服务;
7: 查看达达麻将account服务器的基本的结构;
//G:\..\..\js_ddmj\server\server\account_server\app.js
var db = require('../utils/db');
var configs = require(process.argv[2]);
//init db pool.
//连接数据库(初始化)
db.init(configs.mysql());
//
//获得账号服务器account_server的配置文件
var config = configs.account_server();
var as = require('./account_server');
as.start(config);
//启动服务器
var dapi = require('./dealer_api');
dapi.start(config);
-------------------------------------------
//G:\..\..\js_ddmj\server\server\account_server\account_server.js
var crypto = require('../utils/crypto');
var express = require('express');
var db = require('../utils/db');
var http = require("../utils/http");
//生成一个基于express的对象,来完成web服务,作为web服务器
//通过http协议,和客户端交互数据
var app = express();
var hallAddr = "";
function send(res,ret){
var str = JSON.stringify(ret);
res.send(str)
}
var config = null;
//启动account server账号服务器
exports.start = function(cfg){
config = cfg;
hallAddr = config.HALL_IP + ":" + config.HALL_CLIENT_PORT;
app.listen(config.CLIENT_PORT);
console.log("account server is listening on " + config.CLIENT_PORT);
}
//----函数功能----
//等待客户端的请求,URL----register注册
app.get('/register',function(req,res){
//等待客户端的请求,URL----get_version获取版本
app.get('/get_version',function(req,res){
//等待客户端的请求,URL----get_serverinfo获取服务器信息
app.get('/get_serverinfo',function(req,res){
//等待客户端的请求,URL----guest游客登录
app.get('/guest',function(req,res){
//等待客户端的请求,URL----微信登录的获取auth
app.get('/auth',function(req,res){
//微信登录相关信息
var appInfo = {
//微信登录获取access_token
function get_access_token(code,os,callback){
//微信登录,获取state_info
function get_state_info(access_token,openid,callback){
//微信登录,获取wechat_auth
app.get('/wechat_auth',function(req,res){
//等待客户端的请求,URL----base_info获取基础信息
app.get('/base_info',function(req,res){
------------------------------------------------------------------------------
//G:\..\..\js_ddmj\server\server\configs_local.js
var HALL_IP = "127.0.0.1";
var HALL_CLIENT_PORT = 9001;
var HALL_ROOM_PORT = 9002;
var ACCOUNT_PRI_KEY = "^&*#$%()@";
var ROOM_PRI_KEY = "~!@#$(*&^%$&";
var LOCAL_IP = 'localhost';
exports.mysql = function(){
return {
HOST:'127.0.0.1',
USER:'root',
PSWD:'123456',
DB:'dd_mj',
PORT:3306,
}
}
//账号服配置
exports.account_server = function(){
return {
CLIENT_PORT:9000,
HALL_IP:HALL_IP,
HALL_CLIENT_PORT:HALL_CLIENT_PORT,
ACCOUNT_PRI_KEY:ACCOUNT_PRI_KEY,
//
DEALDER_API_IP:LOCAL_IP,
DEALDER_API_PORT:12581,
VERSION:'20161227',
APP_WEB:'http://fir.im/2f17',
};
};
G:\..\..\js_ddmj\server\server\hall_server\app.js
var client_service = require("./client_service");
var room_service = require("./room_service");
var configs = require(process.argv[2]);
//获取大厅的配置文件
var config = configs.hall_server();
var db = require('../utils/db');
//初始化数据库
db.init(configs.mysql());
//启动客户端的大厅web服务器的请求
client_service.start(config);
//启动客户端的房间web服务器的请求
room_service.start(config);
-----------------------------------------------------------
G:\..\..\js_ddmj\server\server\hall_server\client_service.js
//http协议下的数据交互
var app = express();
var config = null;
//配置好响应请求:登录到大厅服务器;
app.get('/login',function(req,res){
//创建一个用户
app.get('/create_user',function(req,res){
//创建一个房间
app.get('/create_private_room',function(req,res){
//进入一个房间
app.get('/enter_private_room',function(req,res){
//获取历史记录
app.get('/get_history_list',function(req,res){
//获取游戏房间
app.get('/get_games_of_room',function(req,res){
//获取游戏房间详细信息
app.get('/get_detail_of_game',function(req,res){
//获取玩家状态
app.get('/get_user_status',function(req,res){
//获取消息
app.get('/get_message',function(req,res){
//查询服务器是否在线
app.get('/is_server_online',function(req,res){
//启动函数
exports.start = function($config){
config = $config;
app.listen(config.CLEINT_PORT);
console.log("client service is listening on port " + config.CLEINT_PORT);
};
//设置跨域访问
app.all('*', function(req, re