Swift开发Socket.IO客户端:Socket.IO-Client-Swift

分享到:

wift的Socket.IO客户端。支持ws/wss/polling connections 和binary。适合 socket.io 1.0+ 和 Swift 1.1.

For Swift 1.2 use the 1.2 branch.

安装

  1. Requires linking SocketRocket against your xcode project. (Be sure to link the frameworks required by SocketRocket)
  2. Create a bridging header for SocketRocket
  3. Copy the SwiftIO folder into your xcode project

API

Constructor

init(socketURL: String, opts:[String: AnyObject]? = nil)- Constructs a new client for the given URL. opts can be omitted (will use default values. See example)

方法

  1. socket.on(name:String, callback:((data:NSArray?, ack:AckEmitter?) -> Void))- Adds a handler for an event. Items are passed by an array.ackcan be used to send an ack when one is requested. See example.
  2. socket.onAny(callback:((event:String, items:AnyObject?)) -> Void)- Adds a handler for all events. It will be called on any received event.
  3. socket.emit(event:String, args:AnyObject...)- Sends a message. Can send multiple args.
  4. socket.emitWithAck(event:String, args:AnyObject...) -> SocketAckHandler- Sends a message that requests an acknowledgement from the server. Returns a SocketAckHandler which you can use to add an onAck handler. See example.
  5. socket.connect()- Establishes a connection to the server. A "connect" event is fired upon successful connection.
  6. socket.connectWithParams(params:[String: AnyObject])- Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection.
  7. socket.close()- Closes the socket. Once a socket is closed it should not be reopened.

事件

  1. connect- Emitted when on a successful connection.
  2. disconnect- Emitted when the connection is closed.
  3. error- Emitted if the websocket encounters an error.
  4. reconnect- Emitted when the connection is starting to reconnect.
  5. reconnectAttempt- Emitted when attempting to reconnect.

 

示例

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
昵    称:
验证码:

相关文档: