我有多个视图,我需要处理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事件正确发送。我开火的其他事件也正确完成。但, 当socket通过initHandshake连接时,我相信“Socket has Connected ....”应该在日志中看到,因为它是用socketIODidConnect委托方法编写的。同样,我收到事件(我看到socket.m类的日志),但我的委托方法didReceiveEvent从未被调用。同样地,我没有看到任何委托方法的任何日志。 在initHandShake方法中,我也设置了委托: socketConnection = [[SocketIO alloc] initWithDelegate:(id)self]; 但为什么不调用这些方法。
我也想知道,当我收到活动时,我会在不同的事件中执行不同的操作。我将如何转移到特定视图(View的obj不会与此共享以调用他的方法)?如果我创建委托,那么我将必须处理所有视图中的所有委托方法。解决这个问题的最佳方法是什么?为什么这个Singleton&amp;委托方法没有被链接&amp;在设置委托时调用。我哪里错了?
任何帮助,指导都非常感激。非常感谢。
答案 0 :(得分:2)
在SocketIO中,您创建一个SocketIO
是吗?
实际上称为“socketConnection”。我是对的吗?
你必须设置代表!!!
基本上,您的代码必须如此,
socketConnection = make one of these.
socketConnection.delegate = self;
这可能是你的根本问题。我希望它有所帮助!
PS你几乎肯定应该在iOS开发中仅使用属性。摆脱“传统”变量,只使用属性。