php 从 Web 服务器到客户端 bower 的实时通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16860106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Real time notification from web server to client bowers
提问by Hadi.M
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.
我正在使用 php+mysql 开发约会中心 Web 应用程序。我目前尝试做的是在没有第三方推送器和不使用 jQuery SetInterval AJAX 请求的情况下,在从 Web 服务器向客户端/用户 bowers 进行约会时发送通知。我认为 SetInterval 和 AJAX 是一种糟糕的方法,因为客户端和服务器之间的流量过多。
How can I implement notifications without polling the server with SetInterval?
如何在不使用 SetInterval 轮询服务器的情况下实现通知?
回答by MDDY
You can do this using NodeJs. NodeJS is javascript on your server that pushes content to connected clients in real time.
你可以使用 NodeJs 来做到这一点。NodeJS 是您服务器上的 javascript,可实时将内容推送到连接的客户端。
Its really easy to use and setup. You need a server dedicated to the real time app, I use http://nodejitsu.com.
它非常易于使用和设置。您需要一个专用于实时应用的服务器,我使用http://nodejitsu.com。
Server Side
服务器端
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});
}
Client Side
客户端
<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>
I added the easy to use example by @intivev below to complete the answer for future readers
我在下面添加了@intivev 的易于使用的示例,以完成未来读者的答案
回答by Anthony Sterling
You can use WebSockets for this, the Ratchetis a PHP implementation built on top of the React library. I've used both these libraries in production applications and have been more than happy with them.
您可以为此使用 WebSockets,Ratchet是构建在React 库之上的 PHP 实现。我已经在生产应用程序中使用过这两个库,并且对它们非常满意。
Yes, using node.js is an option and maybe a better one - dependant on your current situation.
是的,使用 node.js 是一种选择,也许是更好的选择 - 取决于您当前的情况。
Anthony.
安东尼。
回答by intivev
I feel u are choosing the wrong language for this purpose (PHP), there may be a way out to do it in PHP , but i am damp sure that its would be a twisted one. I recommend using node.js for this purpose as using it you can accomplish pushing notifications to client in much simpler way
我觉得你为此目的(PHP)选择了错误的语言,可能有办法在 PHP 中做到这一点,但我很确定它会是一个扭曲的。我建议为此目的使用 node.js,因为使用它您可以以更简单的方式完成向客户端推送通知
Server Side
服务器端
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});
}
Client Side
客户端
<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>
So you see its lot eassy if you use the suitable language for it
所以如果你使用合适的语言,你会发现它很容易