示例#1
0
int main()
{
#ifdef _WIN32
	INT rc;
    WSADATA wsaData;

	rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (rc) {
		printf("WSAStartup Failed.\n");
        return 1;
    }
#endif

    ws = WebSocket::from_url("ws://localhost:8126/foo");
    assert(ws);
    ws->send("goodbye");
    ws->send("hello");
    while (ws->getReadyState() != WebSocket::CLOSED) {
      ws->poll();
      ws->dispatch(handle_message);
    }

#ifdef _WIN32
	WSACleanup();
#endif
    return 0;
}
示例#2
0
int main()
{
    ws = WebSocket::from_url("ws://localhost:8126/foo");
    assert(ws);
    ws->send("goodbye");
    ws->send("hello");
    while (ws->getReadyState() != WebSocket::CLOSED) {
      ws->poll();
      ws->dispatch(handle_message);
    }
    return 0;
}
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;
}
示例#5
0
int main()
{
    ws = WebSocket::from_url("ws://10.242.5.212:8080");
    assert(ws);
    std::string data = "{ \"data\": { \"cardnumber\": \"042F1919721D80\", \"expiry\": \"\", \"pin\": \"1234\", \"roles\": [ { \"profile\": \"1\", \"type\": \"11\" } ], \"serviceprovider\": \"1001\", \"staffid\": \"5070\", \"type\": \"operator\", \"valid\": true }, \"event\": \"cardpresented\", \"name\": \"cardevent\", \"terminalid\": \"3\", \"type\": \"PUT\", \"userid\": null }";
    
    printf(">>> %s\n", data.c_str());
    ws->send(data.c_str());

    while (ws->getReadyState() != WebSocket::CLOSED) {
      ws->poll();
      ws->dispatch(handle_message);
    }
    delete ws;

    return 0;
}
示例#6
0
int main(int argc, char* argv[]) {

	if (argc < 2) {
		std::cout << "Usage: ecg ws://<host>:<port>/<path>\n";
		exit(0);
	}

	ws = WebSocket::from_url(argv[1]);
	send_message("CONNECT\naccept-version:1.1,1.0\n\n");
	while (ws->getReadyState() != WebSocket::CLOSED) {
		ws->poll();
		ws->dispatch(handle_connect);
	}

	delete ws;
	return 0;
}
示例#7
0
void output_ws_handle_data(obd_data_t *obd) {
    if (!ws || ws->getReadyState() == WebSocket::CLOSED) {
        delete ws;
        res_init();
        ws = WebSocket::from_url(url);
        if (!ws)
            return;
    }

    // We forget endianess exists...
    std::vector<uint8_t> buf;
    uint8_t* start = reinterpret_cast<uint8_t*>(obd);
    // header that indicates type 1, which is telemetry
    buf.push_back(1);
    buf.insert(buf.end(), start, start + sizeof(obd_data_t));
    ws->sendBinary(buf);
    ws->poll();
    ws->dispatchBinary(handle_binary_message);
}
示例#8
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();
}
int main(int argc, char* argv[]) {

	using easywsclient::WebSocket;
	WebSocket::pointer ws = NULL;
	
	sixenseControllerData data;
	sixenseInit();
	
	int numctrl;

	while (!(numctrl = sixenseGetNumActiveControllers()))
		;

	std::ostringstream msgbuf;
	VRCom::Update* updateMsg = new VRCom::Update();
	VRCom::Hydra* msg = new VRCom::Hydra();
	updateMsg->set_allocated_hydra(msg);
	VRCom::Joystick* joy = new VRCom::Joystick();
	VRCom::Position* pos = new VRCom::Position();
	VRCom::Rotation* rot = new VRCom::Rotation();
	msg->set_allocated_pos(pos);
	msg->set_allocated_rot(rot);
	msg->set_allocated_joy(joy);

    // open the websocket server connection
    #ifdef _WIN32
    INT rc;
    WSADATA wsaData;

    rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (rc) {
      printf("WSAStartup Failed.\n");
      return 1;
    }
    #endif

    std::string websocketAddr = "ws://192.168.11.34:4567";

    if (argc > 1) {
    	websocketAddr = argv[1];
    }

    ws = WebSocket::from_url(websocketAddr);
    assert(ws);

	while(true) {
		for (int i = 0; i < numctrl; i++) {
			sixenseGetNewestData(i, &data);

			//std::cerr << "controller: " << i << std::endl;
			//std::cerr << "pos: " << data.pos[0] << ", " << data.pos[1] << ", " << data.pos[2] << std::endl;
			// std::cerr << "rot: " << data.rot_quat[0] << ", " << data.rot_quat[1] << ", " << data.rot_quat[2] << ", " << data.rot_quat[3] << std::endl;
			// std::cerr << "joystick: " << data.joystick_x << ", " << data.joystick_y << std::endl;
			// std::cerr << "trigger: " << data.trigger << std::endl;
			// std::cerr << "buttons: " << (data.buttons&SIXENSE_BUTTON_BUMPER) << " "
			// 						<< (data.buttons&SIXENSE_BUTTON_JOYSTICK) << " "
			// 						<< (data.buttons&SIXENSE_BUTTON_1) << " "
			// 						<< (data.buttons&SIXENSE_BUTTON_2) << " "
			// 						<< (data.buttons&SIXENSE_BUTTON_3) << " "
			// 						<< (data.buttons&SIXENSE_BUTTON_4) << " "
			// 						<< (data.buttons&SIXENSE_BUTTON_START) << std::endl;

			msg->set_ctrl_num(i);
			pos->set_x(data.pos[0]);
			pos->set_y(data.pos[1]);
			pos->set_z(data.pos[2]);
			rot->set_x(data.rot_quat[0]);
			rot->set_y(data.rot_quat[1]);
			rot->set_z(data.rot_quat[2]);
			rot->set_w(data.rot_quat[3]);
			joy->set_x(data.joystick_x);
			joy->set_y(data.joystick_y);
			msg->set_trigger(data.trigger);
			msg->set_buttons(data.buttons);

			updateMsg->SerializeToOstream(&msgbuf);
			ws->sendBinary(msgbuf.str());
			ws->poll();

			std::ostringstream().swap(msgbuf);
          	msgbuf.clear();

			std::this_thread::sleep_for(std::chrono::milliseconds(20));
			// msg.reset();
			//std::cerr << data.pos[0] << std::endl;
			//ws->send("5:1::{\"name\":\"hydra\",\"args\":\"bla\"}");
		
		}
	}

}