IOS SocketIO聊天应用程序问题,Node JS作为后端

时间:2014-08-16 06:53:41

标签: ios iphone sockets socket.io chat

我正在开发一个聊天应用程序,其中有后端功能,如

 client.on('mjmChatAddUser', function(user, room)
    {
        // Guest
        if(user == null)
            user = mjmChatCreateGuestName();

        // Convert special characters to HTML entities
        user = htmlEntities(user);
        room = htmlEntities(room);

        client.username = user;
        client.room = room;
        mjmChatUsernames.push(user);
        client.join(room);

        client.emit('mjmChatStatusUser', 'you have joined to '+ room +' room');

        client.broadcast.to(room).emit('mjmChatStatusUser', user +' has joined to this room');
        client.emit('mjmChatRooms', room);

        io.sockets.to(room).emit('mjmChatUsers', mjmChatGetUsersRoom(room));
    });

现在从ios方面来说,我想用ScoketIO调用这个函数,我已经连接到服务器但是没有启用调用这个函数,这就是我正在做的,是正确的方法,请指导我

- (void) viewDidLoad
{
    [super viewDidLoad];

    // create socket.io client instance
    socketIO = [[SocketIO alloc] initWithDelegate:self];

    // you can update the resource name of the handshake URL
    // see https://github.com/pkyeck/socket.IO-objc/pull/80
    // [socketIO setResourceName:@"whatever"];

    // if you want to use https instead of http
    // socketIO.useSecure = YES;

    // pass cookie(s) to handshake endpoint (e.g. for auth)
    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"localhost", NSHTTPCookieDomain,
                                    @"/", NSHTTPCookiePath,
                                    @"auth", NSHTTPCookieName,
                                    @"56cdea636acdf132", NSHTTPCookieValue,
                                    nil];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
    NSArray *cookies = [NSArray arrayWithObjects:cookie, nil];

    socketIO.cookies = cookies;

    // connect to the socket.io server that is running locally at port 3000
    [socketIO connectToHost:@"MY SERVER" onPort:3000];

}

# pragma mark -
# pragma mark socket.IO-objc delegate methods

- (void) socketIODidConnect:(SocketIO *)socket
{
    NSLog(@"socket.io connected.");
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
    [dict2 setObject:@"demo1" forKey:@"user"];
    [dict2 setObject:@"php" forKey:@"room"];
    [socketIO sendEvent:@"mjmChatAddUser" withData:@{@"Abc":@"php"}];


}

- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet
{
    NSLog(@"didReceiveEvent()");

    // test acknowledge
    SocketIOCallback cb = ^(id argsData) {
        NSDictionary *response = argsData;
        // do something with response
        NSLog(@"ack arrived: %@", response);

        // test forced disconnect
        [socketIO disconnectForced];
    };
    [socketIO sendMessage:@"hello back!" withAcknowledge:cb];
}

- (void) socketIO:(SocketIO *)socket onError:(NSError *)error
{
    if ([error code] == SocketIOUnauthorized) {
        NSLog(@"not authorized");
    } else {
        NSLog(@"onError() %@", error);
    }
}


- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error
{
    NSLog(@"socket.io disconnected. did error occur? %@", error);
}

0 个答案:

没有答案