2017-03-23 10 views
0

IDE 대괄호를 사용하면 아래 코드가 제대로 작동합니다. 그러나 github에 게시하면 PubNub에 올바르게 연결되지 않습니다. 나는 SDK를 제대로 설치하지 않았다고 생각하지만, 이것에 대해서는 약간의 멍청한 생각이 들며 그것을 고치는 방법에 대한 아이디어가 부족합니다. 어떤 도움이라도 대단히 감사하겠습니다!Pubnub 사용자 목록은 github에 게시 할 때 표시되지 않지만 어도비 브래킷에서는 잘 작동합니다.

누구든지이 문제를 해결할 수 있습니까? 여기

<!doctype html> 
<html> 
<head> 
<title>LupkerMusic</title> 


    <script type="text/javascript" src="jquery-3.1.1.js"></script> 
    <link rel="stylesheet" type="text/css" href="css/console.css"> 


</head> 
<body> 

    <h1 class="title">LUPKERMUSIC<img src="lupkermusiclogoinverse.png" 
    style="width:50px;height:50;"></h1> 

<p>List of Users</p> 
<br> 

<div id="channelStateBar" class="channelState"></div> 
<br> 

<button class="btn btn-primary updateButton" 
onclick="updateChannelState()">Update User List</button> 

<script src=https://cdn.pubnub.com/pubnub.min.js></script> 

<script type="text/javascript"> 
    (function() { 
     var publish_key = 'pub-c-a4dc5ebf-redacted'; 
     var subscribe_key = 'sub-c-1a1f6598-redacted'; 
     ssl: true; 
     var username = window.location.search.substring(1).split('=') 
[1]; 
     pubnub =PUBNUB.init({ 
      publish_key : publish_key, 
      subscribe_key : subscribe_key, 
      uuid : username 

     }); 
    })(); 
</script> 

<script type="text/javascript"> 
    function updateChannelState() { 
     pubnub.here_now({ 
      channel : 'lupkschat', 
      withPresence: true, 
      callback : function(m){ debugger; 
       $('#channelStateBar')[0].innerHTML = 
       'Occupancy : ' + m.occupancy + '<br/><br/><br/>' + 
'Users : ' + m.uuids; 
      } 
     }); 
    } 
</script> 
</body> 
</html> 
+0

github *에 * IDE 브래킷 * 및 * 게시가 있다는 것은 무엇을 의미합니까? –

+0

3x 버전이 아닌 [최신 SDK 사용] (https://www.pubnub.com/docs/web-javascript/pubnub-javascript-sdk) https://cdn.pubnub.com/sdk/javascript/pubnub.4.5.0.min.js - 이것은 코드 변경을 의미합니다. 추가 정보 ... –

+0

곧 업데이트 된 코드를 제공합니다. –

답변

0

구문 분석 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>

남겨주세요 코멘트 당신은 추가 질문이있는 경우.

+0

위의 코드 조각은 '사용자 목록 업데이트'버튼을 클릭 할 때 버그가 있습니다. 수정 프로그램에 대한 작업은 시스템에서 자체 파일로 실행하면 코드가 작동합니다. –

+0

Run Code Snippet 문제가 수정되었습니다. 스택 오버플로로 스 니펫을 만든 것은 실수였습니다. –