the*_*dow 8 php comet nginx long-polling node.js
我正在设计一个长轮询应用程序,可以非常迅速地向很多用户广播小的变化.该应用程序将与运行相当标准的cms的网站串联运行.它们都将在一台服务器上运行,并且数据库也是如此.
我非常喜欢LAMP环境,我绝对是开发人员而不是系统管理员.那说我不怕尝试一些新东西.
我花了一天时间研究我的选择,我希望人们可以回答一些问题并给我一些建议.
我把它缩小到这些:
A. Apache and php for the website, Node.js for the app
B. Nginx and php for both the website and app
C. Nginx and php for website, Nginx and Node.js for the app
Run Code Online (Sandbox Code Playgroud)
所以问题:
请记住,设置的简易性可能是一个因素,我对Apache非常熟悉,但我只使用Node,而且我从未安装过Nginx.
如果有人需要,我会很乐意提供澄清.
我个人只会使用 Node.js。您可以将新信息推送给所有可用的客户端,而不是进行长时间的轮询。Node.JS 在交付实时内容时速度非常快,并且能够在一个包中完成所有操作。此外,客户端和服务器端都是用 JavaScript 编写的,这使得开发、调试和交付变得更加容易。作为开发人员,您可以看到这样做的好处。
以下是使用 Node.js 以及express、jade 和 NowJS 模块的应用程序示例。当然,这也可以与运行在 apache 上的 CMS 和提供动态内容的 Node.JS 结合使用。使用 Nginx 或节点脚本充当此脚本和 Apache 前面的反向代理。
服务器.js
var express = require('express')
, app = express.createServer()
, nowjs = require('now')
/* configure express server */
//...
app.get('/', function(req, res){
res.render('chat')
})
var everyone = nowjs.initialize(app)
// Server scoped function called by single client
everyone.now.distributeMsg = function(msg){
// Client scoped function of every connected client
this.now.receiveMsg(msg)
}
app.listen(3000)
Run Code Online (Sandbox Code Playgroud)
聊天玉
!!!
html
head
script(type='text/javascript', src='/nowjs/now.js')
body
#log
input#entry(type='text')
input#submit(type='button')
script
$(function(){
$('#submit').click(function(){
now.distributeMsg($('#entry').val())
})
now.receiveMsg = function(msg){
$('#log').append('<div>' + msg + '</div>')
}
})
Run Code Online (Sandbox Code Playgroud)
是的,它确实就是这么简单,不需要更多的代码行就可以将其变成一个功能齐全的聊天应用程序。事实上,您的标记和 CSS 会比应用程序代码占用更多行。惊人的!
| 归档时间: |
|
| 查看次数: |
3407 次 |
| 最近记录: |