YBSocketLive.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. //
  2. // YBSocketLive.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/29.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBSocketLive.h"
  9. #import <SocketIO/SocketIO-Swift.h>
  10. #import "YBLinkAlertView.h"
  11. #import "YBAnchorPKAlert.h"
  12. @interface YBSocketLive(){
  13. NSDictionary *_roomDic;
  14. BOOL _isBusy;
  15. int _linkMicAskLast;
  16. NSTimer *_linkMicAskTimer; //连麦请求中倒计时
  17. NSString *_linkRequestUid;
  18. }
  19. @property(nonatomic,strong)SocketManager *socketManager;
  20. @property(nonatomic,strong)SocketIOClient *socketClient;
  21. @property(nonatomic,strong)YBLinkAlertView *linkAlertView;
  22. @property(nonatomic,strong)YBAnchorPKAlert *pkAlertView;
  23. @property(nonatomic,assign)BOOL hostLinking; //主播连麦中【包含用户-主播、主播-主播连麦】
  24. @end
  25. @implementation YBSocketLive
  26. static YBSocketLive*liveSocketManeger = nil;
  27. +(instancetype)liveSocketManeger {
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. liveSocketManeger = [[super allocWithZone:NULL]init];
  31. });
  32. return liveSocketManeger;
  33. }
  34. +(instancetype)allocWithZone:(struct _NSZone *)zone {
  35. return [self liveSocketManeger];
  36. }
  37. #pragma mark - socket 链接
  38. -(void)liveConnectSocket:(NSDictionary *)conDic {
  39. _linkMicAskLast = 10;
  40. _roomDic = conDic;
  41. _isBusy = NO;
  42. NSString *socketUrl = minstr([_roomDic valueForKey:@"chatserver"]);
  43. NSString *liveStream = minstr([_roomDic valueForKey:@"stream"]);
  44. _socketManager = [[SocketManager alloc]initWithSocketURL:[NSURL URLWithString:socketUrl] config:@{@"log": @NO, @"compress": @YES}];
  45. _socketClient = _socketManager.defaultSocket;
  46. NSArray *cur = @[@{@"username":[Config getOwnNicename],
  47. @"uid":[Config getOwnID],
  48. @"token":[Config getOwnToken],
  49. @"roomnum":[Config getOwnID],
  50. @"stream":liveStream,
  51. @"lang":[YBLanguageTools serviceLang],
  52. }];
  53. [_socketClient connect];
  54. [_socketClient on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
  55. [_socketClient emit:@"conn" with:cur];
  56. NSLog(@"socket链接");
  57. }];
  58. YBWeakSelf;
  59. [_socketClient on:@"conn" callback:^(NSArray* data, SocketAckEmitter* ack) {
  60. NSLog(@"进入房间");
  61. if (_isBusy && weakSelf.checkLiveingStatus) {
  62. weakSelf.checkLiveingStatus(@"主播-检查状态", @{}); //这里的"主播-检查状态"只是用中文表述事件,不必翻译
  63. }
  64. _isBusy = YES;
  65. }];
  66. [_socketClient on:@"broadcastingListen" callback:^(NSArray* data, SocketAckEmitter* ack) {
  67. /*
  68. if([[data[0] firstObject] isEqual:@"stopplay"]) {
  69. NSLog(@"%@",[data[0] firstObject]);
  70. if (weakSelf.superStopRoom) {
  71. weakSelf.superStopRoom(@"主播-超管关播", @{});//这里的"主播-超管关播"只是用中文表述事件,不必翻译
  72. }
  73. NSDictionary *contentDic = @{@"title":@"",@"msg":YZMsg(@"涉嫌违规,被下播"),@"left":@"",@"right":YZMsg(@"确定")};
  74. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  75. }];
  76. return ;
  77. }
  78. */
  79. for (NSString *path in data[0]) {
  80. NSDictionary *jsonArray = [path JSONValue];
  81. NSDictionary *msg = [[jsonArray valueForKey:@"msg"] firstObject];
  82. NSString *retcode = [NSString stringWithFormat:@"%@",[jsonArray valueForKey:@"retcode"]];
  83. if ([retcode isEqual:@"409002"]) {
  84. [MBProgressHUD showError:YZMsg(@"你已被禁言")];
  85. return;
  86. }
  87. NSString *method = [msg valueForKey:@"_method_"];
  88. [weakSelf getmessage:msg andMethod:method];
  89. }
  90. }];
  91. }
  92. -(void)getmessage:(NSDictionary *)msg andMethod:(NSString *)method{
  93. NSLog(@"收到socekt:%@",msg);
  94. if ([method isEqual:Soc_SendMsg]) {
  95. //文本消息
  96. NSString *msgtype = [msg valueForKey:@"msgtype"];
  97. if([msgtype isEqual:@"2"]) {
  98. //默认-聊天消息
  99. NSString *titleColor = @"0";
  100. NSString *ct = minstr([msg valueForKey:@"ct"]);
  101. if ([lagType isEqual:EN] && ![PublicObj checkNull:minstr([msg valueForKey:@"ct_en"])]) {
  102. ct = minstr([msg valueForKey:@"ct_en"]);
  103. }
  104. NSString *uname = minstr([msg valueForKey:@"uname"]);
  105. NSString *uid = minstr([msg valueForKey:@"uid"]);
  106. NSString *userType = minstr([msg valueForKey:@"usertype"]);
  107. NSString *guardType = minstr([msg valueForKey:@"guard_type"]);
  108. NSString *isAnchor = @"0";
  109. if (![PublicObj checkNull:minstr([msg valueForKey:@"heart"])]) {
  110. //说明是点亮消息
  111. titleColor = [@"light" stringByAppendingFormat:@"%@",[msg valueForKey:@"heart"]];
  112. if (self.userLight) {
  113. self.userLight(@"主播-用户点亮", @{});
  114. }
  115. }
  116. NSDictionary *chatDic = @{@"userName":uname,
  117. @"contentChat":ct,
  118. @"id":uid,
  119. @"titleColor":titleColor,
  120. @"usertype":userType,
  121. @"isAnchor":isAnchor,
  122. @"guard_type":guardType,
  123. };
  124. if (self.userSendMsg) {
  125. self.userSendMsg(@"主播-收到发言", chatDic);
  126. }
  127. }
  128. if([msgtype isEqual:@"0"]) {
  129. NSString *action = minstr([msg valueForKey:@"action"]);
  130. if ([action isEqual:@"1"] && self.userLeave) {
  131. //用户离开.离开的用户 id 在 ct 里边 'id'
  132. self.userLeave(@"主播-用户离开", msg);
  133. NSString *leaveuid = minstr([[msg valueForKey:@"ct"] valueForKey:@"id"]);
  134. if ((_linkAlertView && [_linkAlertView.applyUid isEqual:leaveuid]) || [leaveuid isEqual:_linkRequestUid]) {
  135. _hostLinking = NO;
  136. //退出的用户是发起连麦的人
  137. [self destroyLinkMicAskTimer];
  138. [self cancelLinkAlertShow];
  139. }
  140. }
  141. if ([action isEqual:@"0"] && self.userEnter) {
  142. //用户进入.离开的用户 id 在 ct 里边 'id'
  143. self.userEnter(@"主播-用户进入", msg);
  144. }
  145. }
  146. }
  147. else if ([method isEqual:Soc_SystemNot] || [method isEqual:Soc_KickUser] || [method isEqual:Soc_ShutUpUser] ||[method isEqual:Soc_setAdmin]){
  148. //系统消息、踢人、禁言、设置管理
  149. //在主播端这些类型的消息都只是在公屏显示一句话 即 [msg valueForKey:@"ct"]
  150. NSString *showCt = minstr([msg valueForKey:@"ct"]);
  151. if ([lagType isEqual:EN] && ![PublicObj checkNull:minstr([msg valueForKey:@"ct_en"])]) {
  152. showCt = minstr([msg valueForKey:@"ct_en"]);
  153. }
  154. NSDictionary *chatDic = @{@"userName":YZMsg(@"直播间消息"),
  155. @"contentChat":showCt,
  156. @"id":@"",
  157. @"titleColor":@"firstlogin",
  158. @"usertype":@"",
  159. @"isAnchor":@"",
  160. };
  161. if (self.systemMsg) {
  162. self.systemMsg(@"主播-系统消息", chatDic);
  163. }
  164. }
  165. else if ([method isEqual:Soc_SendGift]){
  166. NSLog(@"=====sendgift-get====:%@",msg);
  167. //送礼物
  168. if ([minstr([msg valueForKey:@"ifpk"]) isEqual:@"1"] && self.linkPKProgress) {
  169. self.linkPKProgress(@"主播-PK进度", msg);
  170. }
  171. if (self.sendGift && [minstr([msg valueForKey:@"roomnum"]) isEqual:[Config getOwnID]]) {
  172. self.sendGift(@"主播-收到礼物", msg);
  173. }
  174. }
  175. else if ([method isEqual:Soc_light]){
  176. //点亮
  177. NSString *msgtype = [msg valueForKey:@"msgtype"];
  178. NSString *action = [msg valueForKey:@"action"];
  179. if([msgtype isEqual:@"0"] && [action isEqual:@"2"] && self.userLight){
  180. self.userLight(@"主播-用户点亮", @{});
  181. }
  182. }
  183. else if ([method isEqual:Soc_stopLive]){
  184. //超管关播
  185. if (self.superStopRoom) {
  186. self.superStopRoom(@"主播-超管关播", @{});
  187. }
  188. // 警告框
  189. [YBLiveRoomAlertView showRoomWarning:msg];
  190. }
  191. else if ([method isEqual:Soc_StartEndLive]){
  192. //直播结束
  193. NSString *action = minstr([msg valueForKey:@"action"]);
  194. if ([action isEqual:@"19"] && self.otherDeviceLogin) {
  195. self.otherDeviceLogin(@"主播-其他设备登陆", @{});
  196. }
  197. if ([action isEqual:@"18"] && self.superStopRoom) {
  198. self.superStopRoom(@"主播-超管关播", @{});
  199. }
  200. }
  201. else if ([method isEqual:Soc_disconnect]){
  202. //断开链接
  203. NSString *action = [msg valueForKey:@"action"];
  204. if ([action isEqual:@"1"] && self.userLeave) {
  205. //用户离开.离开的用户 id 在 ct 里边 'id'
  206. self.userLeave(@"主播-用户离开", msg);
  207. NSString *leaveuid = minstr([[msg valueForKey:@"ct"] valueForKey:@"id"]);
  208. if ((_linkAlertView && [_linkAlertView.applyUid isEqual:leaveuid]) ||[leaveuid isEqual:_linkRequestUid]) {
  209. _hostLinking = NO;
  210. //退出的用户是发起连麦的人
  211. [self destroyLinkMicAskTimer];
  212. [self cancelLinkAlertShow];
  213. }
  214. }
  215. }
  216. else if ([method isEqual:Soc_BuyGuard]){
  217. //购买守护
  218. if (self.buyGuardMsg) {
  219. self.buyGuardMsg(@"主播-买守护", msg);
  220. }
  221. NSDictionary *chatDic = @{@"userName":YZMsg(@"直播间消息"),
  222. @"contentChat":[NSString stringWithFormat:@"%@ %@",[msg valueForKey:@"uname"],YZMsg(@"守护了主播")],
  223. @"id":@"",
  224. @"titleColor":@"firstlogin",
  225. @"usertype":@"",
  226. @"isAnchor":@"0",
  227. };
  228. if (self.systemMsg) {
  229. self.systemMsg(@"主播-系统消息", chatDic);
  230. }
  231. }
  232. else if ([method isEqual:Soc_ConnectVideo]){
  233. //用户-主播连麦
  234. // 1 有人发送连麦请求 2 主播接受连麦 3 主播拒绝连麦 4 用户推流,发送自己的播流地址 5 用户断开连麦 6 主播断开连麦 7 主播正忙碌 8 主播无响应
  235. int action = [minstr([msg valueForKey:@"action"]) intValue];
  236. switch (action) {
  237. case 1:{
  238. if (_linkMicAskLast != 10) {
  239. [self liveSendHostBusyTouser:minstr([msg valueForKey:@"uid"])];
  240. return;
  241. }
  242. if (_hostLinking) {
  243. [self liveSendHostBusyTouser:minstr([msg valueForKey:@"uid"])];
  244. return;
  245. }
  246. [self destroyLinkMicAskTimer];
  247. _linkMicAskLast = 10;
  248. _linkMicAskTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(linkMicAskEvent) userInfo:nil repeats:YES];
  249. _linkRequestUid = minstr([msg valueForKey:@"uid"]);
  250. [self showLinkAlert:msg];
  251. }break;
  252. case 4:{
  253. if ([_linkRequestUid isEqual:minstr([msg valueForKey:@"uid"])] && self.linkPlayMsg) {
  254. self.linkPlayMsg(@"主播-连麦用户流信息", msg);
  255. }
  256. }break;
  257. case 5:{
  258. _hostLinking = NO;
  259. if (self.linkUserDisconnect) {
  260. self.linkUserDisconnect(@"主播-连麦用户挂断", msg);
  261. }
  262. }break;
  263. default:
  264. break;
  265. }
  266. }
  267. else if ([method isEqual:Soc_LiveConnect]){
  268. //1:发起连麦;2;接受连麦;3:拒绝连麦;4:连麦成功通知;5.手动断开连麦;7:对方正忙碌 8:对方无响应
  269. int action = [minstr([msg valueForKey:@"action"]) intValue];
  270. switch (action) {
  271. case 1:{
  272. if (_linkMicAskLast != 10) {
  273. [self liveSendAnchorCtrOfAcntion:@"7" andExtDic:msg];
  274. return;
  275. }
  276. if (_hostLinking) {
  277. [self liveSendAnchorCtrOfAcntion:@"7" andExtDic:msg];
  278. return;
  279. }
  280. if (self.linkHostToHostRequest) {
  281. self.linkHostToHostRequest(@"主播-收到连麦请求", msg);
  282. }
  283. [self destroyLinkMicAskTimer];
  284. _linkMicAskLast = 10;
  285. _linkMicAskTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(linkMicAskEvent) userInfo:nil repeats:YES];
  286. _linkRequestUid = minstr([msg valueForKey:@"uid"]);
  287. [self showLinkAlert:msg];
  288. }break;
  289. case 3:{
  290. [MBProgressHUD showPop:YZMsg(@"对方主播拒绝了你的连麦申请")];
  291. _hostLinking = NO;
  292. }break;
  293. case 4:{
  294. [MBProgressHUD showPop:YZMsg(@"对方主播接受了您的连麦请求,开始连麦")];
  295. if (self.linkHostToHostMsg) {
  296. self.linkHostToHostMsg(@"主播-主播连麦成功", msg);
  297. }
  298. _hostLinking = YES;
  299. }break;
  300. case 5:{
  301. [MBProgressHUD showPop:YZMsg(@"连麦已断开")];
  302. if (self.linkHostToHostDisconnect) {
  303. self.linkHostToHostDisconnect(@"主播-主播断开", msg);
  304. }
  305. _hostLinking = NO;
  306. }break;
  307. case 7:{
  308. [MBProgressHUD showPop:YZMsg(@"对方正忙碌")];
  309. _hostLinking = NO;
  310. }break;
  311. case 8:{
  312. [MBProgressHUD showPop:YZMsg(@"对方无响应")];
  313. _hostLinking = NO;
  314. }break;
  315. default:
  316. break;
  317. }
  318. }
  319. else if ([method isEqual:Soc_LivePK]){
  320. NSLog(@"=====livePK-get====:%@",msg);
  321. //1:发起PK;2;接受PK;3:拒绝PK;4:PK成功通知;5.;7:对方正忙碌 8:对方无响应 9:PK结果
  322. int action = [minstr([msg valueForKey:@"action"]) intValue];
  323. switch (action) {
  324. case 1:{
  325. if (_pkAlertView) {//理论不会出现这种情况
  326. [self liveSendPkCtrAction:@"7"];
  327. return;
  328. }
  329. if (self.linkPKGetRequest) {
  330. self.linkPKGetRequest(@"主播-收到PK请求", msg);
  331. }
  332. [self showPKAlert:msg];
  333. }break;
  334. case 3:{
  335. [self cancelPKAlert];
  336. if (self.linkPKUnAgree) {
  337. self.linkPKUnAgree(@"主播-PK拒绝", msg);
  338. }
  339. [MBProgressHUD showPop:YZMsg(@"对方主播拒绝了您的PK请求")];
  340. }break;
  341. case 4:{
  342. [self cancelPKAlert];
  343. if (self.linkPKSuc) {
  344. self.linkPKSuc(@"主播-开始PK", msg);
  345. }
  346. }break;
  347. case 7:{
  348. [self cancelPKAlert];
  349. if (self.linkPKBusy) {
  350. self.linkPKBusy(@"主播-PK忙碌", msg);
  351. }
  352. [MBProgressHUD showError:YZMsg(@"对方正忙碌")];
  353. }break;
  354. case 8:{
  355. [self cancelPKAlert];
  356. if (self.linkPKTimeout) {
  357. self.linkPKTimeout(@"主播-PK超时", msg);
  358. }
  359. [MBProgressHUD showError:YZMsg(@"对方无响应")];
  360. }break;
  361. case 9:{
  362. if (self.linkPKResult) {
  363. self.linkPKResult(@"主播-PK结果", msg);
  364. }
  365. }break;
  366. default:
  367. break;
  368. }
  369. }
  370. else if ([method isEqual:Soc_SystemWarning]) {
  371. /// 直播警告
  372. [YBLiveRoomAlertView showRoomWarning:msg];
  373. }
  374. }
  375. -(void)showPKAlert:(NSDictionary *)dic {
  376. [self cancelPKAlert];
  377. _pkAlertView = [[YBAnchorPKAlert alloc]initWithFrame:CGRectMake(_window_width*0.15, _window_height/2-(_window_width*0.7/52*34)/2, _window_width*0.7, _window_width*0.7/52*34) andIsStart:NO];
  378. [[UIApplication sharedApplication].delegate.window addSubview:_pkAlertView];
  379. YBWeakSelf
  380. _pkAlertView.anchorPkEvent = ^(AnchorPkAlertType pkAlertType) {
  381. [weakSelf pkAlertCallBack:pkAlertType];
  382. };
  383. }
  384. -(void)pkAlertCallBack:(AnchorPkAlertType)pkAlertType {
  385. [self cancelPKAlert];
  386. switch (pkAlertType) {
  387. case PkAlertType_unAgree:{
  388. [self liveSendPkCtrAction:@"3"];
  389. if (self.linkPKUnAgree) {
  390. self.linkPKUnAgree(@"主播-PK拒绝", @{});
  391. }
  392. }break;
  393. case PkAlertType_Agree:{
  394. [self liveSendPkCtrAction:@"2"];
  395. }break;
  396. case PkAlertType_TimeOut:{
  397. [self liveSendPkCtrAction:@"8"];
  398. if (self.linkPKTimeout) {
  399. self.linkPKTimeout(@"主播-PK超时", @{});
  400. }
  401. }default:
  402. break;
  403. }
  404. }
  405. -(void)cancelPKAlert {
  406. if (_pkAlertView) {
  407. [_pkAlertView removeTimer];
  408. [_pkAlertView removeFromSuperview];
  409. _pkAlertView = nil;
  410. }
  411. }
  412. - (void)showLinkAlert:(NSDictionary *)dic{
  413. [self cancelLinkAlertShow];
  414. _linkAlertView = [[YBLinkAlertView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height) andUserMsg:dic];
  415. _linkAlertView.timeL.text = [NSString stringWithFormat:@"%@(10)...",YZMsg(@"发起连麦请求")];
  416. [[UIApplication sharedApplication].delegate.window addSubview:_linkAlertView];
  417. [_linkAlertView show];
  418. YBWeakSelf;
  419. _linkAlertView.linkAlertEvent = ^(BOOL isAgree,BOOL isHostLink) {
  420. _linkMicAskLast = 10;
  421. [weakSelf destroyLinkMicAskTimer];
  422. if (isAgree) {
  423. //连麦请求-同意
  424. if (isHostLink) {
  425. //主播连麦-检查开播状态
  426. [weakSelf checkLinkLive:dic];
  427. }else {
  428. //用户连麦
  429. [weakSelf liveSendHostIsAgree:YES andTouid:minstr([dic valueForKey:@"uid"])];
  430. _hostLinking = YES;
  431. }
  432. }else{
  433. //连麦-拒绝
  434. if (isHostLink) {
  435. [weakSelf liveSendAnchorCtrOfAcntion:@"3" andExtDic:dic];
  436. }else {
  437. //用户连麦
  438. [weakSelf liveSendHostIsAgree:NO andTouid:minstr([dic valueForKey:@"uid"])];
  439. }
  440. _hostLinking = NO;
  441. }
  442. };
  443. }
  444. //检查主播状态
  445. -(void)checkLinkLive:(NSDictionary *)checkDic {
  446. NSDictionary *postDic = @{@"stream":minstr([checkDic valueForKey:@"stream"]),@"uid_stream":minstr([_roomDic valueForKey:@"stream"])};
  447. YBWeakSelf;
  448. [YBNetworking postWithUrl:@"Livepk.CheckLive" Dic:postDic Suc:^(int code, id info, NSString *msg) {
  449. _linkMicAskLast = 10;
  450. [weakSelf destroyLinkMicAskTimer];
  451. if (code == 0) {
  452. _hostLinking = YES;
  453. NSString *newPull = minstr([[info firstObject] valueForKey:@"pull"]);
  454. [weakSelf liveSendAnchorCtrOfAcntion:@"2" andExtDic:@{@"pull":newPull,@"uid":minstr([checkDic valueForKey:@"uid"])}];
  455. }else{
  456. _hostLinking = NO;
  457. }
  458. } Fail:^(id fail) {
  459. _linkMicAskLast = 10;
  460. [weakSelf destroyLinkMicAskTimer];
  461. _hostLinking = NO;
  462. }];
  463. }
  464. -(void)cancelLinkAlertShow {
  465. _linkMicAskLast = 10;
  466. if (_linkAlertView) {
  467. [_linkAlertView removeFromSuperview];
  468. _linkAlertView = nil;
  469. }
  470. }
  471. -(void)linkMicAskEvent {
  472. _linkMicAskLast -= 1;
  473. _linkAlertView.timeL.text = [NSString stringWithFormat:@"%@(%ds)...",YZMsg(@"发起连麦请求"),_linkMicAskLast];
  474. if (_linkMicAskLast <= 0) {
  475. if (_linkAlertView.isHostToHost) {
  476. [self liveSendAnchorCtrOfAcntion:@"8" andExtDic:@{@"uid":_linkRequestUid}];
  477. }else {
  478. [self liveSendHostTimeOut:_linkRequestUid];
  479. }
  480. [self destroyLinkMicAskTimer];
  481. [self cancelLinkAlertShow];
  482. }
  483. }
  484. -(void)destroyLinkMicAskTimer {
  485. if (_linkMicAskTimer) {
  486. [_linkMicAskTimer invalidate];
  487. _linkMicAskTimer = nil;
  488. }
  489. }
  490. #pragma mark - socket 断开
  491. -(void)liveDisconnectSocket {
  492. [_socketClient disconnect];
  493. [_socketClient off:@""];
  494. [_socketClient leaveNamespace];
  495. _socketClient = nil;
  496. _socketManager = nil;
  497. _hostLinking = NO;
  498. [self cancelLinkAlertShow];
  499. [self cancelPKAlert];
  500. [self destroyLinkMicAskTimer];
  501. }
  502. #pragma mark - /** 发送公屏聊天 */
  503. -(void)liveSendMsg:(NSString *)eventStr {
  504. NSArray *msgData =@[
  505. @{
  506. @"msg": @[
  507. @{
  508. @"_method_": Soc_SendMsg,
  509. @"action": @"0",
  510. @"msgtype": @"2",
  511. @"isAnchor":@"1",
  512. @"uname": [Config getOwnNicename],
  513. @"uid": [Config getOwnID],
  514. @"ct":eventStr,
  515. @"usertype":@"50",
  516. }
  517. ],
  518. @"retcode": @"000000",
  519. @"retmsg": @"OK"
  520. }
  521. ];
  522. [_socketClient emit:@"broadcast" with:msgData];
  523. }
  524. #pragma mark - /** 主播、管理踢人 */
  525. -(void)liveSendKickUser:(NSDictionary *)kickDic {
  526. NSString *ct = [NSString stringWithFormat:@"%@ %@",[kickDic valueForKey:@"touname"],@"被踢出房间"];
  527. NSString *ct_en = [NSString stringWithFormat:@"%@ %@",[kickDic valueForKey:@"touname"],@"kicked out of room"];
  528. NSArray *msgData = @[
  529. @{
  530. @"msg":
  531. @[@{
  532. @"_method_":Soc_KickUser,
  533. @"action":@"2",
  534. @"ct":ct,
  535. @"ct_en":ct_en,
  536. @"uid":[Config getOwnID],
  537. @"uname":[Config getOwnNicename],
  538. @"touid":minstr([kickDic valueForKey:@"touid"]),
  539. @"touname":minstr([kickDic valueForKey:@"touname"]),
  540. @"msgtype":@"4",
  541. }],
  542. @"retcode":@"000000",
  543. @"retmsg":@"OK"}];
  544. [MBProgressHUD showError:YZMsg(@"踢人成功")];
  545. [_socketClient emit:@"broadcast" with:msgData];
  546. }
  547. #pragma mark - /** 主播、管理员禁言 */
  548. -(void)liveSendShutUpUser:(NSDictionary *)shutUpDic {
  549. NSString *type = minstr([shutUpDic valueForKey:@"type"]);
  550. NSString *touname = minstr([shutUpDic valueForKey:@"touname"]);
  551. NSString *msg = [NSString stringWithFormat:@"%@ %@",touname,@"被本场禁言"];
  552. NSString *msg_en = [NSString stringWithFormat:@"%@ %@",touname,@"has been shup up in this live stream"];
  553. if ([type isEqual:@"0"]) {
  554. msg = [NSString stringWithFormat:@"%@ %@",touname,@"被永久禁言"];
  555. msg_en = [NSString stringWithFormat:@"%@ %@",touname,@"shut up permanently"];
  556. }
  557. NSArray *msgData = @[
  558. @{
  559. @"msg":
  560. @[@{
  561. @"_method_":Soc_ShutUpUser,
  562. @"action":@"1",
  563. @"ct":msg,
  564. @"ct_en":msg_en,
  565. @"uid":[Config getOwnID],
  566. @"uname":[Config getOwnNicename],
  567. @"touid":minstr([shutUpDic valueForKey:@"touid"]),
  568. @"touname":touname,
  569. @"msgtype":@"4",
  570. }],
  571. @"retcode":@"000000",
  572. @"retmsg":@"OK"}];
  573. [MBProgressHUD showError:YZMsg(@"禁言成功")];
  574. [_socketClient emit:@"broadcast" with:msgData];
  575. }
  576. #pragma mark - /** 来电话 */
  577. -(void)liveSendPhoneCall:(NSString *)eventStr andEn:(NSString *)eventStr_en{
  578. NSArray *msgData = @[
  579. @{
  580. @"msg":
  581. @[@{
  582. @"_method_":Soc_SystemNot,
  583. @"action":@"13",
  584. @"ct":eventStr,
  585. @"ct_en":eventStr_en,
  586. @"uid":@"",
  587. @"uname":YZMsg(@"直播间消息"),
  588. @"msgtype":@"4",
  589. }],
  590. @"retcode":@"000000",
  591. @"retmsg":@"OK"}];
  592. [_socketClient emit:@"broadcast" with:msgData];
  593. }
  594. #pragma mark - /** 主播设置、取消管理 */
  595. -(void)liveSendSetAdmin:(NSDictionary *)adminDic {
  596. NSString *isAdmin = minstr([adminDic valueForKey:@"isadmin"]);
  597. NSString *touid = minstr([adminDic valueForKey:@"touid"]);
  598. NSString *touname = minstr([adminDic valueForKey:@"touname"]);
  599. NSString *ctMsg;
  600. NSString *ctMsg_en;
  601. if ([isAdmin isEqual:@"0"]) {
  602. ctMsg = @"被取消管理";
  603. ctMsg_en = @"Admin cancelled";
  604. [MBProgressHUD showPop:YZMsg(@"取消管理员成功")];
  605. }else {
  606. ctMsg = @"被设为管理";
  607. ctMsg_en = @"set as Admin";
  608. [MBProgressHUD showPop:YZMsg(@"设置管理员成功")];
  609. }
  610. NSArray *msgData = @[
  611. @{
  612. @"msg":
  613. @[@{
  614. @"_method_":Soc_setAdmin,
  615. @"action":isAdmin,
  616. @"ct":[NSString stringWithFormat:@"%@ %@",touname,ctMsg],
  617. @"ct_en":[NSString stringWithFormat:@"%@ %@",touname,ctMsg_en],
  618. @"uid":[Config getOwnID],
  619. @"uname":YZMsg(@"直播间消息"),
  620. @"touid":touid,
  621. @"touname":touname,
  622. @"msgtype":@"1",
  623. }],
  624. @"retcode":@"000000",
  625. @"retmsg":@"OK"}];
  626. [_socketClient emit:@"broadcast" with:msgData];
  627. }
  628. /** 在售商品-展示、隐藏 */
  629. -(void)liveSendOnSaleShowHidden:(NSDictionary *)goodsDic;{
  630. //action 1-展示 0-隐藏
  631. NSArray *msgData = @[
  632. @{
  633. @"msg":
  634. @[@{
  635. @"_method_":Soc_goodsLiveShow,
  636. @"action":minstr([goodsDic valueForKey:@"action"]),
  637. @"msgtype":@"0",
  638. @"goods_name":minstr([goodsDic valueForKey:@"name"]),
  639. @"goods_old_price":minstr([goodsDic valueForKey:@"old_price"]),
  640. @"goods_price":minstr([goodsDic valueForKey:@"price"]),
  641. @"goods_thumb":minstr([goodsDic valueForKey:@"thumb"]),
  642. @"goods_type":minstr([goodsDic valueForKey:@"type"]),
  643. @"goodsid":minstr([goodsDic valueForKey:@"id"]),
  644. }],
  645. @"retcode":@"000000",
  646. @"retmsg":@"OK"}];
  647. [_socketClient emit:@"broadcast" with:msgData];
  648. }
  649. #pragma mark - /** 用户-主播连麦 */
  650. /*
  651. 1 有人发送连麦请求
  652. 2 主播接受连麦
  653. 3 主播拒绝连麦
  654. 4 用户推流,发送自己的播流地址
  655. 5 用户断开连麦
  656. 6 主播断开连麦
  657. 7 主播正忙碌
  658. 8 主播无响应
  659. */
  660. -(void)liveSendHostIsAgree:(BOOL)isAgree andTouid:(NSString *)touid {
  661. NSArray *msgData = @[
  662. @{
  663. @"msg":
  664. @[@{
  665. @"_method_":Soc_ConnectVideo,
  666. @"action": isAgree?@"2":@"3",
  667. @"msgtype": @"10",
  668. @"touid":touid,
  669. }],
  670. @"retcode":@"000000",
  671. @"retmsg":@"OK"}];
  672. [_socketClient emit:@"broadcast" with:msgData];
  673. }
  674. -(void)liveSendHostDisconnect:(NSDictionary *)userInfo{
  675. _hostLinking = NO;
  676. NSArray *msgData = @[
  677. @{
  678. @"msg":
  679. @[@{
  680. @"_method_":Soc_ConnectVideo,
  681. @"action":@"6",
  682. @"msgtype": @"10",
  683. @"touid":minstr([userInfo valueForKey:@"uid"]),
  684. @"uname":minstr([userInfo valueForKey:@"uname"]),
  685. }],
  686. @"retcode":@"000000",
  687. @"retmsg":@"OK"}];
  688. [_socketClient emit:@"broadcast" with:msgData];
  689. }
  690. -(void)liveSendHostBusyTouser:(NSString *)touid; {
  691. NSArray *msgData = @[
  692. @{
  693. @"msg":
  694. @[@{
  695. @"_method_":Soc_ConnectVideo,
  696. @"action": @"7",
  697. @"msgtype": @"10",
  698. @"touid":touid
  699. }],
  700. @"retcode":@"000000",
  701. @"retmsg":@"OK"}];
  702. [_socketClient emit:@"broadcast" with:msgData];
  703. }
  704. -(void)liveSendHostTimeOut:(NSString *)touid {
  705. NSArray *msgData = @[
  706. @{
  707. @"msg":
  708. @[@{
  709. @"_method_":Soc_ConnectVideo,
  710. @"action": @"8",
  711. @"msgtype": @"10",
  712. @"touid":touid
  713. }],
  714. @"retcode":@"000000",
  715. @"retmsg":@"OK"}];
  716. [_socketClient emit:@"broadcast" with:msgData];
  717. }
  718. #pragma mark - /** 主播-主播 连麦 */
  719. //1:发起连麦;2;接受连麦;3:拒绝连麦;4:连麦成功通知;5.手动断开连麦;7:对方正忙碌 8:对方无响应
  720. ///主播-主播 发起
  721. -(void)liveSendAnchorStartLink:(NSDictionary *)otherInfo andMyInfo:(NSDictionary *)myInfo {
  722. _linkRequestUid = minstr([otherInfo valueForKey:@"uid"]);
  723. NSArray *msgData = @[
  724. @{
  725. @"msg":
  726. @[@{
  727. @"_method_":Soc_LiveConnect,
  728. @"action":@"1",
  729. @"msgtype": @"0",
  730. @"uid":[Config getOwnID],
  731. @"uname":[Config getOwnNicename],
  732. @"uhead":[Config getUserAvatar],
  733. @"sex":[Config getUserSex],
  734. @"pkuid":minstr([otherInfo valueForKey:@"uid"]),
  735. @"pkpull":minstr([myInfo valueForKey:@"pull"]),
  736. @"stream":minstr([_roomDic valueForKey:@"stream"]),
  737. }],
  738. @"retcode":@"000000",
  739. @"retmsg":@"OK"}];
  740. [_socketClient emit:@"broadcast" with:msgData];
  741. }
  742. -(void)liveSendAnchorCtrOfAcntion:(NSString *)action andExtDic:(NSDictionary *)extDic {
  743. NSArray *msgData = @[
  744. @{
  745. @"msg":
  746. @[@{
  747. @"_method_":Soc_LiveConnect,
  748. @"action":action,
  749. @"msgtype": @"0",
  750. @"uid":[Config getOwnID],
  751. @"uname":[Config getOwnNicename],
  752. @"uhead":[Config getUserAvatar],
  753. @"pkuid":minstr([extDic valueForKey:@"uid"]),
  754. @"pkpull":[PublicObj checkNull:minstr([extDic valueForKey:@"pull"])]?@"":minstr([extDic valueForKey:@"pull"]),
  755. }],
  756. @"retcode":@"000000",
  757. @"retmsg":@"OK"}];
  758. [_socketClient emit:@"broadcast" with:msgData];
  759. }
  760. #pragma mark - /** PK */
  761. //1:发起PK;2;接受PK;3:拒绝PK;4:PK成功通知;5.;7:对方正忙碌 8:对方无响应 9:PK结果
  762. -(void)liveSendStartPk {
  763. NSArray *msgData = @[
  764. @{
  765. @"msg":
  766. @[@{
  767. @"_method_":Soc_LivePK,
  768. @"action":@"1",
  769. @"msgtype": @"0",
  770. @"uid":[Config getOwnID],
  771. @"uname":[Config getOwnNicename],
  772. @"uhead":[Config getUserAvatar],
  773. @"sex":[Config getUserSex],
  774. @"pkuid":_linkRequestUid,
  775. @"stream":minstr([_roomDic valueForKey:@"stream"]),
  776. }],
  777. @"retcode":@"000000",
  778. @"retmsg":@"OK"}];
  779. [_socketClient emit:@"broadcast" with:msgData];
  780. }
  781. -(void)liveSendPkCtrAction:(NSString *)action {
  782. NSArray *msgData = @[
  783. @{
  784. @"msg":
  785. @[@{
  786. @"_method_":Soc_LivePK,
  787. @"action":action,
  788. @"msgtype": @"0",
  789. @"uid":[Config getOwnID],
  790. @"uname":[Config getOwnNicename],
  791. @"uhead":[Config getUserAvatar],
  792. @"sex":[Config getUserSex],
  793. @"pkuid":_linkRequestUid,
  794. }],
  795. @"retcode":@"000000",
  796. @"retmsg":@"OK"}];
  797. [_socketClient emit:@"broadcast" with:msgData];
  798. NSLog(@"====PK发送同意拒绝:===%@",msgData);
  799. }
  800. @end