我有多个 View 需要处理 socket.io 的网络连接,所以我创建了单例类,即 MC_SocketHandler。下面是 MC_SocketHandler 类的代码。
// MC_SocketHandler.h
#import <Foundation/Foundation.h>
#import "SocketIO.h"
@interface MC_SocketHandler : NSObject <SocketIODelegate>
// SocketIO
//@property (nonatomic) SocketIO *socketConnection;
+ (MC_SocketHandler *) sharedSocketHanderObj;
+ (SocketIO *) initHandShake;
+ (SocketIO *) getSocketConnection;
-(bool) isConnected;
-(void) disConnect;
-(void) fireAgentLeftChat;
@end
// MC_SocketHandler.m
#import "MC_SocketHandler.h"
#import "MC_APIUtility.h"
@implementation MC_SocketHandler
SocketIO *socketConnection = nil;
static MC_SocketHandler *sharedSocketObj = nil;
+ (MC_SocketHandler *) sharedSocketHanderObj {
if (sharedSocketObj == nil)
sharedSocketObj = [[MC_SocketHandler alloc] init];
return sharedSocketObj;
}
+(SocketIO*) initHandShake {
if (socketConnection == nil) {
NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:[MC_APIUtility getApiToken], @"token", nil];
socketConnection = [[SocketIO alloc] initWithDelegate:(id)self ];
[socketConnection connectToHost:domain onPort:447 withParams:headers];
}
return socketConnection;
}
+ (SocketIO *) getSocketConnection {
return socketConnection;
}
-(bool) isConnected {
if (socketConnection == nil)
return socketConnection.isConnected;
return false;
}
-(void) disConnect {
if (socketConnection != nil && socketConnection.isConnected)
[socketConnection disconnect];
NSLog(@"Disconnected --- %hhd", socketConnection.isConnected );
return;
}
// SocketIO Delegate
-(void) socketIODidConnect:(SocketIO *)socket {
NSLog(@"Socket has Connected....");
}
-(void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet {
NSString *data = packet.data;
NSLog(@"---- didReceoveEvent - data - %@", data);
// Grab data from packet
NSDictionary *dict = packet.dataAsJSON;
NSLog(@"EVENT DATA :- %@ DICT :- %@", data, dict);
/*
EVENTS To Listen
onSuccessInit
visitor_info
new_visitor
agent_online
agent_offline
agent_logout
*/
dict = nil;
// Pull out args fro mdict
//NSArray *args = dict[@"args"];
}
-(void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet {
NSLog(@"Rcvd Message - %@", packet.data);
}
-(void) socketIO:(SocketIO *)socket didSendMessage:(SocketIOPacket *)packet {
NSLog(@"Send Msg - %@", packet.dataAsJSON);
}
-(void) socketIO:(SocketIO *)socket onError:(NSError *)error {
NSLog(@"Error - %@", error);
}
-(void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error {
NSLog(@"Disconnected With Error - %@", error);
}
-(void) fireAgentLeftChat {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[MainAppDataObject sharedAppDataObject].activeAgentChatItem.chatSessionId forKey:@"chat_session_id"];
[socketConnection sendEvent:@"agentLeftChat" withData:dict];
return;
}
- (void)dealloc {
socketConnection = nil;
}
@end
我在我的观点之一中使用它的代码:
// Init SocketIO
SocketIO *socket = [MC_SocketHandler initHandShake];
// Fire Agent Online event
[socket sendEvent:@"setAgentOnline" withData:nil];
握手正确完成,setAgentOnline 事件正确发送。我触发的其他事件也正确完成。但, 当套接字通过 initHandshake 连接时,我相信“套接字已连接......”应该在日志中看到,因为它是用 socketIODidConnect 委托(delegate)方法编写的。同样,我收到事件(我看到 socket.m 类的日志),但从未调用我的委托(delegate)方法 didReceiveEvent。同样,我没有看到任何委托(delegate)方法的日志。 在 initHandShake 方法中,我还设置了委托(delegate): socketConnection = [[SocketIO alloc] initWithDelegate:(id)self ]; 但是为什么不调用这些方法。
我还想知道,当我收到事件时,我必须针对不同的事件执行不同的操作。我将如何转移到特定 View ( View 的 obj 不会与此共享以调用他的方法)?如果我创建委托(delegate),那么我将不得不处理所有 View 中的所有委托(delegate)方法。解决这个问题的最佳方法是什么?以及为什么当我设置委托(delegate)时没有链接和调用此单例和委托(delegate)方法。我哪里出错了?
非常感谢任何帮助和指导。非常感谢。
最佳答案
在 SocketIO 中,您创建一个 SocketIO
是这样吗?
其实叫做“socketConnection”。我说得对吗?
在那个时候...
你必须设置委托(delegate)!!!
基本上,您的代码必须如下所示,
socketConnection = make one of these.
socketConnection.delegate = self;
这可能是您的根本问题。希望对您有所帮助!
PS,几乎可以肯定,您应该在 iOS 开发中仅使用属性。摆脱您的“传统”变量并仅使用属性。
关于iOS Singleton、Delegate 和 SocketIO 之间出了点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24010653/