试图让socket.IO-objc使用socket.io的聊天示例php

时间:2016-12-02 08:25:53

标签: php ios objective-c socket.io chat

我正在使用socket.io的聊天示例基于php(https://github.com/walkor/phpsocket.io),我可以接收消息。但是,我无法发送消息。在尝试设置昵称时,我在节点实例上崩溃了。 在iOS端,我在viewDidLoad方法之后在socketDidConnect方法中发送消息...

(void)viewDidLoad  {
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.

      self.socketIO = [[SocketIO alloc] initWithDelegate:self];
      self.socketIO.useSecure = NO;

      [self.socketIO connectToHost:@"localhost" onPort:3000];
    }

(void) socketIODidConnect:(SocketIO *)socket{
    NSLog(@"We connected....");
    [self.socketIO sendEvent:@"add user" withData:@"testUser"];
}

在服务器端功能添加用户:

  // when the client emits 'add user', this listens and executes
    $socket->on('add user', function ($username) use($socket){
        global $usernames, $numUsers;
        // we store the username in the socket session for this client
        $socket->username = $username;
        // add the client's username to the global list
        $usernames[$username] = $username;
        ++$numUsers;
        $socket->addedUser = true;
        $socket->emit('login', array( 
            'numUsers' => $numUsers
        ));
        // echo globally (all clients) that a person has connected
        $socket->broadcast->emit('user joined', array(
            'username' => $socket->username,
            'numUsers' => $numUsers
        ));
    });

我如何设置用户名和发送消息?

0 个答案:

没有答案