@@ -31,6 +31,7 @@ @interface CRViewController ()<UITableViewDataSource,UITableViewDelegate,NSURLCo
3131 NSMutableSet *_typingUsers;
3232 NSString * _name;
3333 NSInteger _userCount;
34+ NSTimer * _inputTimer;
3435}
3536@property (weak , nonatomic ) IBOutlet UILabel *infoLabel;
3637@property (weak , nonatomic ) IBOutlet UILabel *typingLabel;
@@ -139,7 +140,7 @@ void OnLogin(CFTypeRef ctrl, string const& name, sio::message::ptr const& data,
139140 }
140141}
141142
142- void OnConnected (CFTypeRef ctrl)
143+ void OnConnected (CFTypeRef ctrl,std::string nsp )
143144{
144145 dispatch_async (dispatch_get_main_queue (), ^{
145146 [((__bridge CRViewController*)ctrl) onConnected ];
@@ -181,6 +182,7 @@ - (void)viewDidLoad
181182
182183-(void )viewWillAppear : (BOOL )animated
183184{
185+ _io->set_socket_open_listener (std::bind (&OnConnected, (__bridge CFTypeRef)self,std::placeholders::_1));
184186 _io->set_close_listener (std::bind (&OnClose, (__bridge CFTypeRef)self, std::placeholders::_1));
185187 _io->set_fail_listener (std::bind (&OnFailed, (__bridge CFTypeRef)self));
186188}
@@ -204,8 +206,8 @@ -(void)keyboardWillHide {
204206-(void )viewDidDisappear : (BOOL )animated
205207{
206208 _io->socket ()->off_all ();
207- _io->socket ()-> set_connect_listener (nullptr );
208- _io->socket ()-> set_close_listener (nullptr );
209+ _io->set_open_listener (nullptr );
210+ _io->set_close_listener (nullptr );
209211 _io->close ();
210212}
211213
@@ -336,6 +338,29 @@ -(void) updateUser:(NSString*)user count:(NSInteger) num joinOrLeft:(BOOL) isJoi
336338 }
337339}
338340
341+ -(void ) inputTimeout
342+ {
343+ _inputTimer = nil ;
344+ _io->socket ()->emit (" stop typing" , " " );
345+ }
346+
347+ -(BOOL )textField : (UITextField *)textField shouldChangeCharactersInRange : (NSRange )range replacementString : (NSString *)string
348+ {
349+ if (textField == self.messageField )
350+ {
351+ if (_inputTimer.valid )
352+ {
353+ [_inputTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: 1.0 ]];
354+ }
355+ else
356+ {
357+ _io->socket ()->emit (" typing" , " " );
358+ _inputTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector (inputTimeout ) userInfo: nil repeats: NO ];
359+ }
360+ }
361+ return YES ;
362+ }
363+
339364- (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section
340365{
341366 return [_receivedMessage count ];
@@ -375,22 +400,6 @@ -(void)scrollViewDidScroll:(UIScrollView *)scrollView
375400 }
376401}
377402
378- -(void )textFieldDidBeginEditing : (UITextField *)textField
379- {
380- if (textField == self.messageField )
381- {
382- _io->socket ()->emit (" typing" , " " );
383- }
384- }
385-
386- -(void )textFieldDidEndEditing : (UITextField *)textField
387- {
388- if (textField == self.messageField )
389- {
390- _io->socket ()->emit (" stop typing" , " " );
391- }
392- }
393-
394403-(BOOL )textFieldShouldReturn : (UITextField *)textField
395404{
396405 if (textField == self.nickName ) {
@@ -401,7 +410,6 @@ -(BOOL)textFieldShouldReturn:(UITextField *)textField
401410 using std::placeholders::_3;
402411 using std::placeholders::_4;
403412 socket::ptr socket = _io->socket ();
404- socket->set_connect_listener (std::bind (&OnConnected, (__bridge CFTypeRef)self));
405413
406414 socket->on (" new message" , std::bind (&OnNewMessage, (__bridge CFTypeRef)self, _1,_2,_3,_4));
407415 socket->on (" typing" , std::bind (&OnTyping, (__bridge CFTypeRef)self, _1,_2,_3,_4));
0 commit comments