Skip to content

Commit

Permalink
哎~ 不测试提交代码 就是问题多多 :(
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuruhong committed Jul 29, 2016
1 parent 194b89b commit 8f67a7b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Example/Example/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "ViewController.h"
#import "RHSocketChannel.h"
#import "RHSocketChannel+Heartbeat.h"

#import "RHSocketStringEncoder.h"
#import "RHSocketStringDecoder.h"
Expand Down Expand Up @@ -292,6 +293,13 @@ - (void)channelOpened:(RHSocketChannel *)channel host:(NSString *)host port:(int
req = [[RHSocketPacketRequest alloc] init];
req.object = @{@"key":@"RHSocketJSONSerializationEncoder"};
[channel asyncSendPacket:req];

/**
* 哎~ 不测试提交代码 就是问题多多 :(
* 测试心跳方法,断开连接后,记得调用stopHeartbeatTimer和清理heartbeat
*/
channel.heartbeat = req;
[channel startHeartbeatTimer:30];
}

- (void)channelClosed:(RHSocketChannel *)channel error:(NSError *)error
Expand Down
7 changes: 7 additions & 0 deletions RHSocketKit/Core/Channel/RHSocketChannel+Heartbeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

@interface RHSocketChannel (Heartbeat)

/**
* 固定心跳包
*/
@property (nonatomic, strong) id<RHUpstreamPacket> heartbeat;

/**
* 心跳定时器,这里使用的是NSTimer,在断开连接后,需要手动停止心跳定时器(使用MSWeakTimer更好)
*/
@property (nonatomic, strong) NSTimer *heartbeatTimer;

- (void)stopHeartbeatTimer;
Expand Down
27 changes: 24 additions & 3 deletions RHSocketKit/Core/Channel/RHSocketChannel+Heartbeat.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@
//

#import "RHSocketChannel+Heartbeat.h"
#import <objc/runtime.h>

static char rh_heartbeatKey;
static char rh_heartbeatTimerKey;

@implementation RHSocketChannel (Heartbeat)

@dynamic heartbeat;
@dynamic heartbeatTimer;
- (id<RHUpstreamPacket>)heartbeat
{
return objc_getAssociatedObject(self, &rh_heartbeatKey);
}

- (void)setHeartbeat:(id<RHUpstreamPacket>)heartbeat
{
objc_setAssociatedObject(self, &rh_heartbeatKey, heartbeat, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimer *)heartbeatTimer
{
return objc_getAssociatedObject(self, &rh_heartbeatTimerKey);
}

- (void)setHeartbeatTimer:(NSTimer *)heartbeatTimer
{
objc_setAssociatedObject(self, &rh_heartbeatTimerKey, heartbeatTimer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)stopHeartbeatTimer
{
Expand All @@ -23,7 +44,7 @@ - (void)stopHeartbeatTimer

- (void)startHeartbeatTimer:(NSTimeInterval)interval
{
NSTimeInterval minInterval = MIN(5, interval);
NSTimeInterval minInterval = MAX(5, interval);
[self stopHeartbeatTimer];
self.heartbeatTimer = [NSTimer scheduledTimerWithTimeInterval:minInterval target:self selector:@selector(heartbeatTimerFunction) userInfo:nil repeats:YES];
}
Expand Down
5 changes: 5 additions & 0 deletions RHSocketKit/Core/Channel/RHSocketChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ - (void)closeConnection

- (void)asyncSendPacket:(id<RHUpstreamPacket>)packet
{
if (nil == packet) {
RHSocketLog(@"Warning: RHSocket asyncSendPacket packet is nil ...");
return;
};

if (nil == _encoder) {
RHSocketLog(@"RHSocket Encoder should not be nil ...");
return;
Expand Down

0 comments on commit 8f67a7b

Please sign in to comment.