服务端采用 icomet
openresty使用最新版
git clone https://github.com/ideawu/icomet
cd icomet
make
./comet-server icomet.conf &
文件夹内有demo范例 稍加修改就可以跑起来
server
{
listen 80;
server_name icomet.test.com;
index chat.html;
root /home/lamp/icomet/demo/web;
#lua_code_cache off;
location ~ ^/icomet/.*
{
rewrite ^/icomet/(.*) /$1 break;
proxy_read_timeout 60;
proxy_connect_timeout 60;
proxy_buffering off;
proxy_pass http://127.0.0.1:8000;
}
location ~ ^/front/.*
{
rewrite ^/front/(.*) /$1 break;
proxy_read_timeout 60;
proxy_connect_timeout 60;
proxy_buffering off;
proxy_pass http://127.0.0.1:8100;
}
location /sign
{
default_type text/plain;
content_by_lua_file lua/sign.lua;
}
location /push
{
default_type text/plain;
content_by_lua_file lua/push.lua;
}
access_log /dev/null;
error_log /dev/null;
}
sign.lua
local cname = ngx.var.arg_cname
local cb = ngx.var.arg_cb
local url = "/icomet/sign"
local res,error = ngx.location.capture(url,
{ args = { cname = cname ,cb = cb}}
)
if res.status == 200 then
ngx.say(res.body)
else
ngx.exit(0)
end
push.lua
local cname = ngx.var.arg_cname
local content = ngx.unescape_uri(ngx.var.arg_content)
local url = "/icomet/push"
local res,error = ngx.location.capture(url,
{ args = { cname = cname ,content = content}}
)
if res.status == 200 then
ngx.say(res.body)
else
ngx.exit(0)
end
修改
icomet /demo/web/js/chat.js
var sign_url = 'http://' + app_host + path + '/php/sign.php';
var pub_url = 'http://' + app_host + path + '/php/push.php?cb=?';
替换成
var sign_url = 'http://' + app_host + path + '/sign?test=1';
var pub_url = 'http://' + app_host + path + '/push?cb=?';
icomet_host = ps[0] + ':8100';
替换成
icomet_host = ps[0];
绑定本机hosts
服务端ip icomet.test.com
浏览器访问 http://icomet.test.com