77#define BIND_EVENT (IO,EV,FN ) \
88 do { \
99 client::event_listener_aux l = FN;\
10- IO->bind_event (EV,l);\
10+ IO->on (EV,l);\
1111 } while (0 )
1212
1313#else
1414#define BIND_EVENT (IO,EV,FN ) \
15- IO->bind_event (EV,FN)
15+ IO->on (EV,FN)
1616#endif
1717
1818MainWindow::MainWindow (QWidget *parent) :
@@ -22,28 +22,14 @@ MainWindow::MainWindow(QWidget *parent) :
2222 m_dialog()
2323{
2424 ui->setupUi (this );
25- using std::placeholders::_1;
26- using std::placeholders::_2;
27- using std::placeholders::_3;
28- using std::placeholders::_4;
29- BIND_EVENT (_io," new message" ,std::bind (&MainWindow::OnNewMessage,this ,_1,_2,_3,_4));
30- BIND_EVENT (_io," user joined" ,std::bind (&MainWindow::OnUserJoined,this ,_1,_2,_3,_4));
31- BIND_EVENT (_io," user left" ,std::bind (&MainWindow::OnUserLeft,this ,_1,_2,_3,_4));
32- BIND_EVENT (_io," typing" ,std::bind (&MainWindow::OnTyping,this ,_1,_2,_3,_4));
33- BIND_EVENT (_io," stop typing" ,std::bind (&MainWindow::OnStopTyping,this ,_1,_2,_3,_4));
34- BIND_EVENT (_io," login" ,std::bind (&MainWindow::OnLogin,this ,_1,_2,_3,_4));
35- _io->set_connect_listener (std::bind (&MainWindow::OnConnected,this ));
36- _io->set_fail_listener (std::bind (&MainWindow::OnFailed,this ));
37- _io->set_close_listener (std::bind (&MainWindow::OnClosed,this ,_1));
38-
3925 connect (this ,SIGNAL (RequestAddListItem (QListWidgetItem*)),this ,SLOT (AddListItem (QListWidgetItem*)));
4026 connect (this ,SIGNAL (RequestToggleInputs (bool )),this ,SLOT (ToggleInputs (bool )));
4127}
4228
4329MainWindow::~MainWindow ()
4430{
45- _io->clear_event_bindings ();
46- _io->clear_con_listeners ();
31+ _io->socket ()-> all_off ();
32+ _io->socket ()-> clear_listeners ();
4733 delete ui;
4834}
4935
@@ -55,7 +41,7 @@ void MainWindow::SendBtnClicked()
5541 {
5642 QByteArray bytes = text.toUtf8 ();
5743 std::string msg (bytes.data (),bytes.length ());
58- _io->emit (" new message" ,msg);
44+ _io->socket ()-> emit (" new message" ,msg);
5945 text.append (" :You" );
6046 QListWidgetItem *item = new QListWidgetItem (text);
6147 item->setTextAlignment (Qt::AlignRight);
@@ -86,7 +72,7 @@ void MainWindow::showEvent(QShowEvent *event)
8672void MainWindow::TypingStop ()
8773{
8874 m_timer.reset ();
89- _io->emit (" stop typing" ," " );
75+ _io->socket ()-> emit (" stop typing" ," " );
9076}
9177
9278void MainWindow::TypingChanged ()
@@ -97,7 +83,7 @@ void MainWindow::TypingChanged()
9783 }
9884 else
9985 {
100- _io->emit (" typing" ," " );
86+ _io->socket ()-> emit (" typing" ," " );
10187 }
10288 m_timer.reset (new QTimer (this ));
10389 connect (m_timer.get (),SIGNAL (timeout ()),this ,SLOT (TypingStop ()));
@@ -110,6 +96,20 @@ void MainWindow::NicknameAccept()
11096 m_name = m_dialog->getNickname ();
11197 if (m_name.length ()>0 )
11298 {
99+ using std::placeholders::_1;
100+ using std::placeholders::_2;
101+ using std::placeholders::_3;
102+ using std::placeholders::_4;
103+ socket::ptr sock = _io->socket ();
104+ BIND_EVENT (sock," new message" ,std::bind (&MainWindow::OnNewMessage,this ,_1,_2,_3,_4));
105+ BIND_EVENT (sock," user joined" ,std::bind (&MainWindow::OnUserJoined,this ,_1,_2,_3,_4));
106+ BIND_EVENT (sock," user left" ,std::bind (&MainWindow::OnUserLeft,this ,_1,_2,_3,_4));
107+ BIND_EVENT (sock," typing" ,std::bind (&MainWindow::OnTyping,this ,_1,_2,_3,_4));
108+ BIND_EVENT (sock," stop typing" ,std::bind (&MainWindow::OnStopTyping,this ,_1,_2,_3,_4));
109+ 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 ));
113113 _io->connect (" ws://localhost:3000" );
114114 }
115115}
@@ -234,7 +234,7 @@ void MainWindow::OnConnected()
234234{
235235 QByteArray bytes = m_name.toUtf8 ();
236236 std::string nickName (bytes.data (),bytes.length ());
237- _io->emit (" add user" , nickName);
237+ _io->socket ()-> emit (" add user" , nickName);
238238}
239239
240240void MainWindow::OnClosed (client::close_reason const & reason)
0 commit comments