Skip to content

SignalR client library written in pure Swift

License

Notifications You must be signed in to change notification settings

ARASHz4/SignalRSwift

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SignalR-Swift is a client library for iOS based on https://github.com/DyKnow/SignalR-ObjC by DyKnow. It's built on top of AlamoFire and Starscream. SignalR-Swift is intended to be used along side ASP.NET SignalR, a new library for ASP.NET developers that makes it incredibly simple to add real-time functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

Example Usage

Persistent Connection

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

//Server
public class MyConnection : PersistentConnection 
{
    protected override Task OnReceived(IRequest request, string connectionId, string data) 
    {
        // Broadcast data to all clients
        return Connection.Broadcast(data);
    }
}
import SignalRSwift

//Client
var connection = Connection(withUrl: "http://localhost/endpoint/");

// register for connection lifecycle events
connection.started = {
    print("Connected")
}

connection.reconnecting = {
    print("Reconnecting...")
}

connection.reconnected = {
    print("Reconnected.")
}

connection.closed = {
    print("Disconnected")
}

connection.connectionSlow = { print("Connection slow...") }

connection.error = { error in
  print("Error")
}

connection.start()

Hubs

//Server
public class Chat : Hub 
{
    public void Send(string message)
    {
        // Call the addMessage method on all clients            
        Clients.All.addMessage(message);
    }
}
//Client
import SignalRSwift

// Connect to the service
var hubConnection = HubConnection(withUrl: "http://localhost/endpoint")

var chat = hubConnection.createHubProxy(hubName: "chat")

chat.on(eventName: "addMessage") { (args) in
  if let message = args[0] {
    print("Message: \(message)")
  }
}

// register for connection lifecycle events
hubConnection.started = {
    print("Connected")
}

hubConnection.reconnecting = {
    print("Reconnecting...")
}

hubConnection.reconnected = {
    print("Reconnected.")
}

hubConnection.closed = {
    print("Disconnected")
}

hubConnection.connectionSlow = { print("Connection slow...") }

hubConnection.error = { error in
  print("Error")
}

hubConnection.start()

Customizing Query Params

Persistent Connections

let qs = [
   "param1": "1",
   "param2": "another"
}
var connection = Connection(withUrl: "http://localhost/endpoint", queryString: qs)

Hub Connections

let qs = [
   "param1": "1",
   "param2": "another"
}
var hubConnection = HubConnection(withUrl: "http://localhost/endpoint", queryString: qs)

Customizing Request Headers

Persistent Connections

let headers = [
   "param1": "1",
   "param2": "another"
]
var connection = Connection(withUrl: "http://localhost/endpoint", queryString: qs)
connection.headers = headers

// alternative usage
var connection = Connection(withUrl: "http://localhost/endpoint", queryString: qs)
connection.addValue(value: "1", forHttpHeaderField: "param1")
connection.addValue(value: "another", forHttpHeaderField: "param2")

Hub Connections

let headers = [
   "param1": "1",
   "param2": "another"
}
var hubConnection = HubConnection(withUrl: "http://localhost/endpoint", queryString: qs)
hubConnection.headers = headers

// alternative usage
var hubConnection = HubConnection(withUrl: "http://localhost/endpoint", queryString: qs)
hubConnection.addValue(value: "1", forHttpHeaderField: "param1")
hubConnection.addValue(value: "another", forHttpHeaderField: "param2")

Networking

  • SignalR-Swift uses Alamofire. The minimum supported version of AlamoFire is 5.x
  • SignalR-Swift uses Starscream.

LICENSE

SignalR-Swift is available under the MIT license. See the LICENSE file for more info.
SignalR-Swift uses 3rd-party code which each have specific licenses, see ACKNOWLEDGEMENTS for contributions

About

SignalR client library written in pure Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 99.7%
  • Objective-C 0.3%