如何在Ubuntu下安装npm及websocket-bench

[b]1. 更新package库[/b]
> apt-get update

[b]2. 安装nodejs[/b]
> sudo apt-get install nodejs
[b]
3. 因为npm需要访问目录/usr/bin/node下的配置文件,但是nodejs的配置文件又是安装到/usr/bin/nodejs,所以我们这里需要对这两个文件做一个映射。[/b]
不映射的话,有时候在查看npm版本时,会报:/usr/bin/env:node No such file or directory的错误
> ln -s /usr/bin/nodejs /usr/bin/node

[b]4. 安装npm[/b]
> sudo apt-get install npm
[b]
5. 安装websocket-bench[/b]
> sudo npm install -g websocket-bench
[b]
6. 若需要修改socket.io协议,位置在./usr/local/lib/node_modules/websocket-bench/lib/workers/socketioworker.js第18行[/b]
将:
var client = io.connect(this.server, { 'force new connection' : true});
修改为:
var client = io.connect(this.server, { 'force new connection' : true,'transports': ['websocket', 'polling']});

[b]7. 自定义generator.js[/b]

/*global module, require*/

/*var logger = require('opt.log');*/

module.exports = {

/**
* Before connection (just for faye)
* @param {client} client connection
*/
beforeConnect : function (client) {

},

/**
* on socket io connect
* @param {client} client connection
* @param {done} callback function(err) {}
*/
onConnect : function (client, done) {

var sid;
client.on("message",function(message,ackServerCallback){
if (ackServerCallback) {
// send ack data to push server
ackServerCallback(message);
}
console.log("Channel:"+message.channel + "---Content: "+ message.content)

});
client.emit('authorize', {"appkey":"43cbbd30-1753-4fdb-8e45-57d86dde378f","userId":"75115","xueting":"","startMillis":1470729381064,"artifactVersion":"0.7"},function(data){
sid = data.sid;
client.emit('subscribe',{"channel":"demo_channel","sid":sid});

});
//done();
},

/**
* send a message
* @param {client} client connection
* @param {done} callback function(err) {}
*/
sendMessage : function (client, done) {
//logger.error('Not implement method sendMessage in generator');
// Your logic
//client.emit('test', { hello: 'world' });
//client.publish('/test', { hello: 'world' });
done();
}
};



[b]8. 运行websocket-bench[/b]
> websocket-bench -a 10 -g ./generator.js localhost:9000
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值