Skip to content

Commit 079950e

Browse files
committed
fix emit bug
1 parent bd89d13 commit 079950e

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

examples/QT/SioChatDemo/mainwindow.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void MainWindow::showEvent(QShowEvent *event)
9191
void MainWindow::TypingStop()
9292
{
9393
m_timer.reset();
94-
_io->socket()->emit("stop typing","");
94+
_io->socket()->emit("stop typing");
9595
}
9696

9797
void MainWindow::TypingChanged()
@@ -102,7 +102,7 @@ void MainWindow::TypingChanged()
102102
}
103103
else
104104
{
105-
_io->socket()->emit("typing",nullptr);
105+
_io->socket()->emit("typing");
106106
}
107107
m_timer.reset(new QTimer(this));
108108
connect(m_timer.get(),SIGNAL(timeout()),this,SLOT(TypingStop()));
@@ -234,11 +234,6 @@ void MainWindow::OnStopTyping(std::string const& name,message::ptr const& data,b
234234

235235
void MainWindow::OnLogin(std::string const& name,message::ptr const& data,bool hasAck,message::ptr &ack_resp)
236236
{
237-
message::ptr array = array_message::create();
238-
array->get_vector().push_back(string_message::create("arg1"));
239-
array->get_vector().push_back(string_message::create("arg2"));
240-
_io->socket()->emit("test va",array);
241-
242237
Q_EMIT RequestToggleInputs(true);
243238
int numUser = data->get_map()["numUsers"]->get_int();
244239

src/sio_message.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ namespace sio
293293
message::ptr arr = array_message::create();
294294
arr->get_vector().push_back(string_message::create(event_name));
295295
arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
296+
return arr;
296297
}
297298

298299
private:

src/sio_socket.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ void set_##__FIELD__(__TYPE__ const& l) \
139139

140140
event_listener get_bind_listener_locked(string const& event);
141141

142-
void __ack(int msgId,string const& name,message::ptr const& ack_message);
142+
void ack(int msgId,string const& name,message::ptr const& ack_message);
143143

144-
void __timeout_connection(const boost::system::error_code &ec);
144+
void timeout_connection(const boost::system::error_code &ec);
145145

146-
void __send_connect();
146+
void send_connect();
147147

148-
void __send_packet(packet& p);
148+
void send_packet(packet& p);
149149

150150
static event_listener s_null_event_listener;
151151

@@ -216,7 +216,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
216216
NULL_GUARD(client);
217217
if(m_client->opened())
218218
{
219-
__send_connect();
219+
send_connect();
220220
}
221221
}
222222

@@ -231,24 +231,30 @@ void set_##__FIELD__(__TYPE__ const& l) \
231231
{
232232
NULL_GUARD(m_client);
233233
message::ptr msg_ptr = msglist.to_array_message(name);
234-
packet p(m_nsp, msg_ptr,s_global_event_id);
234+
int pack_id;
235235
if(ack)
236236
{
237+
pack_id = s_global_event_id++;
237238
std::lock_guard<std::mutex> guard(m_event_mutex);
238-
m_acks[s_global_event_id++] = ack;
239+
m_acks[pack_id] = ack;
239240
}
240-
__send_packet(p);
241+
else
242+
{
243+
pack_id = -1;
244+
}
245+
packet p(m_nsp, msg_ptr,pack_id);
246+
send_packet(p);
241247
}
242248

243-
void socket::impl::__send_connect()
249+
void socket::impl::send_connect()
244250
{
245251
NULL_GUARD(m_client);
246252
packet p(packet::type_connect,m_nsp);
247253
m_client->send(p);
248254
m_connection_timer.reset(new boost::asio::deadline_timer(m_client->get_io_service()));
249255
boost::system::error_code ec;
250256
m_connection_timer->expires_from_now(boost::posix_time::milliseconds(20000), ec);
251-
m_connection_timer->async_wait(std::bind(&socket::impl::__timeout_connection,this, std::placeholders::_1));
257+
m_connection_timer->async_wait(std::bind(&socket::impl::timeout_connection,this, std::placeholders::_1));
252258
}
253259

254260
void socket::impl::close()
@@ -257,7 +263,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
257263
if(m_connected)
258264
{
259265
packet p(packet::type_disconnect,m_nsp);
260-
__send_packet(p);
266+
send_packet(p);
261267

262268
if(!m_connection_timer)
263269
{
@@ -306,7 +312,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
306312

307313
void socket::impl::on_open()
308314
{
309-
__send_connect();
315+
send_connect();
310316
}
311317

312318
void socket::impl::on_disconnect()
@@ -408,14 +414,14 @@ void set_##__FIELD__(__TYPE__ const& l) \
408414
if(func)func(ev);
409415
if(needAck)
410416
{
411-
this->__ack(msgId, name, ev.get_ack_message());
417+
this->ack(msgId, name, ev.get_ack_message());
412418
}
413419
}
414420

415-
void socket::impl::__ack(int msgId, const string &name, const message::ptr &ack_message)
421+
void socket::impl::ack(int msgId, const string &name, const message::ptr &ack_message)
416422
{
417423
packet p(m_nsp, make_message(name, ack_message),msgId,true);
418-
__send_packet(p);
424+
send_packet(p);
419425
}
420426

421427
void socket::impl::on_socketio_ack(int msgId, message::ptr const& message)
@@ -438,7 +444,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
438444
if(m_error_listener)m_error_listener(err_message);
439445
}
440446

441-
void socket::impl::__timeout_connection(const boost::system::error_code &ec)
447+
void socket::impl::timeout_connection(const boost::system::error_code &ec)
442448
{
443449
NULL_GUARD(m_client);
444450
if(ec)
@@ -450,7 +456,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
450456
this->on_disconnect();
451457
}
452458

453-
void socket::impl::__send_packet(sio::packet &p)
459+
void socket::impl::send_packet(sio::packet &p)
454460
{
455461
NULL_GUARD(m_client);
456462
if(m_connected)

src/sio_socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace sio
6767

6868
void off_error();
6969

70-
void emit(std::string const& name, message::list const& msglist, std::function<void (message::ptr const&)> const& ack = nullptr);
70+
void emit(std::string const& name, message::list const& msglist = nullptr, std::function<void (message::ptr const&)> const& ack = nullptr);
7171

7272
std::string const& get_namespace() const;
7373

test/sio_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ BOOST_AUTO_TEST_CASE( test_packet_accept_2 )
6767

6868
BOOST_AUTO_TEST_CASE( test_packet_accept_3 )
6969
{
70-
packet p("/nsp",string_message::create("test"),1001,true);
70+
message::ptr array = array_message::create();
71+
array->get_vector().push_back(string_message::create("event"));
72+
array->get_vector().push_back(string_message::create("text"));
73+
packet p("/nsp",array,1001,true);
7174
std::string payload;
7275
std::vector<std::shared_ptr<const std::string> > buffers;
7376
p.accept(payload,buffers);
7477
BOOST_CHECK(p.get_type() == packet::type_ack);
7578
BOOST_CHECK(buffers.size() == 0);
76-
BOOST_CHECK_MESSAGE(payload == "43/nsp,1001\"test\"",std::string("outputing payload:")+payload);
79+
BOOST_CHECK_MESSAGE(payload == "43/nsp,1001[\"event\",\"text\"]",std::string("outputing payload:")+payload);
7780
}
7881

7982
BOOST_AUTO_TEST_CASE( test_packet_accept_4 )

0 commit comments

Comments
 (0)