Comments (10)
What server are you communicating with? A minimalistic example would also help. Also keep in mind that the connection is closed if an error occurs (on_error is called).
from simple-websocket-server.
The internal wss.
If you send a message with disconnected, on_error occurs and you are calling start again to connect to the server.
What if I do not have this plan?
from simple-websocket-server.
/* header file /
WssClient _pClient = NULL;
shared_ptrWssClient::Connection _connection;
/* cpp file */
void CWSTest::on_message(shared_ptrWssClient::Connection connection, shared_ptrWssClient::Message message) {
}
void CWSTest::on_open(shared_ptrWssClient::Connection connection) {
_connection = connection;
}
void CWSTest::on_close(shared_ptrWssClient::Connection connection, int status, const string & reason) {
}
void CWSTest::on_error(shared_ptrWssClient::Connection connection, const SimpleWeb::error_code &ec) {
}
unsigned WINAPI StartThreadFunction(void* arg) {
WssClient* pClient = static_cast<WssClient*>(arg);
while (true)
{
pClient->start();
Sleep(1000);
}
}
bool CWSTest::Connect() {
_pClient = new WssClient("localhost:8080/echo", false);
_pClient->on_message = (std::bind(&CWSTest::on_message, this, std::placeholders::_1, std::placeholders::_2));
_pClient->on_open = (std::bind(&CWSTest::on_open, this, std::placeholders::_1));
_pClient->on_close = (std::bind(&CWSTest::on_close, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
_pClient->on_error = (std::bind(&CWSTest::on_error, this, std::placeholders::_1, std::placeholders::_2));
_beginthreadex(&sa, 0, StartThreadFunction, _pClient, 0, NULL);
}
void CWSTest::send_data() {
auto send_stream = make_sharedWssClient::SendStream();
*send_stream << "test";
_connection->send(send_stream);
}
==============================================
In the above example, when send_data is called in normal state, it is called as on_message, but when send_data is called externally with the network disconnected, on_error is raised. If the network is connected again in the above state, on_open is generated and it looks like connected. If send_data is called again in this state, on_message is not raised and on_error is raised.
from simple-websocket-server.
Because of the ambiguous content modify comment
- The error code after reconnection is error_code.m_val = 2
from simple-websocket-server.
I failed to reproduce your error with the following changes:
diff --git a/ws_examples.cpp b/ws_examples.cpp
index d2dcfc2..315b31b 100644
--- a/ws_examples.cpp
+++ b/ws_examples.cpp
@@ -137,5 +137,11 @@ int main() {
client.start();
+ thread t([&client] {
+ this_thread::sleep_for(chrono::seconds(3));
+ client.stop();
+ client.start();
+ });
+
server_thread.join();
}
Maybe try call ::stop()
before calling ::start()
? This will close the connection properly maybe.
from simple-websocket-server.
If you reconnect and send a message, an error occurs in the function below.
void read_message(const std::shared_ptr &connection, std::size_t num_additional_bytes) {
asio::async_read(*connection->socket, connection->message->streambuf, asio::transfer_exactly(num_additional_bytes > 2 ? 0 : 2 - num_additional_bytes), [this, connection](const error_code &ec, std::size_t bytes_transferred) {
auto lock = connection->handler_runner->continue_lock();
if(!lock)
return;
if(!ec) { // ec.m_val = 2
if(bytes_transferred == 0 && connection->message->streambuf.size() == 0) { // TODO: This might happen on server at least, might also happen here
this->read_message(connection, 0);
return;
}
from simple-websocket-server.
It is expected to be an internal problem.
Thank you for your reply.
from simple-websocket-server.
Did you try calling stop before calling start again? Another option is to create a new client object when reconnecting.
This might also be a Windows issue only, since I have not yet been able to reproduce it on a Unix-like system.
from simple-websocket-server.
It has been determined that an error will occur if an internal server does not send a reconnected message.
Thank you for answering the wrong question.
from simple-websocket-server.
I can't reproduce the problem you describe on Unix-like systems. As I see it, this is either a Windows issue or a problem on your side. Without working example code I cannot know.
from simple-websocket-server.
Related Issues (20)
- [suggest] Send/Broadcast and Text/BinaryMode HOT 1
- Standalone Asio not found - but asio.hpp is present HOT 4
- When I use Google Chrome it happend a error. HOT 3
- exception thrown in server start (boost related) HOT 4
- Connection Timeout HOT 2
- Create example on how to stream data to a client HOT 1
- Unable to compile against standalone asio HOT 1
- How to stop the server? HOT 3
- Installed but ws_examples throws an error HOT 1
- Support for the Sec-WebSocket-Protocol field HOT 3
- fatal error C1083: Cannot open include file : 'pthread.h': No such file or directory HOT 1
- Messages split among frames aren't reasembled HOT 25
- Openssl wants void* instead of char* in 0x1000115fL HOT 1
- how to initiate send message from client HOT 3
- Client request error: End of file
- Access violation exeption HOT 3
- gdiplus
- Can't use wss client HOT 5
- Integration with Simple-Web-Server HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-websocket-server.