鸟类歌声
是一个用于iOS和OS X的WebSocket客户端,用于搭配Phoenix Channels。支持Phoenix Presence!
从版本0.3.0开始,鸟类歌声需要Swift 3.0+。如果您需要Swift 2.2支持,请使用版本0.2.2。
从版本0.6开始,使用Starscream 3.0支持Swift 4。
使用
import Birdsong
…
// In your view controller / client
let socket = Socket(url: NSURL(string: "https://:4000/socket/websocket")!, params: ["key": "secret"])
…
socket.onConnect = {
let channel = self.socket.channel("rooms:some-topic", payload: ["user": "spartacus"])
channel.on("new:msg", callback: { message in
print("New message: \(message)")
})
channel.join()?.receive("ok", callback: { payload in
print("Successfully joined: \(channel.topic)")
})
channel.send("new:msg", payload: ["body": "Hello!"])
.receive("ok", callback: { response in
print("Sent a message!")
})
.receive("error", callback: { reason in
print("Message didn't send: \(reason)")
})
// Presence support.
channel.presence.onStateChange = { newState in
// newState = dict where key = unique ID, value = array of metas.
print("New presence state: \(newState)")
}
channel.presence.onJoin = { id, meta in
print("Join: user with id \(id) with meta entry: \(meta)")
}
channel.presence.onLeave = { id, meta in
print("Leave: user with id \(id) with meta entry: \(meta)")
}
}
socket.connect()
示例
要运行示例项目,克隆仓库,然后首先从Example目录运行`pod install`,然后使用Birdsong.xcworkspace Xcode工作区。示例被配置为直接与Phoenix Chat Example一起工作。它仅连接到`localhost:4000`,加入`rooms:lobby`通道,并记录收到的每条消息。“发送消息”按钮将向通道发送带有递增计数的消息。
安装
支持CocoaPods安装
platform :ios, '9.0'
use_frameworks!
pod 'Birdsong', '~> 0.6'
如果您需要Swift 2.2兼容性,请使用版本0.2.2
。
作者
Simon Manning — sjrmanning@github
许可证
Birdsong采用MIT许可证。更多信息请参阅LICENSE文件。