`

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

阅读更多
1. 更新package库
> apt-get update

2. 安装nodejs
> sudo apt-get install nodejs

3. 因为npm需要访问目录/usr/bin/node下的配置文件,但是nodejs的配置文件又是安装到/usr/bin/nodejs,所以我们这里需要对这两个文件做一个映射。

不映射的话,有时候在查看npm版本时,会报:/usr/bin/env:node No such file or directory的错误
> ln -s /usr/bin/nodejs /usr/bin/node

4. 安装npm
> sudo apt-get install npm

5. 安装websocket-bench

>  sudo npm install -g websocket-bench

6. 若需要修改socket.io协议,位置在./usr/local/lib/node_modules/websocket-bench/lib/workers/socketioworker.js第18行

将:
var client = io.connect(this.server, { 'force new connection' : true});
修改为:
var client = io.connect(this.server, { 'force new connection' : true,'transports': ['websocket', 'polling']});

7. 自定义generator.js
/*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();
  }
};



8. 运行websocket-bench
>  websocket-bench -a 10 -g ./generator.js localhost:9000
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics