Skip to content

Commit ec4d540

Browse files

10 files changed

+204
-57
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ By virtue of being written in C++, this client works in several different platfo
66

77
[![Clients with iPhone, QT, Console and web](https://cldup.com/ukvVVZmvYV.png)](https://github.com/socketio/socket.io-client-cpp/tree/master/examples)
88

9+
## Compatibility table
10+
11+
| C++ Client version | Socket.IO server version |
12+
| ------------------- | ------------------------ |
13+
| 2.x (`2.x` branch) | 1.x / 2.x |
14+
| 3.x (master branch) | 3.x |
15+
916
## Features
1017

1118
- 100% written in modern C++11
12-
- Compatible with socket.io 1.0+ protocol
1319
- Binary support
1420
- Automatic JSON encoding
1521
- Multiplex support
1622
- Similar API to the Socket.IO JS client
1723
- Cross platform
1824

25+
Note: Only the WebSocket transport is currently implemented (no fallback to HTTP long-polling)
26+
1927
## Installation alternatives
2028

2129
* [With CMAKE](./INSTALL.md#with-cmake)

src/internal/sio_client_impl.cpp

+10-21
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ namespace sio
222222
} else {
223223
ss<<uo.get_host();
224224
}
225-
ss<<":"<<uo.get_port()<<"/socket.io/?EIO=3&transport=websocket";
225+
ss<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket";
226226
if(m_sid.size()>0){
227227
ss<<"&sid="<<m_sid;
228228
}
@@ -295,12 +295,6 @@ namespace sio
295295
lib::error_code ec;
296296
this->m_client.send(this->m_con, *payload, frame::opcode::text, ec);
297297
});
298-
if(m_ping_timer)
299-
{
300-
asio::error_code e_code;
301-
m_ping_timer->expires_from_now(milliseconds(m_ping_interval), e_code);
302-
m_ping_timer->async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1));
303-
}
304298
if(!m_ping_timeout_timer)
305299
{
306300
m_ping_timeout_timer.reset(new asio::steady_timer(m_client.get_io_service()));
@@ -497,21 +491,21 @@ namespace sio
497491
m_ping_timeout = 60000;
498492
}
499493

500-
m_ping_timer.reset(new asio::steady_timer(m_client.get_io_service()));
501-
asio::error_code ec;
502-
m_ping_timer->expires_from_now(milliseconds(m_ping_interval), ec);
503-
if(ec)LOG("ec:"<<ec.message()<<endl){};
504-
m_ping_timer->async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1));
505-
LOG("On handshake,sid:"<<m_sid<<",ping interval:"<<m_ping_interval<<",ping timeout"<<m_ping_timeout<<endl);
506494
return;
507495
}
508496
failed:
509497
//just close it.
510498
m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::policy_violation,"Handshake error"));
511499
}
512500

513-
void client_impl::on_pong()
501+
void client_impl::on_ping()
514502
{
503+
packet p(packet::frame_pong);
504+
m_packet_mgr.encode(p, [&](bool /*isBin*/,shared_ptr<const string> payload)
505+
{
506+
this->m_client.send(this->m_con, *payload, frame::opcode::text);
507+
});
508+
515509
if(m_ping_timeout_timer)
516510
{
517511
m_ping_timeout_timer->cancel();
@@ -536,8 +530,8 @@ namespace sio
536530
//FIXME how to deal?
537531
this->close_impl(close::status::abnormal_close, "End by server");
538532
break;
539-
case packet::frame_pong:
540-
this->on_pong();
533+
case packet::frame_ping:
534+
this->on_ping();
541535
break;
542536

543537
default:
@@ -560,11 +554,6 @@ namespace sio
560554
m_ping_timeout_timer->cancel(ec);
561555
m_ping_timeout_timer.reset();
562556
}
563-
if(m_ping_timer)
564-
{
565-
m_ping_timer->cancel(ec);
566-
m_ping_timer.reset();
567-
}
568557
}
569558

570559
void client_impl::reset_states()

src/internal/sio_client_impl.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace sio
170170
//socketio callbacks
171171
void on_handshake(message::ptr const& message);
172172

173-
void on_pong();
173+
void on_ping();
174174

175175
void reset_states();
176176

@@ -201,8 +201,6 @@ namespace sio
201201

202202
packet_manager m_packet_mgr;
203203

204-
std::unique_ptr<asio::steady_timer> m_ping_timer;
205-
206204
std::unique_ptr<asio::steady_timer> m_ping_timeout_timer;
207205

208206
std::unique_ptr<asio::steady_timer> m_reconn_timer;

src/internal/sio_packet.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ namespace sio
5353
Value numVal;
5454
numVal.SetInt((int)buffers.size());
5555
val.AddMember("num", numVal, doc.GetAllocator());
56-
//FIXME can not avoid binary copy here.
57-
shared_ptr<string> write_buffer = make_shared<string>();
58-
write_buffer->reserve(msg.get_binary()->size()+1);
59-
char frame_char = packet::frame_message;
60-
write_buffer->append(&frame_char,1);
61-
write_buffer->append(*(msg.get_binary()));
62-
buffers.push_back(write_buffer);
56+
buffers.push_back(msg.get_binary());
6357
}
6458

6559
void accept_array_message(array_message const& msg,Value& val,Document& doc,vector<shared_ptr<const string> >& buffers)
@@ -253,7 +247,7 @@ namespace sio
253247
{
254248
if (_pending_buffers > 0) {
255249
assert(is_binary_message(buf_payload));//this is ensured by outside.
256-
_buffers.push_back(std::make_shared<string>(buf_payload.data()+1,buf_payload.size()-1));
250+
_buffers.push_back(std::make_shared<string>(buf_payload.data(),buf_payload.size()));
257251
_pending_buffers--;
258252
if (_pending_buffers == 0) {
259253

src/sio_socket.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ namespace sio
269269
void socket::impl::send_connect()
270270
{
271271
NULL_GUARD(m_client);
272-
if(m_nsp == "/")
273-
{
274-
return;
275-
}
276272
packet p(packet::type_connect,m_nsp);
277273
m_client->send(p);
278274
m_connection_timer.reset(new asio::steady_timer(m_client->get_io_service()));

test/echo_server/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
var port = 3000;
22

3-
var io = require('socket.io').listen(port);
3+
var io = require('socket.io')().listen(port);
44
console.log("Listening on port " + port);
55

66
/* Socket.IO events */
77
io.on("connection", function(socket){
88
console.log("new connection");
9-
socket.on('test_text',function()
10-
{
11-
console.log("test text event received.");
9+
socket.on('test_text', (...args) => {
10+
console.log("test text event received.", args);
1211
});
1312

14-
socket.on('test_binary',function()
15-
{
16-
var args =Array.prototype.slice.call(arguments);
13+
socket.on('test_binary', (...args) => {
14+
console.log("test binary event received", args);
1715
if(args[0] instanceof Buffer)
1816
{
1917
console.log("test binary event received,binary length:"+ args[0].length);

test/echo_server/package-lock.json

+165
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/echo_server/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "echo_server",
3-
"author":"Melo Yao",
4-
"version":"0.0.0",
3+
"author": "Melo Yao",
4+
"version": "0.0.0",
55
"dependencies": {
6-
"socket.io": "1.*"
7-
}
6+
"socket.io": "^3.0.5"
7+
}
88
}

test/sio_test

868 KB
Binary file not shown.

test/sio_test.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ TEST_CASE( "test_packet_accept_4" )
118118
int bin1Num = j["bin1"]["num"].get<int>();
119119
char numchar[] = {0,0};
120120
numchar[0] = bin1Num+'0';
121-
CHECK(buffers[bin1Num]->length()==101);
121+
CHECK(buffers[bin1Num]->length()==100);
122122
INFO("outputing payload bin1 num:" << numchar)
123123
CHECK(buffers[bin1Num]->at(50)==0);
124-
CHECK(buffers[bin1Num]->at(0) == packet::frame_message);
124+
CHECK(buffers[bin1Num]->at(0) == 0);
125125
int bin2Num = j["bin2"]["num"].get<int>();
126126
numchar[0] = bin2Num+'0';
127-
CHECK(buffers[bin2Num]->length()==51);
127+
CHECK(buffers[bin2Num]->length()==50);
128128
INFO("outputing payload bin2 num:" << numchar)
129129
CHECK(buffers[bin2Num]->at(25)==1);
130-
CHECK(buffers[bin2Num]->at(0) == packet::frame_message);
130+
CHECK(buffers[bin2Num]->at(0) == 1);
131131
}
132132
#endif
133133

@@ -209,12 +209,11 @@ TEST_CASE( "test_packet_parse_4" )
209209
packet p;
210210
bool hasbin = p.parse("452-/nsp,101[\"bin_event\",[{\"_placeholder\":true,\"num\":1},{\"_placeholder\":true,\"num\":0},\"text\"]]");
211211
CHECK(hasbin);
212-
char buf[101];
213-
buf[0] = packet::frame_message;
214-
memset(buf+1,0,100);
212+
char buf[100];
213+
memset(buf,0,100);
215214

216-
std::string bufstr(buf,101);
217-
std::string bufstr2(buf,51);
215+
std::string bufstr(buf,100);
216+
std::string bufstr2(buf,50);
218217
CHECK(p.parse_buffer(bufstr));
219218
CHECK(!p.parse_buffer(bufstr2));
220219

0 commit comments

Comments
 (0)