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.
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()
//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()
let qs = [
"param1": "1",
"param2": "another"
}
var connection = Connection(withUrl: "http://localhost/endpoint", queryString: qs)
let qs = [
"param1": "1",
"param2": "another"
}
var hubConnection = HubConnection(withUrl: "http://localhost/endpoint", queryString: qs)
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")
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")
- SignalR-Swift uses Alamofire. The minimum supported version of AlamoFire is 5.x
- SignalR-Swift uses Starscream.
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