Skip to content

Commit be858b1

Browse files
committed
Merge branch 'multiplex' of https://github.com/socketio/socket.io-client-cpp into multiplex
2 parents ff7910f + 2d6427c commit be858b1

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

examples/QT/SioChatDemo/mainwindow.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ MainWindow::MainWindow(QWidget *parent) :
2424
ui->setupUi(this);
2525
connect(this,SIGNAL(RequestAddListItem(QListWidgetItem*)),this,SLOT(AddListItem(QListWidgetItem*)));
2626
connect(this,SIGNAL(RequestToggleInputs(bool)),this,SLOT(ToggleInputs(bool)));
27+
_io->set_socket_open_listener(std::bind(&MainWindow::OnConnected,this,std::placeholders::_1));
28+
_io->set_socket_close_listener(std::bind(&MainWindow::OnFailed,this));
2729
}
2830

2931
MainWindow::~MainWindow()
3032
{
3133
_io->socket()->off_all();
32-
_io->socket()->clear_listeners();
34+
_io->socket()->off_error();
3335
delete ui;
3436
}
3537

@@ -107,9 +109,6 @@ void MainWindow::NicknameAccept()
107109
BIND_EVENT(sock,"typing",std::bind(&MainWindow::OnTyping,this,_1,_2,_3,_4));
108110
BIND_EVENT(sock,"stop typing",std::bind(&MainWindow::OnStopTyping,this,_1,_2,_3,_4));
109111
BIND_EVENT(sock,"login",std::bind(&MainWindow::OnLogin,this,_1,_2,_3,_4));
110-
sock->set_connect_listener(std::bind(&MainWindow::OnConnected,this));
111-
112-
sock->set_close_listener(std::bind(&MainWindow::OnFailed,this));
113112
_io->connect("ws://localhost:3000");
114113
}
115114
}
@@ -230,7 +229,7 @@ void MainWindow::OnLogin(std::string const& name,message::ptr const& data,bool h
230229
Q_EMIT RequestAddListItem(item);
231230
}
232231

233-
void MainWindow::OnConnected()
232+
void MainWindow::OnConnected(std::string const& nsp)
234233
{
235234
QByteArray bytes = m_name.toUtf8();
236235
std::string nickName(bytes.data(),bytes.length());

examples/QT/SioChatDemo/mainwindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private Q_SLOTS:
4545
void OnTyping(std::string const& name,message::ptr const& data,bool hasAck,message::ptr &ack_resp);
4646
void OnStopTyping(std::string const& name,message::ptr const& data,bool hasAck,message::ptr &ack_resp);
4747
void OnLogin(std::string const& name,message::ptr const& data,bool hasAck,message::ptr &ack_resp);
48-
void OnConnected();
48+
void OnConnected(std::string const& nsp);
4949
void OnClosed(client::close_reason const& reason);
5050
void OnFailed();
5151
void ShowLoginDialog();

examples/iOS/SioChatDemo/SioChatDemo/CRViewController.mm

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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));

src/sio_socket.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ void set_##__FIELD__(__TYPE__ const& l) \
217217
m_error_listener = nullptr;
218218
}
219219

220-
socket::impl::impl(client_impl *client,std::string const& nsp):m_client(client),m_nsp(nsp)
220+
socket::impl::impl(client_impl *client,std::string const& nsp):
221+
m_client(client),
222+
m_nsp(nsp),
223+
m_connected(false)
221224
{
222225
NULL_GUARD(client);
223226
if(m_client->opened())

0 commit comments

Comments
 (0)