Skip to content

Commit bd4a0cc

Browse files
author
Beno Netto
committed
new files and notification alert
adding new files and create an update with the number of new messages
1 parent 42cc280 commit bd4a0cc

File tree

3 files changed

+412
-0
lines changed

3 files changed

+412
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Realtime Framework
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

notifications.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
session_start();
4+
require('./ortc.php');
5+
//see if is the same file
6+
7+
/* -------------------- */
8+
/* REPLACE THESE VALUES */
9+
/* -------------------- */
10+
$URL = 'http://ortc-developers.realtime.co/server/2.1';
11+
12+
// your realtime.co application key
13+
//sua realtime.co aplicação
14+
$AK = 'YOUR_APPLICATION_KEY';
15+
16+
17+
// your realtime.co private key
18+
// sua realtime.co chave privada
19+
$PK = 'YOUR_APPLICATION_PRIVATE_KEY';
20+
21+
// token: could be randomly generated in the session
22+
// token: pode ser randomicamente gerada na sessão
23+
$TK = 'YOUR_AUTHENTICATION_TOKEN';
24+
25+
$CH = 'myChannel'; //channel // canal
26+
$ttl = 180;
27+
$isAuthRequired = false;
28+
$result = false;
29+
/* -------------------- */
30+
/* END / FIM */
31+
/* -------------------- */
32+
33+
// ORTC auth
34+
// on a live usage we would already have the auth token authorized and stored in a php session
35+
// Since a developer appkey does not require authentication the following code is optional
36+
37+
// ORTC auth
38+
// em uma aplicação rodando você já tem o token auth autorizado e armazenado na sessão do php
39+
// Uma vez que um desenvolvedor AppKey não exige autenticação o código a seguir é opcional
40+
41+
if( ! array_key_exists('ortc_token', $_SESSION) ){
42+
$_SESSION['ortc_token'] = $TK;
43+
}
44+
45+
$rt = new Realtime( $URL, $AK, $PK, $TK );
46+
47+
if($isAuthRequired){
48+
$result = $rt->auth(
49+
array(
50+
$CH => 'w'
51+
),
52+
$ttl
53+
);//post authentication permissions. w -> write; r -> read
54+
//autenticação de permissões para publicação. w -> escrita; r -> leitura
55+
echo '<div class="status-error">authentication status '.( $result ? 'success' : 'failed' ).'</div>';
56+
}
57+
58+
if($result || !$isAuthRequired){
59+
$result = $rt->send($CH, "Sending message from php API", $response);
60+
61+
if($result){
62+
63+
echo '<div class="status-ok"> send status connected</div>';
64+
}else{
65+
echo '<div class="status-error"> send status failed</div>';
66+
67+
}
68+
}
69+
70+
?>
71+
72+
73+
<!doctype html>
74+
<html>
75+
<head>
76+
<title>Testando Realtime.co</title>
77+
<style type="text/css">
78+
body {
79+
color:#333;
80+
font-family:Arial,sans-serif;
81+
}
82+
83+
.status-ok{padding:5px; color:#fff; background:green; margin-top:10px; margin-bottom:10px; width:550px;}
84+
.status-error{padding:5px; color:#fff; background:red; margin-top:10px; margin-bottom:10px; width:550px;}
85+
#log{border-top:1px solid #ccc; padding:10px; margin-top:10px; width:550px; }
86+
.msg{display:none; border:1px solid #ccc; padding:3px; margin-top:3px; float:left; width:500px; height:auto;
87+
color:#32CD32; background:#000; font-family: 'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace;}
88+
.notifications{background:blue; position:absolute; top:10px; right:10px; font-size:30px; color:#fff; padding:5px; display:none; cursor:pointer;}
89+
90+
</style>
91+
</head>
92+
<body>
93+
<input type="text" id="message" />
94+
<input type="button" onclick="sendMessage('myChannel');" value="Send to myChannel" />
95+
<div class="notifications">0</div>
96+
<div id="log"></div>
97+
98+
<script src="http://code.xrtml.org/xrtml-3.0.0.js"></script>
99+
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
100+
<script>
101+
var appkey = '<?php echo($AK); ?>',
102+
url = '<?php echo($URL); ?>',
103+
token = '<?php echo($TK); ?>',
104+
number = $('.notifications').html();
105+
xRTML.ready(function(){
106+
xRTML.Config.debug = true;
107+
//criando uma conexão no realtime.co utilizando sua appkey
108+
xRTML.ConnectionManager.create(
109+
{
110+
id: 'myConn',
111+
appkey: appkey,
112+
authToken: token,
113+
url: url,
114+
channels: [
115+
{name: 'myChannel'}
116+
]
117+
}).bind(
118+
{
119+
message: function(e) {
120+
var log = $("#log");
121+
var count = ++number;
122+
//verifica se a mensagem está vazia / check empty message
123+
if(e.message != ''){
124+
log.prepend('<span class="msg">Message received: ' + e.message + '</span>');
125+
$('.notifications').html(count);
126+
$('.notifications').slideDown();
127+
}
128+
}
129+
});
130+
});
131+
132+
$('.notifications').click(function(){
133+
//zera a contagem de notificações
134+
number = 0;
135+
$(this).html(number);
136+
$('.msg').fadeIn();
137+
});
138+
//função de mensagem, pega o valor do campo #message (input)
139+
function sendMessage(channel){
140+
var msg = $('#message').val();
141+
xRTML.ConnectionManager.sendMessage({
142+
connections: ['myConn'],
143+
channel: channel,
144+
content: msg
145+
});
146+
}
147+
</script>
148+
149+
</body>
150+
</html>

0 commit comments

Comments
 (0)