在线聊天的实现方案有很多,像Ajax长短轮循、富客户端、HTTP长连接、HTML5的event source和websocket等。目前来看HTML5的websocket比较具有优势,对于不支持H5的浏览器可以忽略或者采用兼容的形式来实现。接下来提到的nginx-push-stream-module支持HTML5的event source和websocket以及长轮循等形式。

nginx-push-stream-module模块的安装与配置

  首先需要下载或者克隆插件,地址在https://github.com/wandenberg/nginx-push-stream-module。如果是和nginx一起安装只需要在configure时加上--add-module=模块路径即可,如果已经安装好了nginx可以查看《如何在已经安装好的Nginx上增加新模块》这篇博文,下面说一下配置方式。

  安装后需要在nginx的配置文件nginx.conf或者其他包含的配置文件中做如下配置:

在http段加入

#最大共享内存
push_stream_shared_memory_size                100m;
#频道最大长度
push_stream_max_channel_id_length             200;
#每个频道缓存的最大消息数量
push_stream_max_messages_stored_per_channel   20;
#消息生命周期(分钟)
push_stream_message_ttl                       5m;

添加一个虚拟主机(server)配置如下

    server {
	listen 8080;
	server_name     127.0.0.1;
	location /pub {
		push_stream_publisher admin;
		push_stream_channels_path $arg_id;
		push_stream_store_messages on;
	}
	location ~ /sub/(.*) {
		push_stream_subscriber;
		push_stream_channels_path $1;
		push_stream_last_received_message_time      "$arg_time";
		push_stream_last_received_message_tag       "$arg_tag";
		push_stream_header_template "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta http-equiv=\"Cache-Control\" content=\"no-store\">\r\n<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n<meta http-equiv=\"Pragma\" content=\"no-cache\">\r\n<meta http-equiv=\"Expires\" content=\"Thu, 1 Jan 1970 00:00:00 GMT\">\r\n<script type=\"text/javascript\">\r\nwindow.onError = null;\r\ntry{ document.domain = (window.location.hostname.match(/^(\d{1,3}\.){3}\d{1,3}$/)) ? window.location.hostname : window.location.hostname.split('.').slice(-1 * Math.max(window.location.hostname.split('.').length - 1, (window.location.hostname.match(/(\w{4,}\.\w{2}|\.\w{3,})$/) ? 2 : 3))).join('.');}catch(e){}\r\nparent.PushStream.register(this);\r\n</script>\r\n</head>\r\n<body>";
        	push_stream_message_template "<script>p(~id~,'~channel~','~text~');</script>";
		push_stream_footer_template "</body></html>";
		default_type "text/html; charset=utf-8";
		push_stream_ping_message_interval 10s;
	}
	location ~ /ev/(.*) {
		push_stream_subscriber eventsource;
		push_stream_channels_path $1;
		push_stream_last_received_message_time      "$arg_time";
		push_stream_last_received_message_tag       "$arg_tag";
		push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\",\"tag\":\"~tag~\",\"time\":\"~time~\"}";
		push_stream_ping_message_interval 10s;
	}
	location ~ /ws/(.*) {
		push_stream_subscriber websocket;
		push_stream_channels_path $1;
		push_stream_last_received_message_time      "$arg_time";
		push_stream_last_received_message_tag       "$arg_tag";
		push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\",\"tag\":\"~tag~\",\"time\":\"~time~\"}";
		push_stream_ping_message_interval 10s;
	}
	location ~ /lp/(.*) {
		push_stream_subscriber long-polling;
		push_stream_channels_path $1;
		push_stream_last_received_message_time      "$arg_time";
		push_stream_last_received_message_tag       "$arg_tag";
		push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\",\"tag\":\"~tag~\",\"time\":\"~time~\"}";
		push_stream_longpolling_connection_ttl 30s;
	}

  }

然后重新载入nginx的配置,可以在Linux命令下使用curl做如下测试(其中ch1代表频道)

#发布信息:
curl http://localhost:8080/pub?id=ch1 -d "test message"
#订阅信息:
curl http://localhost:8080/lp/ch1