구문 분석 PubNub 지금
내가 PubNub SDK 4.5.0 (the latest at this post)에 코드를 업데이트 한
결과. 채널에 가입하면 탑승객이 1 명 이상이므로 결과를 얻을 수 있도록 코드를 적절하게 조정했습니다. 하지만
your PubNub Admin Dashboard's Debug Console 또는
PubNub Dev Console 같은 다른 클라이언트의 채널을 구독 할 수 있습니다. 4 × PubNub의 SDK에서
, 다중 채널 및 채널 당 UUID를 가진
hereNow results are presented in a much more rick format, 다음과 같이이 예제는 하나 개의 특정 채널을 찾고
{
"status": 200,
"message": "OK",
"payload": {
"channels": {
"81d8d989-b95f-443c-a726-04fac323b331": {
"uuids": [
"70fc1140-22b5-4abc-85b2-ff8c17b24d59"
],
"occupancy": 1
},
"81b7a746-d153-49e2-ab70-3037a75cec7e": {
"uuids": [
"91363e7f-584b-49cc-822c-52c2c01e5928"
],
"occupancy": 1
},
"c068d15e-772a-4bcd-aa27-f920b7a4eaa8": {
"uuids": [
"ccfac1dd-b3a5-4afd-9fd9-db11aeb65395"
],
"occupancy": 1
}
},
"total_channels": 3,
"total_occupancy": 3
},
"service": "Presence"
}
때문에, 나는 채널 루프를 구현하고 단지 않았다 채널 이름을 응답 구문 분석에 하드 코딩합니다. 여기에 결과 코드가 있고 demo/demo
키를 사용하는 것처럼 작동하지만 사용자 자신의 pub/sub 키로 교체해야합니다. 그러나 처음에는 securing you app with PubNub Access Manager없이 공개 키를 공개해서는 안됩니다.
<script type="text/javascript">
(function() {
var publish_key = 'demo'; // replace with your pub key
var subscribe_key = 'demo'; // replace with your sub key
var username = "uuid-" + Date.now()
// window.location.search.substring(1).split('=');
pubnub = new PubNub({
publishKey : publish_key,
subscribeKey : subscribe_key,
ssl: true,
uuid : username
});
pubnub.addListener({
status: function(statusEvent) {
console.log("Status event received: ", statusEvent);
},
message: function(message) {
console.log("Message received: ", message);
},
presence: function(presenceEvent) {
console.log("Presence event received: ", presenceEvent);
// by monitoring join/leave/timeout events,
// you can update the user list in realtime
// after the initial hereNow call
}
});
// subscribing to channel so that at least
// 1 occupant is there when we call hereNow
pubnub.subscribe({
channels: ['lupkschat'],
withPresence: true
});
})();
</script>
<script type="text/javascript">
var updateChannelState = function() {
pubnub.hereNow({
channels : ['lupkschat'],
withPresence: true
},
function(status, response){
console.log(status, response);
// hardcoded with known channel for this simple example
// best practice would be to loop through all channels
var channel = response.channels['lupkschat'];
var uuids = [];
for (var i=0; i < channel.occupancy; i++) {
uuids.push(channel.occupants[i].uuid);
}
$('#channelStateBar')[0].innerHTML = 'Occupancy : '
+ channel.occupancy + '<br>'
+ 'Users : [' + uuids.join(" | ") + "]";
});
};
</script>
<html>
<head>
<title>LupkerMusic</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.5.0.min.js" type="text/javascript"></script>
</head>
<body>
<h1 class="title">LUPKERMUSIC</h1>
List of Users
<div class="channelState" id="channelStateBar"></div>
<button class="btn btn-primary updateButton" onclick="updateChannelState()">Update User List</button>
<script type="text/javascript">
(function() {
var publish_key = 'demo'; // replace with your pub key
var subscribe_key = 'demo'; // replace with your sub key
var username = "uuid-" + Date.now()
// window.location.search.substring(1).split('=');
pubnub = new PubNub({
publishKey : publish_key,
subscribeKey : subscribe_key,
ssl: true,
uuid : username
});
pubnub.addListener({
status: function(statusEvent) {
console.log("Status event received: ", statusEvent);
},
message: function(message) {
console.log("Message received: ", message);
},
presence: function(presenceEvent) {
console.log("Presence event received: ", presenceEvent);
// by monitoring join/leave/timeout events,
// you can update the user list in realtime
// after the initial hereNow call
}
});
// subscribing to channel so that at least
// 1 occupant is there when we call hereNow
pubnub.subscribe({
channels: ['lupkschat'],
withPresence: true
});
})();
</script>
<script type="text/javascript">
var updateChannelState = function() {
pubnub.hereNow({
channels : ['lupkschat'],
withPresence: true
},
function(status, response){
console.log(status, response);
// hardcoded with known channel for this simple example
// best practice would be to loop through all channels
var channel = response.channels['lupkschat'];
var uuids = [];
for (var i=0; i < channel.occupancy; i++) {
uuids.push(channel.occupants[i].uuid);
}
$('#channelStateBar')[0].innerHTML = 'Occupancy : '
+ channel.occupancy + '<br>'
+ 'Users : [' + uuids.join(" | ") + "]";
});
};
</script>
</body>
</html>
남겨주세요 코멘트 당신은 추가 질문이있는 경우.
github *에 * IDE 브래킷 * 및 * 게시가 있다는 것은 무엇을 의미합니까? –
3x 버전이 아닌 [최신 SDK 사용] (https://www.pubnub.com/docs/web-javascript/pubnub-javascript-sdk) https://cdn.pubnub.com/sdk/javascript/pubnub.4.5.0.min.js - 이것은 코드 변경을 의미합니다. 추가 정보 ... –
곧 업데이트 된 코드를 제공합니다. –