项目作者: ScaleDrone

项目描述 :
Swift Client for Scaledrone Realtime Messaging Service (WIP)
高级语言: Swift
项目地址: git://github.com/ScaleDrone/Scaledrone-Swift.git
创建时间: 2017-08-14T16:08:10Z
项目社区:https://github.com/ScaleDrone/Scaledrone-Swift

开源协议:MIT License

下载


Scaledrone Swift

Use the Scaledrone Swift client to connect to the Scaledrone realtime messaging service

This project is still a work in progress, pull requests and issues are very welcome.

Installation

CocoaPods

Check out Get Started tab on cocoapods.org.

To use Scaledrone in your project add the following ‘Podfile’ to your project

  1. pod 'Scaledrone', '~> 0.5.2'

Then run:

  1. pod install

Carthage

Check out the Carthage Quick Start instructions.

To use Scaledrone with Carthage, add the following to your Cartfile:

  1. github "ScaleDrone/Scaledrone-Swift"

Then run:

  1. carthage update

After that, follow the instructions on Carthage’s docs.

Swift Package Manager

Use Xcode to add this repo as a package. Search for https://github.com/ScaleDrone/Scaledrone-Swift.

Usage

First thing is to import the framework. See the Installation instructions on how to add the framework to your project.

  1. import Scaledrone

Once imported, you can connect to Scaledrone.

  1. scaledrone = Scaledrone(channelID: "your-channel-id")
  2. scaledrone.delegate = self
  3. scaledrone.connect()

After you are connected, there are some delegate methods that we need to implement.

scaledroneDidConnect

  1. func scaledroneDidConnect(scaledrone: Scaledrone, error: Error?) {
  2. print("Connected to Scaledrone")
  3. }

scaledroneDidReceiveError

  1. func scaledroneDidReceiveError(scaledrone: Scaledrone, error: Error?) {
  2. print("Scaledrone error", error ?? "")
  3. }

scaledroneDidDisconnect

  1. func scaledroneDidDisconnect(scaledrone: Scaledrone, error: Error?) {
  2. print("Scaledrone disconnected", error ?? "")
  3. }

Authentication

Implement the ScaledroneAuthenticateDelegate protocol and set an additional delegate

  1. scaledrone.authenticateDelegate = self

Then use the authenticate method to authenticate using a JWT

  1. scaledrone.authenticate(jwt: "jwt_string")

scaledroneDidAuthenticate

  1. func scaledroneDidAuthenticate(scaledrone: Scaledrone, error: Error?) {
  2. print("Scaledrone authenticated", error ?? "")
  3. }

Sending messages

  1. scaledrone.publish(message: "Hello from Swift", room: "myroom")
  2. // Or
  3. room.publish(message: ["foo": "bar", "1": 2])

Subscribing to messages

Subscribe to a room and implement the ScaledroneRoomDelegate protocol, then set additional delegation

  1. let room = scaledrone.subscribe(roomName: "myroom")
  2. room.delegate = self

scaledroneRoomDidConnect

  1. func scaledroneRoomDidConnect(room: ScaledroneRoom, error: Error?) {
  2. print("Scaledrone connected to room", room.name, error ?? "")
  3. }

scaledroneRoomDidReceiveMessage

The member argument exists when the message was sent to an observable room using the socket API (not the REST API).

  1. func scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: ScaledroneMessage) {
  2. if message.member != nil {
  3. // This message was sent to an observable room
  4. // This message was sent through the socket API, not the REST API
  5. print("Received message from member:", message.memberID as Any)
  6. }
  7. let data = message.data
  8. if let messageData = data as? [String: Any] {
  9. print("Received a dictionary:", messageData)
  10. }
  11. if let messageData = data as? [Any] {
  12. print("Received an array:", messageData)
  13. }
  14. if let messageData = data as? String {
  15. print("Received a string:", messageData)
  16. }
  17. }

Observable rooms

Observable rooms act like regular rooms but provide additional functionality for keeping track of connected members and linking messages to members.

Adding data to the member object

Observable rooms allow adding custom data to a connected user. The data can be added in two ways:

  1. Passing the data object to a new instance of Scaledrone in your Swift code.

    1. let scaledrone = Scaledrone(channelID: "<channel_id>", data: ["name": "Swift", "color": "#ff0000"])

    This data can later be accessed like so:

    1. func scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {
    2. print("member joined with clientData", member.clientData)
    3. }
  2. Adding the data to the JSON Web Token as the data clause during authentication. This method is safer as the user has no way of changing the data on the client side.

    1. {
    2. "client": "client_id_sent_from_javascript_client",
    3. "channel": "channel_id",
    4. "data": {
    5. "name": "Swift",
    6. "color": "#ff0000"
    7. },
    8. "permissions": {
    9. "^main-room$": {
    10. "publish": false,
    11. "subscribe": false
    12. }
    13. },
    14. "exp": 1408639878000
    15. }

    This data can later be accessed like so:

    1. func scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {
    2. print("member joined with authData", member.authData)
    3. }

Receiving the observable events

Implement the ScaledroneObservableRoomDelegate protocol, then set additional delegation.

Observable room names need to be prefixed with observable-

  1. let room = scaledrone.subscribe(roomName: "observable-room")
  2. room.delegate = self
  3. room.observableDelegate = self

scaledroneObservableRoomDidConnect

  1. func scaledroneObservableRoomDidConnect(room: ScaledroneRoom, members: [ScaledroneMember]) {
  2. // The list will contain yourself
  3. print(members.map { (m: ScaledroneMember) -> String in
  4. return m.id
  5. })
  6. }

scaledroneObservableRoomMemberDidJoin

  1. func scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {
  2. print("member joined", member, member.id)
  3. }

scaledroneObservableRoomMemberDidLeave

  1. func scaledroneObservableRoomMemberDidLeave(room: ScaledroneRoom, member: ScaledroneMember) {
  2. print("member left", member, member.id)
  3. }

Message History

When creating a Scaledrone room you can supply the number of messages to recieve from that room’s history. The messages will arrive, in reverse chronological order and one by one, in scaledroneRoomDidReceiveMessage, just like real-time messages.

In order to recieve message history messages, this feature needs to be enabled in the Scaledrone dashboard. You can learn more about Message History and its limitations in Scaledrone docs.

  1. let room = scaledrone.subscribe(roomName: "chat-room", messageHistory: 50)

Basic Example

  1. import UIKit
  2. class ViewController: UIViewController, ScaledroneDelegate, ScaledroneRoomDelegate {
  3. let scaledrone = Scaledrone(channelID: "your-channel-id")
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. scaledrone.delegate = self
  7. scaledrone.connect()
  8. }
  9. func scaledroneDidConnect(scaledrone: Scaledrone, error: Error?) {
  10. print("Connected to Scaledrone channel", scaledrone.clientID)
  11. let room = scaledrone.subscribe(roomName: "notifications")
  12. room.delegate = self
  13. }
  14. func scaledroneDidReceiveError(scaledrone: Scaledrone, error: Error?) {
  15. print("Scaledrone error")
  16. }
  17. func scaledroneDidDisconnect(scaledrone: Scaledrone, error: Error?) {
  18. print("Scaledrone disconnected")
  19. }
  20. func scaledroneRoomDidConnect(room: ScaledroneRoom, error: Error?) {
  21. print("Scaledrone connected to room", room.name)
  22. }
  23. func scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: String) {
  24. print("Room received message:", message)
  25. }
  26. }

For a longer example see the ViewController.swift file.

Migration notes

0.5.0

Scaledrone 0.5.0 removes the use of NSError in favor of Error in the delegate methods, and adds support for Swift 5.

0.5.2:

scaledroneRoomDidReceiveMessage(room:message:member) was renamed to scaledroneRoomDidReceiveMessage(room:message:) and message is now of type ScaledroneMessage which includes the member and message IDs, the message’s time as well as the data that was sent.

Todo:

  • Automatic reconnection