号码变更时如何实时输出? [英] How to output in realtime when number is change?
问题描述
我有以下代码可用于输出:
I have this code for output :
$tot_clicks6 = $db->FetchArray($db->Query("SELECT SUM(visits) AS sum_visits FROM surf"));
和
<?=$tot_clicks6['sum_visits']?>
显示总数.
推荐答案
或者您可以使用实时框架.我在Realtime.co工作,我们就是这样做的.
Or you can use a real time framework. I work for Realtime.co and we do just that.
您可以在 www.realtime.co 处获得免费许可证,在 http://www.xrtml.org/downloads_62.html#pubsub:php 并使用以下内容应该广播信息的页面的代码(例如,您的管理页面).注意:此代码与您在Github上找到的ORTC示例相同( https://github.com/RTWWorld/pubsub-examples/tree/master/PHP )满足您的需求.
You can get a free license at www.realtime.co, get the PHP API at http://www.xrtml.org/downloads_62.html#pubsub:php and use the following code for the page that should broadcast the information (your administration page, for example). Note: this code is the same you can find at Github for the ORTC example (https://github.com/RTWWorld/pubsub-examples/tree/master/PHP) adapted for your needs.
<?php
error_reporting(E_ALL);
session_start();
require('./ortc.php');
/* -------------------- */
/* REPLACE THESE VALUES */
/* -------------------- */
$URL = 'http://ortc-developers.realtime.co/server/2.1';
$AK = 'YOUR_APPLICATION_KEY';// your realtime.co application key
$PK = 'YOUR_APPLICATION_PRIVATE_KEY';// your realtime.co private key
$TK = 'YOUR_AUTHENTICATION_TOKEN';// token: could be randomly generated in the session
$CH = 'MyChannel'; //channel
$ttl = 180;
$isAuthRequired = false;
$result = false;
/* -------------------- */
/* END */
/* -------------------- */
// ORTC auth
// on a live usage we would already have the auth token authorized and stored in a php session
// Since a developer appkey does not require authentication the following code is optional
if( ! array_key_exists('ortc_token', $_SESSION) ){
$_SESSION['ortc_token'] = $TK;
}
$rt = new Realtime( $URL, $AK, $PK, $TK );
// Your query
$tot_clicks6 = $db->FetchArray($db->Query("SELECT SUM(visits) AS sum_visits FROM surf"));
if($isAuthRequired){
$result = $rt->auth(
array(
$CH => 'w'
),
$ttl
);//post authentication permissions. w -> write; r -> read
echo 'authentication status '.( $result ? 'success' : 'failed' ).'<br/>';
}
if($result || !$isAuthRequired){
$result = $rt->send($CH, tot_clicks6['sum_visits'], $response);
echo ' send status '.( $result ? 'success' : 'failed' ).'<br/>';
}
?>
在接收器页面上,您需要使用JavaScript接收数据并显示它.对于此示例,我只是用数据提醒用户.
On the receiver page, you'll need to receive the data, using JavaScript and display it. For this example I'm just alerting the user with the data.
<!doctype html>
<html>
<head>
</head>
<body>
<script src="http://code.xrtml.org/xrtml-3.2.0.js"></script>
<script>
var appkey = 'YOUR_APPLICATION_KEY';
var url = 'http://ortc-developers.realtime.co/server/2.1';
var authToken = 'YOUR_AUTHENTICATION_TOKEN';
var channel = 'MyChannel';
xRTML.load(function(){
xRTML.Config.debug = true;
xRTML.ConnectionManager.create({
id: 'myConn',
appkey: appkey,
authToken: authToken,
url: url,
channels: [
{name: channel}
]
}).bind({
message: function(e) {
alert(e);
}
});
});
</script>
</body>
</html>
有了此代码,您将不需要使用AJAX或类似的东西.您可以将数据推送到浏览器.
With this code you won't need to use AJAX or anything like that. You'll be able to push your data to browsers instead.
希望有帮助!
这篇关于号码变更时如何实时输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!