从Web服务器到客户端Bowers的实时通知 [英] Real time notification from web server to client bowers
问题描述
我正在使用php + mysql开发约会中心Web应用程序.我目前想做的是在没有第三方推送器且不使用jQuery SetInterval AJAX请求的情况下,从Web服务器向客户端/用户Bower进行约会时发送通知.我认为SetInterval& AJAX是一种不好的方法,因为客户端和服务器之间的流量过多.
I am developing an appointments center web application using php+mysql. what I currently trying to do is to send notification when an appointment has been made from web server to client/user bowers without third party pushers and without using jQuery SetInterval AJAX request. I think SetInterval & AJAX is a bad approach because there would be too much traffic between the client and the server.
如何在不使用SetInterval轮询服务器的情况下实现通知?
How can I implement notifications without polling the server with SetInterval?
推荐答案
您可以使用NodeJ进行此操作. NodeJS是服务器上的javascript,可将内容实时推送到连接的客户端.
You can do this using NodeJs. NodeJS is javascript on your server that pushes content to connected clients in real time.
它真的很容易使用和设置. 您需要专用于实时应用程序的服务器,我使用 http://nodejitsu.com .
Its really easy to use and setup. You need a server dedicated to the real time app, I use http://nodejitsu.com.
服务器端
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, url = require('url')
app.listen(8080);
function handler (req, res) {
// parse URL
var requestURL = url.parse(req.url, true);
// if there is a message, send it
if(requestURL.query.message)
sendMessage(decodeURI(requestURL.query.message));
// end the response
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("");
}
function sendMessage(message) {
io.sockets.emit('notification', {'message': message});
}
客户端
<script src="socket.io.min.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('notification', function (data) {
console.log(data.message);
});
</script>
我在下面添加了@intivev易于使用的示例,以为将来的读者提供答案
I added the easy to use example by @intivev below to complete the answer for future readers
这篇关于从Web服务器到客户端Bowers的实时通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!