webSocket开源框架:SocketRocket 简单的使用

需要用到webSocket,所以搜集了一下使用方法,

git下载地址:square/SocketRocket

gitHUB 上没有看懂,就要 cocoaPod 导入了 socketRocket

导入这个包然后添加所需要的框架

导入头文件

#import "SRWebSocket.h"

代码的实现

#import <UIKit/UIKit.h>
#import "SocketRocket.h"

@interface ViewController : UIViewController<SRWebSocketDelegate>

@property (nonatomic,strong)SRWebSocket *webSocket;


@end
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *showTxt;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated{
    [self Reconnect];
}

- (void)viewDidDisappear:(BOOL)animated{
    // Close WebSocket
    self.webSocket.delegate = nil;
    [self.webSocket close];
    self.webSocket = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];


}
//初始化
- (void)Reconnect{
    self.webSocket.delegate = nil;
    [self.webSocket close];
    
    self.webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://echo.websocket.org"]]];
    self.webSocket.delegate = self;
    
    self.title = @"Opening Connection...";
    
    [self.webSocket open];
}
 //成功连接
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
    NSLog(@"Websocket Connected");
    self.title = @"Connected!";
}
//连接失败,打印错误信息
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
    NSLog(@":( Websocket Failed With Error %@", error);
    self.title = @"Connection Failed! (see logs)";
    self.webSocket = nil;
}
//接收服务器发送信息
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
    NSLog(@"Received \"%@\"", message);
    self.showTxt.text = message;
}
// 长连接关闭
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"WebSocket closed");
    self.title = @"Connection Closed! (see logs)";
    self.webSocket = nil;
}
//该函数是接收服务器发送的pong消息
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload {
    NSString *reply = [[NSString alloc] initWithData:pongPayload encoding:NSUTF8StringEncoding];
    NSLog(@"%@",reply);
}

- (IBAction)sendAction:(id)sender {
    
    [self.view endEditing:YES];
    // WebSocket
    if (self.webSocket) {
        [self.webSocket send:@"test success"];
    }
}

有关这个方法需要说一下

-(void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload{
    socketPong = socketPong + 1;
}

其中最后一个是接受pong消息的,在这里就要提一下心跳包,一般情况下建立长连接都会建立一个心跳包,用于每隔一段时间通知一次服务端,客户端还是在线,这个心跳包其实就是一个ping消息,我的理解就是建立一个定时器,每隔十秒或者十五秒向服务端发送一个ping消息,这个消息可是是空的,例如
 NSData * data = [[NSData alloc]init];
  [_webSocket sendPing:data];
发送过去消息以后,服务器会返回一个pong消息,这个消息是解读不了的,但是每次返回时就会调用-(void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload这个函数,如果要用到话,可以像我做的那样,直接在该函数里面统计一下收到的次数,跟发送的次数比较,如果每次发送之前,自己发送的ping消息的个数,跟收到pong消息的个数相同,那就代表一直在连接状态,但是服务器发送pong消息时候是自动发送的,服务器是看不到客户端发送的ping消息的,同理,服务端也一样,只要服务端建立有心跳连接,那么服务端也是一直在发送ping消息,客户端一直在回复pong消息,这两个消息是处理不了的,没办法解读这个歌消息体里面的内容。还有一点就是在发送ping消息的时候,如果非要想在里面加入一些参数,这个参数不能太大,太大的话,会提示发送失败

//server的例子参考

//http://haoningabc.iteye.com/blog/2124605

 

posted @ 2016-10-09 14:01  _水畔竹汐  阅读(8235)  评论(0编辑  收藏  举报