Coder Social home page Coder Social logo

Comments (10)

eidheim avatar eidheim commented on June 12, 2024

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.

brant-m avatar brant-m commented on June 12, 2024

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.

brant-m avatar brant-m commented on June 12, 2024

/* 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.

brant-m avatar brant-m commented on June 12, 2024

Because of the ambiguous content modify comment

  • The error code after reconnection is error_code.m_val = 2

from simple-websocket-server.

eidheim avatar eidheim commented on June 12, 2024

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.

brant-m avatar brant-m commented on June 12, 2024

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.

brant-m avatar brant-m commented on June 12, 2024

It is expected to be an internal problem.
Thank you for your reply.

from simple-websocket-server.

eidheim avatar eidheim commented on June 12, 2024

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.

brant-m avatar brant-m commented on June 12, 2024

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.

eidheim avatar eidheim commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.