现在我正在处理objective -c上的socket.io,并且正在发送事件请求。我正在使用 pkyeck socket.io api 并发送一个事件,他们以他设置的方式将 args 值放入字典中,在 sentevent 函数中处理该字典并获取您发送的变量字典并将其与args 键。因此,如果我遵循该模式,我发送到 socket.io 服务器的事件将如下所示。
{"args":[{"username":"yeah", "id":"2"}],"name":"login"}
但是,为了让我的服务器正确处理 socket.io 调用,调用需要如下所示。
{"args":["yeah","2"],"name":"login"}
但是我无法以这样的方式发送变量,以便字典进行这样的设置。如果我使用 NSArray,则请求将按原样发送。
{"args":[["yeah","2"]}],"name":"login"}
如果我发送一个附加的字符串,它看起来像这样。
{"args":[{"'yeah','2'"}],"name":"login"}
这是我到目前为止所做的
- (void) socketIO:(SocketIO *)socket onMessage:(NSString *)status{
NSLog(@"Current status %@", status);
if([status isEqualToString:@"ready"]){
SocketIOCallback cb = ^(id argsData) {
NSDictionary *response = argsData;
// do something with response
NSLog(@"ack arrived: %@", response);
// test forced disconnect
//[socketIO disconnectForced];
};
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Yeah" forKey:@"username"];
[dict setObject:@"eqe74squbaah9eqvk7bok3rh09" forKey:@"session"];
[dict setObject:userid forKey:@"user_id"];
NSMutableArray *arr=[NSArray arrayWithObjects:@"Yeah", @"eqe74squbaah9eqvk7bok3rh09", userid, nil];
NSString *eventName=@"'Yeah',";
eventName=[eventName stringByAppendingString:@"'eqe74squbaah9eqvk7bok3rh09',"];
eventName=[eventName stringByAppendingString:@"'"];
eventName=[eventName stringByAppendingString:userid];
eventName=[eventName stringByAppendingString:@"'"];
// NSLog(@"This is the string to be sent %@",eventName);
[socketIO sendEvent:@"login" withData:eventName andAcknowledge:cb];
[socketIO sendEvent:@"subscribe" withData:@"47058" andAcknowledge:cb];
}
这是发送 socket.io 事件请求的方法。
- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:eventName forKey:@"name"];
// do not require arguments
if (data != nil) {
[dict setObject:[NSArray arrayWithObject:data] forKey:@"args"];
}
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"event"];
packet.data = [SocketIOJSONSerialization JSONStringFromObject:dict error:nil];
packet.pId = [self addAcknowledge:function];
if (function) {
packet.ack = @"data";
}
[self send:packet];
}