非常教程

Socket.IO参考手册

指南 | Guides

Client API

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

使用全局IO, 如果使用独立构建(例如:window/socket.io/socket.io.js或CDN),或者require('socket.io-client')调用的结果

被调用时,它会Manager为给定的URL 创建一个新的对象,并尝试重新使用现有的Manager对于后续调用,除非该multiplex选项被传递false。传递这个选项相当于'force new connection': true

其余的选项传递给Manager构造函数(详见下文)。

一个Socket实例返回由路径URL中指定的命名空间,默认为/。例如,如果urlhttp://localhost/users,则将建立一个传输连接并建立http://localhost一个Socket.IO连接/users

IO#protocol

此客户端使用的Socket.io协议修订号。

IO#Socket

引用Socket构造函数。

IO#Manager

引用Manager构造函数。

IO#Emitter

引用Emitter构造函数。

Manager(url:String, opts:Object)

Manager表示与给定Socket.IO服务器的连接。一个或多个Socket实例与管理端关联。管理端可以通过io每个Socket实例的属性进行访问。

这些opts也是engine.io在初始化底层的时候传递的Socket

选项: - reconnection是否自动重新连接(true) - reconnectionDelay尝试重新连接前等待的时间(1000) - reconnectionDelayMax重新连接之间等待的最长时间(5000)。每次尝试都会按照指定的数量增加重新连接reconnectionDelay。- connect_errorconnect_timeout事件之前的连接超时(20000

Events

  • connect。成功连接后激发。

  • connect_error。遇到连接错误时触发。

参数:

-  `Object` error objectconnect_timeout. Fired upon a connection timeout.reconnect. Fired upon a successful reconnection.

参数:

-  `Number` reconnection attempt numberreconnect_attempt. Fired upon an attempt to reconnect.reconnecting. Fired upon an attempt to reconnect.

参数:

-  `Number` reconnection attempt numberreconnect_error. Fired upon a reconnection attempt error.

参数:

-  `Object` error objectreconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts 

上面的事件也发生在重新连接的独立socket上,这取决于此Manager

Manager#reconnection(v:Boolean):Manager

设置reconnection选项,如果没有参数传递,则返回它。

Manager#reconnectionAttempts(v:Boolean):Manager

设置reconnectionAttempts选项,如果没有参数传递,则返回它。

Manager#reconnectionDelay(v:Boolean):Manager

设置reconectionDelay选项,如果没有参数传递,则返回它。

Manager#reconnectionDelayMax(v:Boolean):Manager

设置reconectionDelayMax选项,如果没有参数传递,则返回它。

Manager#timeout(v:Boolean):Manager

设置timeout选项,如果没有参数传递,则返回它。

Socket

Events

  • connect。成功连接时触发。
  • error。遇到连接错误时触发

参数:

-  `Object` error datadisconnect. Fired upon a disconnection.reconnect. Fired upon a successful reconnection.

参数:

-  `Number` reconnection attempt numberreconnect_attempt. Fired upon an attempt to reconnect.reconnecting. Fired upon an attempt to reconnect.

参数:

-  `Number` reconnection attempt numberreconnect_error. Fired upon a reconnection attempt error.

参数:

-  `Object` error objectreconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts 
Socket.IO

Socket.IO 是一个面向实时 web 应用的 JavaScript 库。它使得服务器和客户端之间实时双向的通信成为可能。

主页 http://socket.io/
源码 https://github.com/socketio/socket.io
发布版本 1.4.5

Socket.IO目录

1.客户 | Client
2.指南 | Guides
3.服务 | Server