Socket.io客戶端API

官方文檔直譯


原文地址:http://socket.io/docs/client-api/#


IO(url:String, opts:Object):Socket

暴露在window中作為全局的接口如果被單獨(dú)創(chuàng)建(例如:/socket.io/socket.io.js或者使用CDN)泽西,則結(jié)果應(yīng)該使用require('socket.io-client')昙衅。

Exposed as the io global in window if using the standalone build (eg: /socket.io/socket.io.js or the CDN), or the result of calling require('socket.io-client').

當(dāng)被調(diào)用,它會(huì)根據(jù)提供的URL創(chuàng)建一個(gè)新的Manager拖陆,并且將這個(gè)已經(jīng)存在的Manager返回給之后調(diào)用這個(gè)方法的對象怔接, 除非multiplex選項(xiàng)被設(shè)置為false。設(shè)置這個(gè)選項(xiàng)等價(jià)于設(shè)置'force new connection': true

When called, it creates a new Manager for the given URL, and attempts to reuse an existing Manager for subsequent calls, unless the multiplex option is passed with false. Passing this option is the equivalent of passing 'force new connection': true.

其余的選項(xiàng)被傳遞給Manager的構(gòu)造函數(shù)(詳細(xì)說明見下面)钞翔。

The rest of the options are passed to the Manager constructor (see below for details).

一個(gè)Socket實(shí)例是在URL中通過路徑名返回指定的命名空間,默認(rèn)為/席舍。例如布轿,如果一個(gè)urlhttp://localhost/users,一個(gè)傳輸連接將被與http://localhost建立,并且 一個(gè) Socket.IO 連接將會(huì)被與/users建立

A Socket instance is returned for the namespace specified by the pathname in the URL, defaulting to /. For example, if the url is http://localhost/users, a transport connection will be established to http://localhost and a Socket.IO connection will be established to
/users.

IO#協(xié)議

客戶端工作的Socket.io協(xié)議版本號(hào)汰扭。

IO#Socket

參考Socket構(gòu)造函數(shù)稠肘。

IO#Manager

參考Manager構(gòu)造函數(shù)。

IO#Emitter

參考Emitter構(gòu)造函數(shù)萝毛。

Manager(url:String, opts:Object)

一個(gè)Manager代表著一個(gè)和Socket.IO服務(wù)建立的鏈接项阴。一個(gè)或更多Socket實(shí)例都是和這個(gè)manager又聯(lián)系的。對于 每個(gè)Socket實(shí)例都可以通過io來被這個(gè)manager聯(lián)系笆包。

A Manager represents a connection to a given Socket.IO server. One or more Socket instances are associated with the manager. The manager can be accessed through the io property of each Socket instance.

opts通常在Socket初始化時(shí)傳遞給底層的engine.io环揽。

The opts are also passed to engine.io upon initialization of the underlying Socket.

選項(xiàng):

  • reconnection是否自動(dòng)重新連接(true)。
  • reconnectionDelay多長時(shí)間后建立一個(gè)新的連接(1000)庵佣。
  • reconnectionDelayMax重新連接的最大等待時(shí)間(5000)歉胶。每一個(gè)嘗試重新連接都由reconnectionDelay在指定的數(shù)量上增加。
  • timeout當(dāng)一個(gè)connect_errorconnect_timeout事件被發(fā)送之前的連接超時(shí)巴粪。
  • reconnection whether to reconnect automatically (true)
  • reconnectionDelay how long to wait before attempting a new reconnection (1000)
  • reconnectionDelayMax maximum amount of time to wait between reconnections (5000). Each attempt increases the reconnection by the amount specified by reconnectionDelay.
  • timeout connection timeout before a connect_error and connect_timeout events are emitted (20000)

事件

  • connect通今。有一個(gè)成功的連接則發(fā)送該事件。
  • connect_error肛根。有一個(gè)鏈接錯(cuò)誤則發(fā)送該事件辫塌。
    參數(shù):
    • Object 錯(cuò)誤對象
  • connect_timeout。有一個(gè)鏈接超時(shí)則發(fā)送該事件派哲。
  • reconnect臼氨。有一個(gè)成功的重新連接則發(fā)送該事件。
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_attempt芭届。開始一個(gè)重新連接則發(fā)送該事件储矩。
  • reconnecting。正在重新連接則發(fā)送該事件喉脖。
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_error椰苟。有一個(gè)重新連接的錯(cuò)誤則發(fā)送該事件。
    參數(shù):
    • Object 錯(cuò)誤對象
  • reconnect_failed树叽。在reconnectionAttempts內(nèi)無法重新連接則發(fā)送該事件舆蝴。
  • connect. Fired upon a successful connection.
  • connect_error. Fired upon a connection error.
    Parameters:
    • Object error object
  • connect_timeout. Fired upon a connection timeout.
  • reconnect. Fired upon a successful reconnection.
    Parameters:
    • Number reconnection attempt number
  • reconnect_attempt. Fired upon an attempt to reconnect.
  • reconnecting. Fired upon an attempt to reconnect.
    Parameters:
    • Number reconnection attempt number
  • reconnect_error. Fired upon a reconnection attempt error.
    Parameters:
    • Object error object
  • reconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts

取決于Manager,上面的事件也在獨(dú)立的sockets重新連接的時(shí)候被發(fā)送题诵。

The events above are also emitted on the individual sockets that reconnect that depend on this Manager.

Manager#reconnection(v:Boolean):Manager

設(shè)置reconnection的選項(xiàng)洁仗,如果沒有傳參那么就返回它自己。

Manager#reconnectionAttempts(v:Boolean):Manager

設(shè)置reconnectionAttempts選項(xiàng)性锭,如果沒有傳參那么就返回它自己赠潦。

Manager#reconnectionDelay(v:Boolean):Manager

設(shè)置reconectionDelay選項(xiàng),如果沒有傳參那么就返回它自己草冈。

Manager#reconnectionDelayMax(v:Boolean):Manager

設(shè)置reconectionDelayMax選項(xiàng)她奥,如果沒有傳參那么就返回它自己瓮增。

Manager#timeout(v:Boolean):Manager

設(shè)置timeout選項(xiàng),如果沒有傳參那么就返回它自己哩俭。

Socket

事件

  • connect绷跑。鏈接中則發(fā)送該事件。
  • error凡资。有一個(gè)鏈接錯(cuò)誤則發(fā)送該事件
    參數(shù):
    • Object 錯(cuò)誤對象
  • disconnect砸捏。斷開連接則發(fā)送該事件。
  • reconnect隙赁。有一個(gè)成功的重新連接則發(fā)送該事件
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_attempt垦藏。開始一個(gè)重新連接則發(fā)送該事件。
  • reconnecting伞访。正在重新連接則發(fā)送該事件掂骏。
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_error。有一個(gè)重新連接的錯(cuò)誤則發(fā)送該事件咐扭。
    參數(shù):
    • Object 錯(cuò)誤對象
  • reconnect_failed芭挽。在reconnectionAttempts內(nèi)無法重新連接則發(fā)送該事件滑废。
  • connect. Fired upon connecting.
  • error. Fired upon a connection error
    Parameters:
    • Object error data
  • disconnect. Fired upon a disconnection.
  • reconnect. Fired upon a successful reconnection.
    Parameters:
    • Number reconnection attempt number
  • reconnect_attempt. Fired upon an attempt to reconnect.
  • reconnecting. Fired upon an attempt to reconnect.
    Parameters:
    • Number reconnection attempt number
  • reconnect_error. Fired upon a reconnection attempt error.
    Parameters:
    • Objecterror object
  • reconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蝗肪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蠕趁,更是在濱河造成了極大的恐慌薛闪,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,423評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件俺陋,死亡現(xiàn)場離奇詭異豁延,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)腊状,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,147評論 2 385
  • 文/潘曉璐 我一進(jìn)店門诱咏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人缴挖,你說我怎么就攤上這事袋狞。” “怎么了映屋?”我有些...
    開封第一講書人閱讀 157,019評論 0 348
  • 文/不壞的土叔 我叫張陵苟鸯,是天一觀的道長。 經(jīng)常有香客問我棚点,道長早处,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,443評論 1 283
  • 正文 為了忘掉前任瘫析,我火速辦了婚禮砌梆,結(jié)果婚禮上默责,老公的妹妹穿的比我還像新娘。我一直安慰自己咸包,他們只是感情好傻丝,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,535評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著诉儒,像睡著了一般葡缰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上忱反,一...
    開封第一講書人閱讀 49,798評論 1 290
  • 那天泛释,我揣著相機(jī)與錄音,去河邊找鬼温算。 笑死怜校,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的注竿。 我是一名探鬼主播茄茁,決...
    沈念sama閱讀 38,941評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼巩割!你這毒婦竟也來了裙顽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,704評論 0 266
  • 序言:老撾萬榮一對情侶失蹤宣谈,失蹤者是張志新(化名)和其女友劉穎愈犹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體闻丑,經(jīng)...
    沈念sama閱讀 44,152評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡漩怎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,494評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了嗦嗡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勋锤。...
    茶點(diǎn)故事閱讀 38,629評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖侥祭,靈堂內(nèi)的尸體忽然破棺而出叁执,到底是詐尸還是另有隱情,我是刑警寧澤卑硫,帶...
    沈念sama閱讀 34,295評論 4 329
  • 正文 年R本政府宣布徒恋,位于F島的核電站,受9級(jí)特大地震影響欢伏,放射性物質(zhì)發(fā)生泄漏入挣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,901評論 3 313
  • 文/蒙蒙 一硝拧、第九天 我趴在偏房一處隱蔽的房頂上張望径筏。 院中可真熱鬧葛假,春花似錦、人聲如沸滋恬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恢氯。三九已至带斑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間勋拟,已是汗流浹背勋磕。 一陣腳步聲響...
    開封第一講書人閱讀 31,978評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留敢靡,地道東北人挂滓。 一個(gè)月前我還...
    沈念sama閱讀 46,333評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像啸胧,于是被迫代替她去往敵國和親赶站。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,499評論 2 348

推薦閱讀更多精彩內(nèi)容