宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
//  RHSocketService.m
//  RHSocketDemo
//
//  Created by zhuruhong on 15/6/18.
//  Copyright (c) 2015年 zhuruhong. All rights reserved.
//
 
#import "RHSocketService.h"
 
NSString *const kNotificationSocketServiceState = @"kNotificationSocketServiceState";
NSString *const kNotificationSocketPacketRequest = @"kNotificationSocketPacketRequest";
NSString *const kNotificationSocketPacketResponse = @"kNotificationSocketPacketResponse";
 
@interface RHSocketService ()
 
@end
 
@implementation RHSocketService
 
+ (instancetype)sharedInstance
{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
 
- (instancetype)init
{
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectSocketPacketRequest:) name:kNotificationSocketPacketRequest object:nil];
    }
    return self;
}
 
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
#pragma mark - setter & getter
 
- (RHChannelBeats *)channelBeats
{
    if (nil == _channelBeats) {
        _channelBeats = [[RHChannelBeats alloc] init];
        
        __weak typeof(self) weakSelf = self;
        _channelBeats.beatBlock = ^(RHChannelBeats *channelBeats) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (!strongSelf.connectParam.heartbeatEnabled) {
                return;
            }
            [strongSelf asyncSendPacket:strongSelf.heartbeat];
        };
    }
    return _channelBeats;
}
 
#pragma mark - public function
 
- (void)startServiceWithHost:(NSString *)host port:(int)port
{
    RHSocketConnectParam *connectParam = [[RHSocketConnectParam alloc] init];
    connectParam.host = host;
    connectParam.port = port;
    [self startServiceWithConnectParam:connectParam];
}
 
- (void)startServiceWithConnectParam:(RHSocketConnectParam *)connectParam
{
    if (self.isRunning) {
        return;
    }
    
    _connectParam = connectParam;
    
    __weak typeof(self) weakSelf = self;
    [self startWithConfig:^(RHChannelConfig *config) {
        config.connectParam = weakSelf.connectParam;
        config.encoder = weakSelf.encoder;
        config.decoder = weakSelf.decoder;
        config.channelBeats = weakSelf.channelBeats;
        config.delegate = self;
    }];
    
}
 
- (void)stopService
{
    [super stopService];
    [self.channelBeats stop];
}
 
#pragma mar - recevie response data
 
- (void)detectSocketPacketRequest:(NSNotification *)notif
{
    id object = notif.object;
    [self asyncSendPacket:object];
}
 
#pragma mark -
 
#pragma mark - RHSocketChannelDelegate
 
- (void)channelOpened:(RHSocketChannel *)channel host:(NSString *)host port:(int)port
{
    [super channelOpened:channel host:host port:port];
    
    NSDictionary *userInfo = @{@"host":host, @"port":@(port), @"isRunning":@(self.isRunning)};
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationSocketServiceState object:@(self.isRunning) userInfo:userInfo];
}
 
- (void)channelClosed:(RHSocketChannel *)channel error:(NSError *)error
{
    [super channelClosed:channel error:error];
    
    NSDictionary *userInfo = @{@"isRunning":@(self.isRunning)};
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationSocketServiceState object:@(self.isRunning) userInfo:userInfo];
}
 
- (void)channel:(RHSocketChannel *)channel received:(id<RHDownstreamPacket>)packet
{
    [super channel:channel received:packet];
    
    NSDictionary *userInfo = @{@"RHSocketPacket":packet};
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationSocketPacketResponse object:nil userInfo:userInfo];
}
 
@end