發佈日期:

Socket.io錯誤訊息意義

I have found:

  • “ping timeout”: client stopped responding to pings in the allotted amount of time (per the pingTimeout config setting).
  • “transport close”: this appears to happen if the client side stopped sending data at all… or maybe there’s some kind of callback causing this to happen. I can see it happen if I just close a tab or follow a link from a page where I have an active connection to the server. But I’m not clear if this is always a case of the client causing it to happen.

    Sorry for re-opening the issue. Regarding the transport close that is the reason when page is closed/reloaded, it also happens some times in bad network conditions specifically when the ping packets are not delivered to the client. For the latter I need to handle it in the server side by waiting for the client to reconnect. Is there a way to properly distinguish between these two?

  • “Client namespace disconnect”: When the client sends a disconnect packet (client.disconnect())
  • “server namespace disconnect”: Looks to be when the server performs a socket.disconnect() action.
  • “Transport error”: An error occurred, I assume this is a server side error, but I’m not totally clear, as I’ve not been able to trigger one on my own.
  • “io server disconnect” This occurs using a third party library socketIOAuth when authentication fails

閱讀全文 Socket.io錯誤訊息意義

發佈日期:

Socket.io介紹

Socket.io

socket.io是基於Websocket的Client-Server實時通信庫

Socket.io承繼了Node.js的事件處理方法,把Client端與Server端的程式統一成一至的操作方式,讓使用者可以只需專注在處理「事件」,就可以快速開發出應用,他也支援『房間』的概念,可以使用同一條WebSocket卻擁有不被彼此干擾的資料傳輸(多種聊天頻道的概念)。另外,他也提供了很好的fallback機制,即使用戶的瀏覽器不支援WebSocket,他還是可以利用Flash、XMLHttpRequest等方式來傳送資訊(速度會比較慢就是了)。這些機制都他都包裝好了,所以寫程式時並不需要知道這些細節,只需要設定好就可以運作。

Socket.io 特性整理

  • Events 自訂事件。
  • Rooms Room 的概念只存在於伺服器端。可以理解為訊息處理時的聽眾分組,可對同一個分組內的聽眾進行廣播。
  • Namespaces 命名空間,我理解為底層連線的分組管理,不同命名空間可以走同一條 Engine.io 連線或是各自連線,每個命名空間可以各自驗證是否接受連線。
  • ACK 回調 如同 HTTP 之於 TCP,HTTP 為 TCP 提供了一套請求與響應的模型。ACK 也為 Socket.io 提供了一套請求與響應的通訊模型。
  • 連線維護
  • 自動斷線重連
  • ping/pong 心跳

閱讀全文 Socket.io介紹

發佈日期:

Socket.io自行增加header

伺服器端

範例程式碼:

import express from "express";
import http from "http";

const app = express();
const server = http.createServer(app);

const sio = require("socket.io")(server, {
    handlePreflightRequest: (req, res) => {
        const headers = {
            "Access-Control-Allow-Headers": "Content-Type, Authorization",
            "Access-Control-Allow-Origin": req.headers.origin, //or the specific origin you want to give access to,
            "Access-Control-Allow-Credentials": true
        };
        res.writeHead(200, headers);
        res.end();
    }
});

sio.on("connection", () => {
    console.log("Connected!");
});

server.listen(3000);

閱讀全文 Socket.io自行增加header

發佈日期:

Engine.io介紹

Engine.io介紹

Socket.io是在engine.io的基礎上去實作的
Gitlab連結: Engine.IO: the realtime engine
engine.iosocket.io提供跨瀏覽器/跨設備的雙向通信的底層庫。engine.io使用了WebsocketXHR方式封裝了一套socket協議。在低版本的瀏覽器中,不支持Websocket,為了兼容使用長輪詢( polling )替代。

關於長輪詢可參考我的另一篇文章:WebSocket與Ajax的不同
過去WebSocket未出來時,許多聊天室使用的都是長輪詢的方式去實作,而engine.io則可依據客戶端環境兼容使用這兩種方式。
閱讀全文 Engine.io介紹

發佈日期:

Redis Sentinel

Sentinel特性

Redis Sentinel為Redis提供高可用性。實際上,這意味著使用Sentinel可以創建Redis部署,該部署可以在沒有人工干預的情況下抵抗某些類型的故障。

Redis Sentinel還提供其他附帶任務,例如監視,通知,並充當客戶端的配置提供程序。

這是宏觀上Sentinel功能的完整列表(即,大圖):

監控。Sentinel會不斷檢查您的主實例和副本實例是否按預期工作。
通知。Sentinel可以通過API通知系統管理員或其他計算機程序,其中一個受監視的Redis實例出了問題。
自動故障轉移。如果主服務器未按預期工作,則Sentinel可以啟動故障轉移過程,在該過程中將副本升級為主服務器,將其他附加副本重新配置為使用新的主服務器,並通知使用Redis服務器的應用程序要使用的新地址。連接時。
配置提供程序。Sentinel充當客戶端服務發現的授權來源:客戶端連接到Sentinels,以詢問負責給定服務的當前Redis主服務器的地址。如果發生故障轉移,Sentinels將報告新地址。

Redis Sentinel是一個分佈式系統:

Sentinel本身設計為在有多個Sentinel進程協同合作的配置中運行。具有多個Sentinel進程進行協作的優點如下:

當多個哨兵就給定的主機不再可用這一事實達成共識時,將執行故障檢測。這降低了誤報的可能性。
即使不是所有的Sentinel進程都在工作,Sentinel仍能正常工作,從而使系統能夠應對故障。畢竟,擁有故障轉移系統本身就是一個單點故障,這沒有任何樂趣。
閱讀全文 Redis Sentinel

發佈日期:

npm module – ioredis介紹

Redis介紹

Redis是REmote DIctionary Server(遠程字典服務器)的縮寫,它以字典結構(key-value鍵值對結構)存儲數據,並允許其他應用通過TCP協議讀寫字典中的內容。所以,redis是一個key-value存儲系統,或者說是一個key-value數據庫。

Redis的內存存儲和持久化Redis數據庫中的所有數據都存儲在內存中。由於內存的讀寫速度遠快於硬盤,因此Redis在性能上對比其他基於硬盤存儲的數據庫有非常明顯的優勢,在一臺普通的筆記本電腦上,edis可以在一秒內讀寫超過十萬個鍵值。將數據存儲在內存中也有問題,例如,程序退出後內存中的數據會丟失。不過 Redis提供了對持久化的支持,即將可以內存中的數據異步寫入到硬盤中,同時不影響繼續提供服務

ioredis介紹


ioredis是一個功能強大的功能強大的Redis客戶,已被世界上最大的在線商務公司阿里巴巴和許多其他了不起的公司所使用。
閱讀全文 npm module – ioredis介紹

發佈日期:

用Charles以本地/遠端內容取代網路回應

以本地檔案內容取代網路回應

有時我們在本地開發網頁功能時,若以API的方式去讀取資料,當我們在測試時可能會希望回傳某個固定的response。這時候就可以用Map Local的功能。

使用步驟
1、Save Response
選擇要模擬數據的接口,然後右鍵,選擇“Save Response”
閱讀全文 用Charles以本地/遠端內容取代網路回應