與瀏覽者互動

您可以用直播視訊 API 即時取得直播視訊播放的留言和心情,讓影片製作者和線上表演者可以與瀏覽者互動。您可定時查詢 LiveVideo 物件,以取得最新留言和心情,或設定 Server-Sent Event,以接收即時留言和心情

取得最新留言和心情

若要取得直播視訊播放的最新留言或心情,請發送要求至:

GET /{live-video-id}/comments
GET /{live-video-id}/reactions

要求範例

curl -i -X GET \
  "https://graph.facebook.com/{live-video-id}/comments?access_token={access-token}"
GraphRequest request = GraphRequest.newGraphPathRequest(
  accessToken,
  "/{live-video-id}/comments",
  new GraphRequest.Callback() {
    @Override
    public void onCompleted(GraphResponse response) {
      // Insert your code here
    }
});

request.executeAsync();
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
    initWithGraphPath:@"/{live-video-id}/comments"
           parameters:nil
           HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    // Insert your code here
}];
FB.api(
  '/{live-video-id}/comments',
  'GET',
  {},
  function(response) {
      // Insert your code here
  }
);
try {
  // Returns a `FacebookFacebookResponse` object
  $response = $fb->get(
    '/{live-video-id}/comments',
    '{access-token}'
  );
} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();

回應範例

{
  "data": [
    {
      "created_time": "2018-08-30T18:47:02+0000",
      "from": {
        "name": "Steph C.",
        "id": "552524095105158"
      },
      "message": "This is such a great live stream.",
      "id": "911936075671494_911936769004758"
    },
    {
      "created_time": "2018-08-30T18:47:17+0000",
      "from": {
        "name": "Kevin D.",
        "id": "552524095105158"
      },
      "message": "Shoutout over here!",
      "id": "911936075671494_911936909004744"
    },
    {
      "created_time": "2018-08-30T18:48:14+0000",
      "from": {
        "name": "Clay T.",
        "id": "552524095105158"
      },
      "message": "Where is this place?",
      "id": "911936075671494_911937292338039"
    }
    ],
  "paging": {
    "cursors": {
      "before": "WTI5d...",
      "after": "WTI5d..."
    }
  }
}

請參閱留言心情關係連線參考資料,以取得關於可傳回欄位,以及篩選和排序說明的詳細資訊。可每隔幾秒輪詢一次留言和心情。

接收即時留言和心情

若要即時在瀏覽器用戶端中接收留言和心情,請設定 Server Sent Event 並發送要求至下列端點:

GET /{live-video-id}/live_comments
GET /{live-video-id}/live_reactions

請注意,串流活動的主機網址為:https://streaming-graph.facebook.com

要求範例

GET https://streaming-graph.facebook.com/{live-video-id}/live_comments?access_token={access-token}
var source = new EventSource("https://streaming-graph.facebook.com/{live-video-id}/live_comments?access_token={access-token}");
source.onmessage = function(event) {
  // Do something with event.message for example
};

回應範例

: ping
data:
  {
    "created_time":"2018-08-30T21:11:01+0000",
    "id":"911936075671494_912014908996944",
    "view_id":43329028,
    "from":
      {
        "id":"552524095105158",
        "name":"Kerry Fisher"
      },
    "message":"I love this video!"
  }
: ping
: ping

請參閱直播留言直播心情參考資料,以取得關於可傳回欄位、Ping 頻率和篩選的詳細資訊。