-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnectHelper.m
More file actions
227 lines (197 loc) · 8.02 KB
/
ConnectHelper.m
File metadata and controls
227 lines (197 loc) · 8.02 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*********************************************************************
* 版权所有 apj_zzz
*
* 文件名称: ConnectHelper
* 内容摘要: 即时通信连接管理类
* 其它说明: 实现文件
* 作 成 者: ZGD
* 完成日期: 2016年07月26日
* 修改记录1:
* 修改日期:
* 修 改 人:
* 修改内容:
* 修改记录2:
**********************************************************************/
#import "ConnectHelper.h"
#import "UserData.h"
#import "UUMessage.h"
#import "Common.h"
#import "BettingOddsView.h"
#import "KLCPopup.h"
#import "Base64.h"
#import "MJExtension.h"
#import "UserWebService.h"
static ConnectHelper *sui;
@implementation ConnectHelper
@synthesize roomSocket;
+ (ConnectHelper *)shareInstence
{
@synchronized(self) {
if (!sui)
{
sui = [[ConnectHelper alloc] init];
sui.roomSocket = [[SocketIOClient alloc] initWithSocketURL:[NSURL URLWithString:HC_HOST] config:@{@"log": @YES, @"forcePolling": @YES}];
}
return sui;
}
}
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
//{
// if ([keyPath isEqualToString:@"status"]) {
//
// if (sui.roomSocket.status == SocketIOClientStatusConnected && [[UserData getUserData] isLogin]) {
// NSMutableDictionary *ttDict = [NSMutableDictionary dictionaryWithCapacity:3];
// [ttDict setObject:[UserData getUserData].userID forKey:@"userID"];
// [ttDict setObject:[UserData getUserData].nickname forKey:@"nickName"];
// if ([UserData getUserData].face) {
// [ttDict setObject:[UserData getUserData].face forKey:@"icon"];
// }else{
// [ttDict setObject:@"userDefaultHeader" forKey:@"icon"];
// }
//
// [UserData getUserData].socDict = ttDict;
// // 登录服务器
// [[ConnectHelper shareInstence] connectLogin:[UserData getUserData].socDict];
// [[NSNotificationCenter defaultCenter] postNotificationName:@"userStatusNotification" object:nil];
// }else{
// [UserData getUserData].serverTime = @"-1";
// [[NSNotificationCenter defaultCenter] postNotificationName:@"userStatusNotification" object:nil];
// }
// }
//}
-(void)updateUserStatus
{
if (sui.roomSocket.status == SocketIOClientStatusConnected && [[UserData getUserData] isLogin]) {
NSMutableDictionary *ttDict = [NSMutableDictionary dictionaryWithCapacity:3];
[ttDict setObject:[UserData getUserData].userID forKey:@"userID"];
[ttDict setObject:[UserData getUserData].nickname forKey:@"nickName"];
if ([UserData getUserData].face) {
[ttDict setObject:[UserData getUserData].face forKey:@"icon"];
}else{
[ttDict setObject:@"userDefaultHeader" forKey:@"icon"];
}
[UserData getUserData].socDict = ttDict;
// 登录服务器
[[ConnectHelper shareInstence] connectLogin:[UserData getUserData].socDict];
[[NSNotificationCenter defaultCenter] postNotificationName:@"userStatusNotification" object:nil];
}else{
[UserData getUserData].serverTime = @"-1";
[[NSNotificationCenter defaultCenter] postNotificationName:@"userStatusNotification" object:nil];
}
}
// 连接服务器
-(void)connectHost
{
// 添加监听
// [self.roomSocket addObserver:self forKeyPath:@"handlers" options:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserStatus) name:@"userStatusChange" object:nil];
[sui.roomSocket connectWithTimeoutAfter:10 withHandler:^{
NSLog(@"连接超时");
}];
[sui.roomSocket on:@"reconnect" callback:^(NSArray * _Nonnull dataArray, SocketAckEmitter * _Nonnull ack) {
NSLog(@"重连sock信息:%@",dataArray);
}];
}
// 断开连接
-(void)disconnect
{
[sui.roomSocket disconnect];
}
// 登录用户到实时通讯
-(void)connectLogin:(NSDictionary *)userInfo
{
[sui.roomSocket emit:@"connectUser" with:@[userInfo]];
[sui.roomSocket on:@"bettingBack" callback:^(NSArray * _Nonnull dataArray, SocketAckEmitter * _Nonnull ack) {
NSLog(@"开奖结果揭晓:%@",dataArray);
[[NSNotificationCenter defaultCenter] postNotificationName:TimeNoticeUpdate object:nil];
if (dataArray.count != 0) {
[self apllyData:dataArray.firstObject];
}
}];
}
-(void)apllyData:(NSDictionary *)tDict
{
NSString *baseStr = tDict[@"key"];
if (baseStr) {
NSString *jsonStr = [baseStr base64DecodedString];
NSArray *tmpArray = [Common arrayWithJsonString:jsonStr];
NSMutableArray *tDataArray = [NSMutableArray arrayWithCapacity:1];
for (NSDictionary *ttDict in tmpArray) {
if ([[ttDict objectForKey:@"user_id"] isEqualToString:[UserData getUserData].userID]) {
[tDataArray addObject:ttDict];
}
}
if (tDataArray.count != 0) {
BettingOddsView *bodds = [[BettingOddsView alloc]init];
KLCPopup *pop = [KLCPopup popupWithContentView:bodds showType:KLCPopupShowTypeFadeIn dismissType:KLCPopupDismissTypeFadeOut maskType:KLCPopupMaskTypeDimmed dismissOnBackgroundTouch:YES dismissOnContentTouch:NO];
[pop show];
[bodds showOddsWithArray:tDataArray];
[UserWebService getUserInfo:[UserData getUserData].userID blcok:^(id aDict, int errCode) {
if (errCode == 0) {
return ;
}
NSString *code = [aDict objectForKey:@"code"];
if (code.intValue != 1) {
return;
}
NSDictionary *dataDict = aDict[@"data"];
NSString *money = [NSString stringWithFormat:@"%@",dataDict[@"money"]];
[UserData getUserData].money = money;
}];
}
}
}
// 加入房间
-(void)joinRoomChat:(NSDictionary *)userInfo roomID:(NSString *)roomId
{
if (userInfo) {
[sui.roomSocket emit:@"join" with:@[userInfo,roomId]];
}
// 房间消息监听
[sui.roomSocket on:@"roomInfo" callback:^(NSArray * _Nonnull dataArray, SocketAckEmitter * _Nonnull ack) {
if (self.roomDelegate) {
// [self.roomDelegate roomSystemTips:dataArray[0]];
[self.roomDelegate roomMemberList:dataArray[1]];
}
}];
// 房间消息监听
[sui.roomSocket on:@"fromMsg" callback:^(NSArray * _Nonnull dataArray, SocketAckEmitter * _Nonnull ack) {
if (self.roomDelegate) {
[self.roomDelegate roomMessageReceive:dataArray[0]];
}
}];
// 投注计时器监听
[sui.roomSocket on:@"bettingTime" callback:^(NSArray * _Nonnull dataArray, SocketAckEmitter * _Nonnull ack) {
if (dataArray.count != 0) {
[[NSNotificationCenter defaultCenter] postNotificationName:TimeCountUpDate object:dataArray.firstObject];
if (dataArray.firstObject) {
NSDictionary *tmpDict = dataArray.firstObject;
[UserData getUserData].serverTime = [NSString stringWithFormat:@"%@",tmpDict[@"serverTime"]];
}
}
}];
}
// 离开房间
-(void)leave
{
[UserData getUserData].bettingStatus = @"-1";
[sui.roomSocket emit:@"leave" with:@[]];
}
// 发送消息
-(void)sendRoomMessage:(NSDictionary *)msgInfo
{
[sui.roomSocket emit:@"toMsg" with:@[msgInfo]];
// 发送返回
[sui.roomSocket on:@"sendBack" callback:^(NSArray * _Nonnull dataArray, SocketAckEmitter * _Nonnull ack) {
if (self.roomDelegate) {
NSLog(@"消息发送返回:%@",dataArray);
if (dataArray.count > 0) {
NSDictionary *aDict = dataArray.firstObject;
if (aDict[@"messageId"] ) {
[UUMessage messageStatusUpdate:@"1" msgId:aDict[@"messageId"]];
}
}
}
}];
}
@end