socket.io-protocol

This document describes the Socket.IO protocol. For a reference JavaScript implementation, take a look atsocket.io-parser,socket.io-clientandsocket.io.

Protocol version

Current protocol revision:4.

Parser API

Parser#Encoder

An object that encodes socket.io packets to engine.io-transportable form. Its only public method is Encoder#encode.

Encoder#encode(Object:packet, Function:callback)

Encodes aPacketobject into an array of engine.io-compatible encodings. If the object is pure JSON the array will contain a single item, a socket.io encoded string. If the object contains binary data (ArrayBuffer, Buffer, Blob, or File) the array's first item will be a string with packet-relevant metadata and JSON with placeholders where the binary data was held in the initial packet. The remaining items will be the raw binary data to fill in the placeholders post-decoding.

The callback function is called with the encoded array as its only argument. In the socket.io-parser implementation, the callback writes each item in the array to engine.io for transport. The expectation for any implementation is that each item in the array is transported sequentially.

Parser#Decoder

An object that decodes data from engine.io into complete socket.io packets.

The expected workflow for using the Decoder is to call theDecoder#addmethod upon receiving any encoding from engine.io (in the sequential order in which the encodings are received) and to listen to the Decoder's 'decoded' events to handle fully decoded packets.

Decoder#add(Object:encoding)

Decodes a single encoded object from engine.io transport. In the case of a non-binary packet, the one encoding argument is used to reconstruct the full packet. If the packet is of typeBINARY_EVENTorACK, then additional calls to add are expected, one for each piece of binary data in the original packet. Once the final binary data encoding is passed to add, the full socket.io packet is reconstructed.

After a packet is fully decoded, the Decoder emits a 'decoded' event (via Emitter) with the decoded packet as the sole argument. Listeners to this event should treat the packet as ready-to-go.

Decoder#destroy()

Deallocates the Decoder instance's resources. Should be called in the event of a disconnect mid-decoding, etc. to prevent memory leaks.

Parser#types

Array of packet type keys.

Packet

Each packet is represented as a vanillaObjectwith anspkey that indicates what namespace it belongs to (see "Multiplexing") and atypekey that can be one of the following:

Packet#CONNECT(0)

Packet#DISCONNECT(1)

Packet#EVENT(2)

Packet#ACK(3)

Packet#ERROR(4)

Packet#BINARY_EVENT(5)

Packet#BINARY_ACK(6)

EVENT

data(Array) a list of arguments, the first of which is the event name. Arguments can contain any type of field that can result ofJSONdecoding, including objects and arrays of arbitrary size.

id(Number) if theididentifier is present, it indicates that the server wishes to be acknowledged of the reception of this event.

BINARY_EVENT

data(Array) seeEVENTdata, but with addition that any of the arguments may contain non-JSON arbitrary binary data. For encoding, binary data is considered either a Buffer, ArrayBuffer, Blob, or File. When decoding, all binary data server-side is a Buffer; on modern clients binary data is an ArrayBuffer. On older browsers that don't support binary, every binary data item is replaced with an object like so:{base64: true, data: }. When aBINARY_EVENTorACKpacket is initially decoded, all of the binary data items will be placeholders, and will be filled by additional calls toDecoder#add.

id(Number) seeEVENTid.

ACK

data(Array) seeEVENTdata. Encoded as string like theEVENTtype above. Should be used when an ACK function is not called with binary data.

id(Number) seeEVENTid.

BINARY_ACK

data(Array) seeACKdata. Used when the arguments for an ACK function contain binary data; encodes packet in theBINARY_EVENTstyle documented above.

id(Number) seeEVENTid.

ERROR

data(Mixed) error data

Transport

The socket.io protocol can be delivered over a variety of transports.socket.io-clientis the implementation of the protocol for the browser and Node.JS overengine.io-client.

socket.iois the server implementation of the protocol overengine.io.

Multiplexing

Socket.IO has built-in multiplexing support, which means that each packet always belongs to a givennamespace, identified by a path string (like/this). The corresponding key in thePacketobject isnsp.

When the socket.io transport connection is established, a connection attempt to the/namespace is assumed (ie: the server behaves as if the client had sent aCONNECTpacket to the/namespace).

In order to support multiplexing of multiple sockets under the same transport, additionalCONNECTpackets can be sent by the client to arbitrary namespace URIs (eg:/another).

When the server responds with aCONNECTpacket to the corresponding namespace, the multiplexed socket is considered connected.

Alternatively, the server can respond with anERRORpacket to indicate a multiplexed socket connection error, such as authentication errors. The associated error payload varies according to each error, and can be user-defined.

After aCONNECTpacket is received by the server for a givennsp, the client can then send and receiveEVENTpackets. If any of the parties receives anEVENTpacket with anidfield, anACKpacket is expected to confirm the reception of said packet.

License

MIT

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末绒疗,一起剝皮案震驚了整個濱河市势木,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖蜗巧,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乌逐,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機州刽,發(fā)現死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浪箭,“玉大人穗椅,你說我怎么就攤上這事∧唐埽” “怎么了匹表?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵门坷,是天一觀的道長。 經常有香客問我袍镀,道長默蚌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任苇羡,我火速辦了婚禮绸吸,結果婚禮上,老公的妹妹穿的比我還像新娘设江。我一直安慰自己锦茁,他們只是感情好,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布叉存。 她就那樣靜靜地躺著码俩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪歼捏。 梳的紋絲不亂的頭發(fā)上稿存,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機與錄音瞳秽,去河邊找鬼挠铲。 笑死,一個胖子當著我的面吹牛寂诱,可吹牛的內容都是我干的拂苹。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼痰洒,長吁一口氣:“原來是場噩夢啊……” “哼瓢棒!你這毒婦竟也來了?” 一聲冷哼從身側響起丘喻,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤脯宿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后泉粉,有當地人在樹林里發(fā)現了一具尸體连霉,經...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年嗡靡,在試婚紗的時候發(fā)現自己被綠了跺撼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡讨彼,死狀恐怖歉井,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情哈误,我是刑警寧澤哩至,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布躏嚎,位于F島的核電站,受9級特大地震影響菩貌,放射性物質發(fā)生泄漏卢佣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一箭阶、第九天 我趴在偏房一處隱蔽的房頂上張望珠漂。 院中可真熱鬧,春花似錦尾膊、人聲如沸媳危。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽待笑。三九已至,卻和暖如春抓谴,著一層夾襖步出監(jiān)牢的瞬間暮蹂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工癌压, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仰泻,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓滩届,卻偏偏與公主長得像集侯,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子帜消,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內容