fayeserver

fayeserver

APACHE-2.0 License

Downloads
9
Stars
7
Committers
1

fayeserver

faye server for nodejs

安装

在终端里输入

git clone https://github.com/i5ting/fayeserver.git
npm install
npm start

默认端口是4567,如果需要,请修改index.js文件。

npm run publish	

用法

在网页里

<script type="text/javascript" 
        src="http://at35.com:4567/faye/client.js">
        </script>
<script type="text/javascript">
  var client = new Faye.Client('http://at35.com:4567/faye', {
  	timeout : 120,
		retry		: 5
	});
	
	Logger = {
	  incoming: function(message, callback) {
	    console.log('incoming', message);
	    callback(message);
	  },
	  outgoing: function(message, callback) {
	    console.log('outgoing', message);
	    callback(message);
	  }
	};

	client.addExtension(Logger);
	
	client.on('transport:down', function() {
	  // the client is offline
	});

	client.on('transport:up', function() {
	  // the client is online
	});
	
	
	var subscription = client.subscribe('/foo', function(message) {
	  // handle message
		console.log(message);
	});
	
	setTimeout(function(){
		var publication = client.publish('/foo', {text: 'Hi there, foo test'});

		publication.then(function() {
		  alert('Message received by server!');
		}, function(error) {
		  alert('There was a problem: ' + error.message);
		});
		
	},2000);
</script>

说明

  • /foo是需要订阅的主题
  • subscribe是订阅(浏览器端订阅主题,当其他地方发布一样的主题的时候,回调里的message会收到信息的)
  • publish是发布(浏览器发布主题,服务器端其实也可以发布的,暂未提供,见todo)

如果访问不了 http://at35.com:4567/faye/client.js ,请邮件给我,[email protected]

publish http api

发布主题的http api 说明

  • POST
  • url = /pub
  • post data = key=foo&value=somevalue`

通过curl命令测试本地POST

curl -d "key=foo&value=sss" http://127.0.0.1:4567/pub

测试线上环境

curl -d "key=foo&value=sss" http://at35.com:4567/pub

使用node发送post请求(java可以使用httpclient)

var request = require('request');

request.post({
	url:'http://at35.com:4567/pub', 
	form: {
		key:'foo',
		value:'' + str
	}
}, function(err,httpResponse,body){ 
	/* ... */ 
	if(err)
		console.log(err);
})	

检测步骤

  • 打开client/index.html
  • 在终端里执行curl命令
  • 看浏览器,是否会弹出sss

优化

todo

  • 试验redis作为存储
  • 可视化monitor
  • publish http api
  • extract http api to src/http_pub_api.js
  • add mocha && supertest for test
  • use commander.js for bin
  • client wrapper

url