-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
153 lines (132 loc) · 4.42 KB
/
index.html
File metadata and controls
153 lines (132 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8>
<!-- add libs -->
<script language="JavaScript" type="text/javascript" src="//comet-server.com/js/lib/jquery.min.js" ></script>
<script src="//comet-server.com/CometServerApi.js" type="text/javascript"></script>
</head>
<body>
<style>
hr{
clear:both;
}
.hide{
display:none;
}
#log{
color:#000
}
.status{
color:#272;
font-weight:bold;
}
.note{
background:#eee;
padding:1px 10px;
margin:5px;
border-radius:5px;
text-align:justify;
}
</style>
<h3>Auth chat</h3>
<div class="note">
<p><a href="https://github.com/CppComet/auth-example">Git repository with all code of this example.</a></p>
<p>In this example show how authenticate on CppComet and how send personal messages to users via CppComet</p>
<ul>
<li><a href="http://comet-server.org/doku.php/en" target="_blank">More info about CppComet</a></li>
<li><a href="http://comet-server.org/doku.php/en:comet:cometql" target="_blank">More info about CometQL</a></li>
<li><a href="http://comet-server.org/doku.php/en:comet:authentication" target="_blank">More info about auth in CppComet</a></li>
</ul>
<p>
Open the example in two or more browsers, Copy your `USER_ID` from one
window and paste it into another window in the field labeled
'Identificator of user who must will receive the message`
enter the message text and press send. You will see that the message
will come only to the window with the same USER_ID you specified when
sending it.
</p>
</div>
<div class="root">
<hr>
<div><span id="userId"></span> - use this id to send message</div>
<hr>
<div class="status">
We are waiting for authorization on the comet server
</div>
<hr>
<input type="text" placeholder="Message" id="text"> - Personal text message for user<br>
<input type="text" placeholder="user_id" id="to_user_id"> - Identificator of user who must will receive the message<br>
<button onclick="send($('#to_user_id').val(), $('#text').val())">Send</button>
<hr>
<div id="log"></div>
</div>
<script type="text/javascript">
function log()
{
console.log(arguments);
$("#log").append(JSON.stringify(arguments)+"<hr>");
}
// Authorization on comets server
function auth()
{
var user_id = 0;
try{
user_id = localStorage['user_id']
}catch(e){
user_id = Math.floor(Math.random()*1000000)
}
if(!user_id)
{
user_id = Math.floor(Math.random()*1000000)
localStorage['user_id'] = user_id
}
$("#userId").text("USER_ID = "+user_id);
log("Request authorization for user_id="+user_id)
var user_key = "PassForUser"+user_id
// The first step – it is sending user’s ID and a random hash in your system to the comet-server via CometQL.
// auth.php ( https://github.com/CppComet/auth-example/blob/master/auth.php )
return $.when($.ajax({
url: "https://comet-server.com/doc/CometQL/auth-chat/auth.php?user_id="+user_id+"&user_key="+user_key+"&t="+Math.floor(Math.random()*1000000),
type: "GET",
dataType:'json',
})).always(function(res){
$(".status").html("We are waiting for connection to the comet server");
// The function start accepts connection settings and opens new connection.
// https://comet-server.com/wiki/doku.php/en:comet:javascript_api#connection_with_server
cometApi.start({
node: "app.comet-server.ru",
dev_id: 15,
user_id:user_id,
user_key:user_key,
})
}).promise();
}
/**
*
* @param {integer} user_id
* @param {string} text
*/
function send(user_id, text)
{
// Send message
// send.php ( https://github.com/CppComet/auth-example/blob/master/send.php )
$.ajax({
url: "https://comet-server.com/doc/CometQL/auth-chat/send.php?user_id="+user_id+"&msg="+text+"&t="+Math.floor(Math.random()*1000000),
type: "GET",
})
}
cometApi.onAuthSuccess(function()
{
// We will initialize the call after successful authorization on the comet server.
$(".StartCallBtn").removeClass("hide");
log("Authorization on the comet server is complete")
$(".status").html("Authorization on the comet server is complete");
})
CometServer().subscription("msg.message", function(e){
log(e)
})
auth();
</script>
</body>
</html>