void pushcpp::EventThread()
{
	while (true) {
		/* attempt to connect */
		DEBUG("polling thread started");

		while (
			this->m_websocket == NULL ||
			((WebSocket::pointer)this->m_websocket)->
			getReadyState() == WebSocket::CLOSED
		) {
			DEBUG("Attempting to connect!");

			if (this->m_websocket != NULL)
				delete((WebSocket::pointer) this->m_websocket);

			this->m_websocket = (void*) WebSocket::from_url(m_url);
		}

		WebSocket::pointer ws = (WebSocket::pointer) this->m_websocket;

		DEBUG("connected, (re)subscribing to channels");

		while (ws->getReadyState() != WebSocket::CLOSED) {
			ws->poll(100);
			ws->dispatch([this, ws](const string & msg) {
				WS_Dispatch(msg);
			});

			if (m_wantDisconnect)
				ws->close();
		}

		DEBUG("Lost connection, readyState: %d", ws->getReadyState());
		this->m_socketId = "";

		for (auto it = m_channelData.begin(); it != m_channelData.end(); it++)
			it->second.clear();

		if (m_connectionEventHandler)
			m_connectionEventHandler(ConnectionEvent::DISCONNECTED);

		if (m_wantDisconnect)
			break;
	}

	m_wantDisconnect = false;

	DEBUG("thread was stopped");
}
int main()
{
    using easywsclient::WebSocket;
    WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo");
    assert(ws);
    ws->send("goodbye");
    while (true) {
        ws->poll();
        ws->dispatch([](const std::string & message) {
            printf(">>> %s\n", message.c_str());
            ws->close();
        });
    }
    return 0;
}
Beispiel #3
0
void handle_connect(const std::string & message) {

	std::cout << "<<<" << message << "\n";

	while (1) {

		std::stringstream commandStr;
		commandStr << "SEND\n";
		commandStr << "destination:/ecg\n\n{";
		commandStr << "\"x\":" << get_current_time();
		commandStr << ",\"y\":" << eHealth.getECG();
		commandStr << "}\n";

		send_message(commandStr.str());

		ws->poll();

		delay(10);
	}
	ws->close();
}
Beispiel #4
0
void handle_message(const std::string & message)
{
    printf("<<< %s\n", message.c_str());
    if (message == "world") { ws->close(); }
}