原文在这里:http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/
一直很想了解Comet的实现,正好看见了上面的这个文章,就装了个CentOS做了一下实验,过程如下:
1、下载软件和安装
引用
wget http://pushmodule.slact.net/downloads/nginx_http_push_module-0.692.tar.gz
tar -xvzf nginx_http_push_module-0.692.tar.gz
wget http://nginx.org/download/nginx-0.8.52.tar.gz
tar -xvzf nginx-0.8.52.tar.gz
./configure --prefix=/usr/local/nginx --add-module=/root/nginx_http_push_module-0.692
make
sudo make install
configure过程中会提示哪些包没有,我的就是pcre-devel包没有,直接使用光盘rpm 安装上就可以了
2、配置文件,修改nginx的配置文件nginx.conf,并启动nginx
引用
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 81;
server_name localhost;
root /var/www/localhost;
location /cheetah {
push_channel_group pushmodule_cheetah;
location /cheetah/pub {
set $push_channel_id cheetah;
push_publisher;
push_message_timeout 5s; # Give the clients time
push_message_buffer_length 10; # to catch up
}
location /cheetah/sub {
set $push_channel_id cheetah;
push_subscriber;
send_timeout 3600;
}
}
}
}
启动nginx
引用
/usr/local/nginx/sbin/nginx
3、编辑html测试文件,send.html用来发送信息,listen.html则是接受信息,当然保证jquery在相应的目录中。
send.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Send</title>
<script type="text/javascript" src="javascript/jquery-1.4.2.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
alert('aa');
function showResult(status, response) {
$('#result').html('<strong>status:</strong> ' + status +
'<br /><strong>response:</strong><br />' + response);
};
$(document).ready(function() {
$('#pub').submit(function() {
message = $('#message').val();
/* Do not send empty message */
if (message == '') {
return false;
}
$.ajax({
url: '/cheetah/pub',
data: message,
dataType: 'text',
type: 'post',
success: function(responseText, textStatus, xhr) {
showResult(textStatus, responseText);
},
error: function(xhr, textStatus, errorThrown) {
showResult(textStatus, errorThrown);
}
});
return false;
});
});
/* ]]> */
</script>
</head>
<body>
<form id="pub" method="post" action="/cheetah/pub">
<input type="text" class="message" name="message" id="message" />
<input class="submit" type="submit" value="send" />
</form>
<div id="result"></div></div>
</body>
</html>
listen.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Listen</title>
<script type="text/javascript" src="javascript/jquery-1.4.2.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
function listen(last_modified, etag) {
$.ajax({
'beforeSend': function(xhr) {
xhr.setRequestHeader("If-None-Match", etag);
xhr.setRequestHeader("If-Modified-Since", last_modified);
},
url: '/cheetah/sub',
dataType: 'text',
type: 'get',
cache: 'false',
success: function(data, textStatus, xhr) {
etag = xhr.getResponseHeader('Etag');
last_modified = xhr.getResponseHeader('Last-Modified');
div = $('<div class="msg">').text(data);
info = $('<div class="info">').text('Last-Modified: ' + last_modified + ' | Etag: ' + etag);
$('#data').prepend(div);
$('#data').prepend(info);
/* Start the next long poll. */
listen(last_modified, etag);
},
error: function(xhr, textStatus, errorThrown) {
$('#data').prepend(textStatus + ' | ' + errorThrown);
}
});
};
$(document).ready(function() {
/* Start the first long poll. */
/* setTimeout is required to let the browser know
the page is finished loading. */
setTimeout(function() {listen('', '');}, 500);
});
/* ]]> */
</script>
<style type="text/css">
#data {
margin: .5em;
}
#data .info {
font-weight: bold;
font-size: 14px;
}
#data .msg {
white-space: pre;
font-family: courier;
font-size: 14px;
margin-bottom: .5em;
margin-left: .5em;
}
</style>
</head>
<body>
<div id="data"></div>
</body>
</html>
大功告成,只要在send.html里面输入内容并发送,listen.html就会接收到信息
分享到:
相关推荐
Windows 平台 Nginx Rtmp模块编译后的文件,nginx版本是nginx-1.12.1,自测可用 Windows 平台 Nginx Rtmp模块编译后的文件,nginx版本是nginx-1.12.1,自测可用
nginx的内部结构是由核心部分和一系列的功能模块所组成。这样划分是为了使得每个模块的功能相对简单,便于开发,同时也便于对系统进行功能扩展。这样的模块化设计类似于面向对象中的接口类,它增强了nginx源码的...
这一部分介绍配置项的方式,更偏重于带领对Nginx还比较陌生的读者熟悉它,通过了解几个基本Nginx模块的配置修改方式,进而使读者可以通过查询官网、第三方网站来了解如何使用所有Nginx模块的用法。? 在第二部分的...
# 使用方法 双击nginx.exe # 简要说明 conf/nginx.conf 为配置文件实例 RTMP监听 1935 端口,启用live 和hls 两个application HTTP监听 8080 端口, * :8080/stat 查看stream状态 * :8080/index.html 为一个直播...
Windows下编译Nginx并添加模块,在项目中使用过的
test-nginx, 面向 Nginx C 模块和 OpenResty Lua库开发的数据驱动测试 电子邮件名称Test::Nginx - Nginx MODULE 和 Nginx/openresty库和应用程序的数据驱动测试脚手架 table-内容NAME描述用户指南使用 Test::Nginx ...
深入理解Nginx模块开发及架构解析,深入理解Nginx模块开发及架构解析
书中首先通过介绍官方Nginx的基本用法和配置规则,帮助读者了解一般Nginx模块的用法,然后重点介绍了女口何开发HTTP模块(含HTTP过滤模块)来得到定制化的Nginx,其中包括开发—个功能复杂的模块所需要了解的各种知识...
本书首先通过介绍官方Nginx的基本用法和配置规则,帮助读者了解一般Nginx模块的用法,然后重点介绍如何开发HTTP模块(含HTTP过滤模块)来得到定制的Nginx,其中包括开发一个功能复杂的模块所需要了解的各种知识,如...
书中首先通过介绍官方Nginx的基本用法和配置规则,帮助读者了解一般Nginx模块的用法,然后重点介绍了女口何开发HTTP模块(含HTTP过滤模块)来得到定制化的Nginx,其中包括开发—个功能复杂的模块所需要了解的各种知识...
但如果希望通过阅读本书的第二、第三两部分,来学习Nginx的模块开发和架构设计 技巧时,则必须了解C语言的基本语法。在阅读本书第三部分时,需要读者对TCP有一个基 本的了解,同时对Linux操作系统也应该有简单的了解...
Nginx模块开发OpenResty简单使用笔记整理 ### Nginx简介 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中。与Apache相比。 同时,大量的第三方扩展模块也令...
模块开发与架构解析》首先通过介绍官方Nginx的基本用法和配置规则,帮助读者了解一般Nginx模块的用法,然后重点介绍如何开发HTTP模块(含HTTP过滤模块)来得到定制的Nginx,其中包括开发一个功能复杂的模块所需要...
本书首先通过介绍官方Nginx的基本用法和配置规则,帮助读者了解一般Nginx模块的用法,然后重点介绍如何开发HTTP模块(含HTTP过滤模块)来得到定制的Nginx,其中包括开发一个功能复杂的模块所需要了解的各种知识,如...
深入理解Nginx模块开发与架构解析第2版LinuxUnix技术丛书
Nginx模块源码 nginx-notice-2 一只小麻雀。 通过Post的方式取文件 Nginx 0.7 可编译。
但由于项目需要在Windows上使用nginx,无奈只好自己去找资料,在Windows上编译nginx-openresty同时加入rtmp模块。本资源是Windows上生成好的openresty-1.19.3.1+nginx-rtmp-module的包,同时包含win32、win64版本。
nginx-rtmp-module-master 源码包 nginx的rtmp流媒体模块
本书首先通过介绍官方Nginx的基本用法和配置规则,帮助读者了解一般Nginx模块的用法,然后重点介绍如何开发HTTP模块(含HTTP过滤模块)来得到定制的Nginx,其中包括开发一个功能复杂的模块所需要了解的各种知识,如...
主要就是nginx的memcache和echo模块。另外假如需要安装memcached服务的话,里面还有安装包,方便进行测试。