pomelo 环境
操作系统和node pomelo版本
ubuntu16.04 64bit
node 5.8.0 ($node -v)
npm 3.7.3 ($npm -v)
pomelo 1.2.3 ($pemelo –version)
npm安装的模块
注意模块的版本,比如
generic-pool 我的安装命令是
$npm install generic-pool@"<3.0" -g
因为generic-pool 3.0 在之前的基础上有很大的改变
pomelo建立项目
pomelo初始项目 需要选择websocket,socke.io(按照提示选择即可)
- package.json
每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称、版本、许可证等元数据)。npm install命令根据这个配置文件,自动下载所需的模块,也就是配置项目所需的运行和开发环境。
例如game-server文件夹下的 package.json
{
"name":"pemelopro1","version":"0.0.1","private":false,"dependencies":{ "pomelo":"1.2.3" } }
package.json 更多参考
http://javascript.ruanyifeng.com/nodejs/packagejson.html#
- $sh npm-install.sh
pomelo 新建项目后,需要intall模块 即使用如下命令
sh npm-install.sh
可以查看npm-install.sh的内容,如下
cd ./game-server && npm install -d
echo '============ game-server npm installed ============'
cd ..
cd ./web-server && npm install -d
echo '============ web-server npm installed ============'
MysqL操作,注意自己的模块版本
game-server/app 目录下新建两个文件
dao/MysqL/dao-pool.js
var _poolModule = require('generic-pool');
/* * Create MysqL connection pool. */
var createMysqLPool = function(app) {
var MysqLConfig = app.get('MysqL');
var pool = _poolModule.Pool({
name: 'MysqL',create: function(callback) {
var MysqL = require('MysqL');
var client = MysqL.createConnection({
host: MysqLConfig.host,user: MysqLConfig.user,password: MysqLConfig.password,database: MysqLConfig.database
});
callback(null,client);
},destroy: function(client) {
client.end();
},max: 10,idleTimeoutMillis : 30000,log : false
});
return pool;
};
exports.createMysqLPool = createMysqLPool;
var sqlclient = module.exports;
var _pool;
var NND = {};
/* * Init sql connection pool * @param {Object} app The app for the server. */
NND.init = function(app){
_pool = (require('./dao-pool')).createMysqLPool(app);
};
/** * Excute sql statement * @param {String} sql Statement The sql need to excute. * @param {Object} args The args for the sql. * @param {fuction} cb Callback function. * */
NND.query = function(sql,args,cb){
_pool.acquire(function(err,client) {
if (!!err) {
console.error('[sqlqueryErr] '+err.stack);
return;
}
client.query(sql,function(err,res) {
_pool.release(client);
cb(err,res);
});
});
};
/** * Close connection pool. */
NND.shutdown = function(){
_pool.destroyAllNow();
};
/** * init database */
sqlclient.init = function(app) {
if (!!_pool){
return sqlclient;
} else {
NND.init(app);
sqlclient.insert = NND.query;
sqlclient.update = NND.query;
sqlclient.delete = NND.query;
sqlclient.query = NND.query;
return sqlclient;
}
};
/** * shutdown database */
sqlclient.shutdown = function(app) {
NND.shutdown(app);
};
{
"development": { "host" : "127.0.0.1","port" : "3306","database" : "mydb","user" : "root","password" : "123456" },"production": { "host" : "127.0.0.1","password" : "123456" } }
app.loadConfig("MysqL",app.getBase() + "/config/MysqL.json"); // 添加配置
var dbclient = require("./app/dao/MysqL/MysqL.js").init(app); // 初始化dbclient
app.set("dbclient",dbclient);// dbclient 为外部数据库接口,app.get("dbclient") 来使用
完整的app.js
var pomelo = require('pomelo');
/** * Init app for client. */
var app = pomelo.createApp();
app.set('name','pemelopro1');
app.loadConfig("MysqL",app.getBase() + "/config/MysqL.json"); // 添加配置
// app configuration
app.configure('production|development','connector',function(){
app.set('connectorConfig',{
connector : pomelo.connectors.sioconnector,//websocket,htmlfile,xhr-polling,jsonp-polling,flashsocket
transports : ['websocket'],heartbeats : true,closeTimeout : 60,heartbeatTimeout : 60,heartbeatInterval : 25
});
var dbclient = require("./app/dao/MysqL/MysqL.js").init(app); // 初始化dbclient
app.set("dbclient",dbclient);// dbclient 为外部数据库接口,app.get("dbclient") 来使用
});
// start app
app.start();
process.on('uncaughtException',function (err) {
console.error(' Caught exception: ' + err.stack);
});
{username:'zhang',password:'123'}
<@H_18_403@script type="text/javascript"> var pomelo = window.pomelo; var host = "127.0.0.1"; var port = "3010"; function @H_18_403@show() { pomelo.init({ host: host,port: port,log: true },function() { pomelo.request("connector.entryHandler.entry",{username:'zhang',password:'123'},function(data) { alert(data.msg); }); }); } </@H_18_403@script>
直接修改Handler.prototype.entry 方法
Handler.prototype.entry = function(msg,session,next) {
//next(null,{code: 200,msg: 'game server is ok.'});
var username = msg.username;
var password = msg.password
var clientMsg ="";
clientMsg += 'username:';
clientMsg += username;
clientMsg += 'password:';
clientMsg += password;
console.log(clientMsg);
var sql = "insert into `User`(`username`,`password`) values(?,?);";
var args = [username,password];
var dbclient = this.app.get('dbclient');//获取全局MysqL client
dbclient.query(sql,function (err,res) {//执行sql语句 函数insert和query等
console.log("......................");
console.log(err + " " + JSON.stringify(res));
console.log("......................");
if (err) {
next(null,{code: 200,msg: 'db error'});
}
else {
next(null,msg: clientMsg });
}
});
//next(null,result);
//next(null,msg: clientMsg});
};
- 运行截图
baidu搜出来的(基本都这样,否则需要github上看文档),写的不错,不过没有交代pomelo的环境,并且模块版本不一样,也是不行的,注意我上面方法的区别之处。
- 建议安装node-dev模块
$ npm install node-dev -g
z在运行web-server时
原来的 node app.js 换成
$node-dev --debug app.js
当有js文件修改后 自动去重启nodejs进程,这样就不用每次ctrl+c终止,然后再重启了 而且不仅支持js文件,.json .node .coffee 文件格式都支持