ActionCableClient 0.2.3

ActionCableClient 0.2.3

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布最新发布2016年11月
SwiftSwift 版本3.0
SPM支持 SPM

Daniel Rhodes维护。



ActionCableClient

ActionCable 是 Rails 5 中随 Rails 5 一同推出的新 WebSocket 服务器,它使您能够轻松地向应用程序添加实时功能。此 Swift 客户端使得与该服务器相连变得无比简单,抽象掉了除了启动所需之外的所有内容。

安装

要安装,请简单地进行以下操作:

使用

入门 & 连接

import ActionCableClient

self.client = ActionCableClient(url: URL(string: "ws://domain.tld/cable")!)

// Connect!
client.connect()

client.onConnected = {
    print("Connected!")
}

client.onDisconnected = {(error: Error?) in
    print("Disconnected!")
}

订阅一个频道

// Create the Room Channel
let roomChannel = client.create("RoomChannel") //The channel name must match the class name on the server

// More advanced usage
let room_identifier = ["room_id" : identifier]
let roomChannel = client.create("RoomChannel", identifier: room_identifier, autoSubscribe: true, bufferActions: true)

频道回调

// Receive a message from the server. Typically a Dictionary.
roomChannel.onReceive = { (JSON : Any?, error : ErrorType?) in
    print("Received", JSON, error)
}

// A channel has successfully been subscribed to.
roomChannel.onSubscribed = {
    print("Yay!")
}

// A channel was unsubscribed, either manually or from a client disconnect.
roomChannel.onUnsubscribed = {
    print("Unsubscribed")
}

// The attempt at subscribing to a channel was rejected by the server.
roomChannel.onRejected = {
    print("Rejected")
}

在频道上执行操作

// Send an action
roomChannel["speak"](["message": "Hello, World!"])

// Alternate less magical way:
roomChannel.action("speak", ["message": "Hello, World!"])

// Note: The `speak` action must be defined already on the server

授权 & 头部信息

// ActionCable can be picky about origins, so if you
// need it can be set here.
client.origin = "https://domain.tld/"

// If you need any sort of authentication, you 
// will not have cookies like you do in the browser,
// so set any headers here.
//
// These are available in the `Connection`
// on the server side.

client.headers = [
    "Authorization": "sometoken"
]

杂项

client.onPing = {

}

有关更多文档,请参阅wiki

要求

Starscream:底层的 WebSocket 库。

作者

Daniel Rhodes, [email protected]

许可协议

ActionCableClient 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。