Files
Nicholas Carrigan (he/him) 3da4be21bb chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to
staging.
2021-05-05 22:43:49 +05:30

2.1 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
589fc831f9fc0f352b528e76 處理連接斷開 2 301552 handle-a-disconnect

--description--

你也許注意到,目前爲止我們只處理用戶數量的增加,沒有處理減少。 事實上,處理用戶斷開連接也很簡單。 區別在於,新連接的監聽是發生在整個服務器上,但連接斷開的監聽是發生在每個 socket 上。

爲此,我們需要在目前的 'connect' 監聽裏面添加另一個監聽器,監聽 socket 斷開連接 'disconnect' 的事件。 通過登錄已與控制檯斷開連接的用戶,你可以測試這個功能。

socket.on('disconnect', () => {
  /*anything you want to do on disconnect*/
});

爲確保客戶端可以看到實時的用戶數量,我們應該在用戶斷開時讓 currentUsers 減 1然後發送 “user count” 事件,並使用修改後的用戶數量。

**注意:**和 'disconnect' 類似,所有 socket 可以發送到服務器的事件,我們都應該在有 “socket” 定義的連接監聽器裏處理。

完成上述要求後,請提交你的頁面鏈接。 如果你遇到了問題,可以參考這裏的答案。

--hints--

服務器應處理斷開 socket 連接的事件。

(getUserInput) =>
  $.get(getUserInput('url') + '/_api/server.js').then(
    (data) => {
      assert.match(data, /socket.on.*('|")disconnect('|")/gi, '');
    },
    (xhr) => {
      throw new Error(xhr.statusText);
    }
  );

客戶端應監聽 “user count” 事件。

(getUserInput) =>
  $.get(getUserInput('url') + '/public/client.js').then(
    (data) => {
      assert.match(
        data,
        /socket.on.*('|")user count('|")/gi,
        'Your client should be connection to server with the connection defined as socket'
      );
    },
    (xhr) => {
      throw new Error(xhr.statusText);
    }
  );

--solutions--

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/