测试已测试 | ✓ |
语言编程语言 | Obj-CObjective C |
许可 | MIT |
发布最新发布 | 2016年3月 |
由 Alex Billingsley,DyKnow 技术团队,Joel Dart 维护。
SignalR-ObjC 是 iOS 和 Mac OS X 的客户端库。它构建在两个流行的开源库AFNetworking和SocketRocket之上。SignalR-ObjC 与 ASP.NET SignalR 一起使用,这是一个面向 ASP.NET 开发者的新库,可以轻松地将实时功能添加到您的应用程序中。什么是“实时Web”功能?这是服务器端代码在实时推送内容到连接客户端的能力。
使用 CocoaPods 将 SignalR-ObjC 集成到您的 Xcode 项目中,请在 Podfile 中指定它。
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'SignalR-ObjC', '~> 2.0'
然后,运行以下命令
$ pod install
Hub | |
---|---|
SRHubConnection | |
核心 | |
SRConnection | |
传输 | |
SRAutoTransport | SRAutoTransport 根据客户端和服务器支持的最佳传输进行选择。这是通过回退到性能较低的传输来实现的。 默认传输回退为: 1. SRWebSocketTransport(如果服务器支持) 2. SRServerSentEventsTransport 3. SRLongPollingTransport |
SRWebSocketTransport | WebSocket 是建立客户端和服务器之间真正持久、双向连接的唯一传输方式。 |
SRServerSentEventsTransport | 使用服务器发送事件(也称为 EventSource),服务器可以在任何时间向客户端推送新的数据,只需将消息推送到客户端即可。服务器发送事件与轮询相比需要更少的连接,因此将具有更低的延迟。 |
SRLongPollingTransport | 轮询不会创建持久连接,而是通过保持打开直到服务器响应的请求来轮询服务器。在服务器响应后关闭连接,然后立即请求新的连接。这可能导致一些延迟,因为连接重置。 |
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 "SignalR.h"
//Client
SRConnection *connection = [SRConnection connectionWithURL:@"https:///mysite/echo"];
// Register for connection lifecycle events
[connection setStarted:^{
NSLog(@"Connection Started");
[connection send:@"hello world"];
}];
[connection setReceived:^(NSString *message) {
NSLog(@"Connection Recieved Data: %@",message);
}];
[connection setConnectionSlow:^{
NSLog(@"Connection Slow");
}];
[connection setReconnecting:^{
NSLog(@"Connection Reconnecting");
}];
[connection setReconnected:^{
NSLog(@"Connection Reconnected");
}];
[connection setClosed:^{
NSLog(@"Connection Closed");
}];
[connection setError:^(NSError *error) {
NSLog(@"Connection Error %@",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 "SignalR.h"
// Connect to the service
SRHubConnection *hubConnection = [SRHubConnection connectionWithURL:@"https:///mysite"];
// Create a proxy to the chat service
SRHubProxy *chat = [hubConnection createHubProxy:@"chat"];
[chat on:@"addMessage" perform:self selector:@selector(addMessage:)];
// Register for connection lifecycle events
[hubConnection setStarted:^{
NSLog(@"Connection Started");
[connection send:@"hello world"];
}];
[hubConnection setReceived:^(NSString *message) {
NSLog(@"Connection Recieved Data: %@",message);
}];
[hubConnection setConnectionSlow:^{
NSLog(@"Connection Slow");
}];
[hubConnection setReconnecting:^{
NSLog(@"Connection Reconnecting");
}];
[hubConnection setReconnected:^{
NSLog(@"Connection Reconnected");
}];
[hubConnection setClosed:^{
NSLog(@"Connection Closed");
}];
[hubConnection setError:^(NSError *error) {
NSLog(@"Connection Error %@",error);
}];
// Start the connection
[hubConnection start];
- (void)addMessage:(NSString *)message {
// Print the message when it comes in
NSLog(message);
}
id qs = @{
@"param1": @1,
@"param2": @"another"
};
SRConnection *connection = [SRConnection connectionWithURL:@"https:///mysite" queryString:qs];
id qs = @{
@"param1": @1,
@"param2": @"another"
};
SRHubConnection *hubConnection = [SRHubConnection connectionWithURL:@"https:///mysite" queryString:qs];
id headers = @{
@"param1": @1,
@"param2": @"another"
};
SRConnection *connection = [SRConnection connectionWithURL:@"https:///mysite"];
[connection setHeaders:headers];
//Alternative Usage
SRConnection *connection = [SRConnection connectionWithURL:@"https:///mysite"];
[connection addValue:@"1" forHTTPHeaderField:@"param1"];
[connection addValue:@"another" forHTTPHeaderField:@"param2"];
id headers = @{
@"param1": @1,
@"param2": @"another"
};
SRHubConnection *hubConnection = [SRHubConnection connectionWithURL:@"https:///mysite"];
[hubConnection setHeaders:headers];
//Alternative Usage
SRHubConnection *hubConnection = [SRHubConnection connectionWithURL:@"https:///mysite"];
[hubConnection addValue:@"1" forHTTPHeaderField:@"param1"];
[hubConnection addValue:@"another" forHTTPHeaderField:@"param2"];
SignalR-ObjC 需要 iOS 7.0 或以上版本,或者 Mac OS 10.9(64位,支持现代 Cocoa 运行时)或以上版本。
SignalR-ObjC 可在 MIT 许可证下使用。更多信息请参阅许可证文件。
SignalR-ObjC 使用了第三方代码,每个代码段都有自己特定的许可证,请参阅感谢文件以获取贡献者信息。