eidheim / Simple-WebSocket-Server

A very simple, fast, multithreaded, platform independent WebSocket (WS) and WebSocket Secure (WSS) server and client library implemented using C++11, Boost.Asio and OpenSSL. Created to be an easy way to make WebSocket endpoints in C++.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Client blocking the program

juvasquezg opened this issue · comments

Hi,

Excuse me for the question, I am not an expert.

I have a program that gets camera stream using live555 with live555helper

I need to send stream by WebSocket. I have in live555helper src/rtspconnectionclient.cpp

void RTSPConnection::start()
{
	if (m_rtspClient)
	{
		Medium::close(m_rtspClient);
	}
	
	m_rtspClient = new RTSPClientConnection(*this, m_env, m_callback, m_url, m_timeout, 
 m_rtpovertcp, m_verbosity);

        // Simple-WebSocket-Client 
	client.on_message=[&client](shared_ptr<WsClient::Message> message) {
		auto message_str=message->string();
	
		cout << "Client: Message received: \"" << message_str << "\"" << endl;
	
		// No close, I want to send later again
	};

	client.on_open=[&client]() {
		cout << "Client: Opened connection" << endl;

		string message="Hello";
		cout << "Client: Sending message: \"" << message << "\"" << endl;

		auto send_stream=make_shared<WsClient::SendStream>();
		*send_stream << message;
		client.send(send_stream);
	};

	client.on_close=[](int status, const string& /*reason*/) {
		cout << "Client: Closed connection with status code " << status << endl;
	};

	//See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
	client.on_error=[](const boost::system::error_code& ec) {
		cout << "Client: Error: " << ec << ", error message: " << ec.message() << endl;
	};

	client.start();
}

The program blocking

I get

Opening connection to 192.168.1.38, port 554...
Client: Opened connection
Client: Sending message: "Hello"
Client: Message received: "Recibido"

Then I send more data in live555helper src/rtspconnectionclient.cpp

void RTSPConnection::SessionSink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds)
{
        // Send more data
	string message="Hello";
	cout << "Client: Sending message: \"" << message << "\"" << endl;

	auto send_stream=make_shared<WsClient::SendStream>();
	*send_stream << message;
	client.send(send_stream);
	if (numTruncatedBytes != 0)
	{
		delete [] m_buffer;
		envir() << "buffer too small " << (int)m_bufferSize << " allocate bigger one\n";
		allocate(m_bufferSize*2);
	}
	else if (m_callback)
	{
		if (!m_callback->onData(this->name(), m_buffer, frameSize+m_markerSize, presentationTime))
		{
			envir() << "NOTIFY failed\n";
		}
	}
	this->continuePlaying();
}

The program does not reach the afterGettingFrame

Only if I add

cout << "Client: Sending close connection" << endl;
client.send_close(1000);

Every time I send a message.

Can I keep the connection to the server open and then send the data?

Thank you so much!

I cannot see anything wrong here. The websocket client should attempt to send a message to the connected server when afterGettingFrame is called.

Hi,

I have server_ws.cpp

#include "server_ws.hpp"

using namespace std;

typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer;

int main()
{
	WsServer server;
	server.config.port = 8080;

	// and enpoint echo
	auto& echo=server.endpoint["^/echo/?$"];

	echo.on_message=[&server](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message) {
		auto message_str=message->string();

		cout << "Server: Message received: \"" << message_str << "\" from " << (size_t)connection.get() << endl;

		cout << "Server: Sending message \"" << message_str <<  "\" to " << (size_t)connection.get() << endl;

		string message_ack="Recibido";
		auto send_stream=make_shared<WsServer::SendStream>();
		*send_stream << message_ack;
		server.send(connection, send_stream, [](const boost::system::error_code& ec){
            if(ec) {
                cout << "Server: Error sending message. " <<
                //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
                        "Error: " << ec << ", error message: " << ec.message() << endl;
            }
        });
	};

	echo.on_open=[](shared_ptr<WsServer::Connection> connection) {
        cout << "Server: Opened connection " << (size_t)connection.get() << endl;
    };

    echo.on_close=[](shared_ptr<WsServer::Connection> connection, int status, const string& /*reason*/) {
        cout << "Server: Closed connection " << (size_t)connection.get() << " with status code " << status << endl;
    };

    echo.on_error=[](shared_ptr<WsServer::Connection> connection, const boost::system::error_code& ec) {
        cout << "Server: Error in connection " << (size_t)connection.get() << ". " << 
                "Error: " << ec << ", error message: " << ec.message() << endl;
    };

	thread server_thread([&server](){
        //Start WS-server
        server.start();
    });

	//Wait for server to start so that the client can connect
    this_thread::sleep_for(chrono::seconds(1));


    server_thread.join();
	
    return 0;
}

When I run server_ws afterGettingFrame is not called.

I get server_ws.cpp

Server: Opened conecction 140519250332720
Server: Message received: "Hello" from 140519250332720
Server: Sending message "Recibido" to 140519250332720

and the client is not calling afterGettingFrame, I get

Client: Opened connection
Client: Sending message: "Hello"
Client: Message received: "Recibido"

When I don't run server_ws.cpp the client call afterGettingFrame.

The server_ws.cpp es the problem.

What do you think?

Thank you!!

I do not have time to look into the details here, but client.start(); blocks your RTSPConnection::start(). If it should not, you need to put client.start() into a thread.

Hi,

Thank you so much. I will try. Closed.

Regards,