123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- // socket.io-client-cpp_test.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "sio_client.h"
- #include <functional>
- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- #include <string>
- #include "constdef.h"
- #include <chrono>
- #include <stdio.h>
- #include <tchar.h>
- #include <rapidjson/writer.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/prettywriter.h>
- #include "jsonCommon.h"
- #include <boost\locale.hpp>
- #include "wsClient.h"
- #include "wsClientMgr.h"
- #include "wsTimerThread.h"
- #include "singleton_test_class1.h"
- using namespace sio;
- using namespace std;
- std::mutex _lock;
- std::condition_variable_any _cond;
- bool connect_finish = false;
- bool socket_opened = false;
- class connection_listener
- {
- public:
- connection_listener()
- {
- }
- void on_connected()
- {
- std::cout << "sio connected" << std::endl;
- _lock.lock();
- _cond.notify_all();
- connect_finish = true;
- _lock.unlock();
- }
- void on_close( client::close_reason const& reason )
- {
- std::cout << "sio closed" << std::endl;
- exit( 0 );
- }
- void on_fail()
- {
- std::cout << "sio failed" << std::endl;
- exit( 0 );
- }
- void on_socket_opened()
- {
- std::cout << "on_socket_opened" << std::endl;
- _lock.lock();
- socket_opened = true;
- _cond.notify_all();
- _lock.unlock();
- }
- void OnLogin( std::string const& name, message::ptr const& data, bool isAck, message::list &ack_resp )
- {
- int res_code = ( int ) data->get_map()["code"]->get_int();
- switch ( res_code )
- {
- case -100: //登录失败
- {
- std::cout << "sio failed to login" << std::endl;
- break;
- }
- case 0:
- {
- if ( "" == data->get_map()["code"]->get_string() )
- { // 登录成功
- std::cout << "sio login ok" << std::endl;
- }
- else
- { // 退出登录
- std::cout << "sio failed to login" << std::endl;
- }
- break;
- }
- default:
- break;
- }
- }
- void OnCallMessage( string const& name, message::ptr const& data, bool isAck, message::list &ack_resp )
- {
- std::cout << "OnCallMessage, name=" << name.c_str() << std::endl;
- if ( data->get_flag() == message::flag_object )
- {
- string cmd = data->get_map()[JSON_ROOT_KEY_CMD]->get_string();
- char szLog[1024] = { 0 };
- sprintf( szLog, "received cmd from web: %s", cmd.c_str() );
- std::cout << szLog << std::endl;
- }
- }
- void OnReConnect( unsigned, unsigned )
- {
- std::cout << "sio OnReConnect" << std::endl;
- connect_finish = false;
- socket_opened = false;
- }
- };
- std::unique_ptr<sio::client> ws_client( new sio::client() );
- //sio::client ws_client;
- connection_listener l;
- socket::ptr current_socket;
- std::string BuildLogin( const YA::_JS_LOGIN_& Login )
- {
- rapidjson::StringBuffer sb;
- rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
- rapidjson::Document doc;
- rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
- rapidjson::Value root( rapidjson::kObjectType );
- rapidjson::Value child( rapidjson::kObjectType );
- rapidjson::Value key( rapidjson::kStringType );
- rapidjson::Value value( rapidjson::kStringType );
- key.SetString( "cmd", allocator );
- value.SetString( JSON_CMD_VALUE_LOGIN, allocator );
- root.AddMember( key, value, allocator );
- key.SetString( "user_name", allocator );
- value.SetString( Login.user_name.c_str(), allocator );
- child.AddMember( key, value, allocator );
- key.SetString( "user_pass", allocator );
- value.SetString( Login.user_password.c_str(), allocator );
- child.AddMember( key, value, allocator );
- key.SetString( "data", allocator );
- root.AddMember( key, child, allocator );
- root.Accept( writer );
- return sb.GetString();
- }
- //初始化
- void ws_init()
- {
- ws_client->set_reconnect_attempts( 0 );
- using std::placeholders::_1;
- using std::placeholders::_2;
- using std::placeholders::_3;
- using std::placeholders::_4;
- socket::ptr sock = ws_client->socket();
- //sock->on( JSON_CMD_VALUE_USER, sio::socket::event_listener_aux( std::bind( &connection_listener::OnLogin, &l, _1, _2, _3, _4 ) ) );
- //sock->on( "code", sio::socket::event_listener_aux( std::bind( &connection_listener::OnLogin, &l, _1, _2, _3, _4 ) ) );
- sock->on( JSON_CMD_VALUE_CALL, sio::socket::event_listener_aux( std::bind( &connection_listener::OnCallMessage, &l, _1, _2, _3, _4 ) ) );
- ws_client->set_open_listener( std::bind( &connection_listener::on_connected, &l ) );
- ws_client->set_close_listener( std::bind( &connection_listener::on_close, &l, std::placeholders::_1 ) );
- ws_client->set_reconnect_listener(std::bind(&connection_listener::OnReConnect, &l, std::placeholders::_1, std::placeholders::_2));
- ws_client->set_fail_listener( std::bind( &connection_listener::on_fail, &l ) );
- ws_client->set_socket_open_listener( std::bind( &connection_listener::on_socket_opened, &l ) );
- }
- //登录
- void ws_login()
- {
- std::cout << "ws_login()" << std::endl;
- YA::_JS_LOGIN_ Login;
- Login.user_name = JSON_VALUE_USERNAME;
- Login.user_password = JSON_VALUE_PASSWORD;
- std::string strLogin = BuildLogin( Login );
- strLogin += "\n";
- strLogin = boost::locale::conv::to_utf<char>( strLogin, "GBK" );
- current_socket->emit( JSON_CMD_VALUE_USER, strLogin, [&]( sio::message::list const& msglist )
- {
- message::ptr msg_ptr = msglist[0];
- int n = ( int ) msg_ptr->get_map()["code"]->get_int();
- if ( 0 == n )
- {
- std::cout << "Login ok, code=" << n << std::endl;
- }
- else
- {
- std::cout << "Login failed,code=" << n << std::endl;
- }
- } );
- }
- void simple_test()
- {
- //初始化
- ws_init();
- ws_client->connect( "ws://192.168.5.101:8086" );
- //ws_client->connect( "ws://192.168.8.175:8086" );
- //ws_client->connect( "ws://localhost:8086" );
- _lock.lock();
- if ( !connect_finish )
- {
- _cond.wait( _lock );
- }
- _lock.unlock();
- current_socket = ws_client->socket();
- _lock.lock();
- if ( !socket_opened )
- {
- _cond.wait( _lock );
- }
- _lock.unlock();
- std::this_thread::sleep_for( std::chrono::milliseconds( 500 ) );
- //登录
- ws_login();
- system( "PAUSE" );
- }
- //处理req_all_data命令的函数
- void On_req_all_data( int ID, std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
- {
- std::cout << "[Onreq_all_data] ";
- std::cout << "{" << ID << "} ";
- string strdata = data->get_map()["data"]->get_string().c_str();
- string version = data->get_map()["version"]->get_string().c_str();
- std::cout << "data=" << strdata.c_str() << ", version=" << version.c_str() << std::endl;
- }
- void On_req_all_person_on_car( int ID, std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
- {
- std::cout << "[On_req_all_person_on_car] ";
- std::cout << "{" << ID << "} ";
- std::vector<message::ptr> vdata = data->get_map()["data"]->get_vector();
- std::cout << vdata[0]->get_string() << ",";
- std::cout << vdata[1]->get_int() << std::endl;
- }
- void wsClient_class_test()
- {
- YA::wsClient client;
- std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
- MsgFuncList.insert( std::make_pair( "req_all_data", On_req_all_data ) );
- MsgFuncList.insert( std::make_pair( "req_all_person_on_car", On_req_all_person_on_car ) );
- //初始化
- client.init( 1, "ws://192.168.5.101:8086", MsgFuncList );
- //连接服务器端
- if ( client.connect() != 0 )
- {
- std::cout << "连接服务器失败" << std::endl;
- client.close();
- return;
- }
- //登录
- client.login();
- }
- void wsClientMgr_class_test()
- {
- //构造
- std::vector<std::string> uri_list;
- std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
- uri_list.push_back( "ws://192.168.5.101:8086" );
- //uri_list.push_back( "ws://192.168.5.102:8086" );
- MsgFuncList.insert( std::make_pair( "req_all_data", On_req_all_data ) );
- MsgFuncList.insert( std::make_pair( "req_all_person_on_car", On_req_all_person_on_car ) );
- swsClientMgr.Build( uri_list, MsgFuncList );
- //连接服务器
- if ( swsClientMgr.connect() != 0 )
- {
- std::cout << "连接服务器失败" << std::endl;
- return;
- }
- //登录
- swsClientMgr.login();
- std::shared_ptr<YA::wsClient> pClient;
- //通过URI获得某个websocket客户端
- pClient = swsClientMgr.GetClientByURI( "ws://192.168.5.101:8086" );
- if ( pClient != NULL )
- {
- std::cout << "ID=" << pClient->GetID() << ",URI=" << pClient->get_uri().c_str() << std::endl;
- }
- //通过下标获得某个websocket客户端
- pClient = swsClientMgr[1];
- if ( pClient != NULL )
- {
- std::cout << "ID=" << pClient->GetID() << ",URI=" << pClient->get_uri().c_str() << std::endl;
- }
- }
- void wsTimerThread_test()
- {
- YA::_THREAD_CONFIG_ Config;
- Config.SendInterval = 1;
- swsTimerThrd.Init( Config );
- swsTimerThrd.Start();
- YA::_CARD_POS_ cardpos;
- //602 人卡
- cardpos.ID = 602;
- cardpos.x = 3326.9895924094;
- cardpos.y = 100;
- cardpos.down_time = 1536161101000;
- cardpos.enter_area_time = 1536161561000;
- cardpos.rec_time = 1536163198000;
- cardpos.work_time = 2099000;
- cardpos.map_id = 5;
- cardpos.area_id = 32;
- cardpos.dept_id = 12;
- cardpos.stat = 0;
- cardpos.running_stat = 7;
- cardpos.biz_stat = 0;
- cardpos.speed = 6;
- cardpos.landmark_id = 27;
- cardpos.lm_direction = 2;
- cardpos.landmark_dis = 426;
- cardpos.level_id = 6;
- cardpos.is_on_duty = 0;
- cardpos.Type = 1;
- swsTimerThrd.upt_card_pos( cardpos );
- //603 人卡
- cardpos.ID = 603;
- cardpos.x = 3326.9895924094;
- cardpos.y = 100;
- cardpos.down_time = 1536161101000;
- cardpos.enter_area_time = 1536161561000;
- cardpos.rec_time = 1536163198000;
- cardpos.work_time = 2099000;
- cardpos.map_id = 5;
- cardpos.area_id = 22;
- cardpos.dept_id = 12;
- cardpos.stat = 0;
- cardpos.running_stat = 7;
- cardpos.biz_stat = 0;
- cardpos.speed = 6;
- cardpos.landmark_id = 27;
- cardpos.lm_direction = 2;
- cardpos.landmark_dis = 426;
- cardpos.level_id = 6;
- cardpos.is_on_duty = 0;
- cardpos.Type = 1;
- swsTimerThrd.upt_card_pos( cardpos );
- //197 人卡
- cardpos.ID = 197;
- cardpos.x = 4741.15;
- cardpos.y = -37.12;
- cardpos.down_time = 1534390846000;
- cardpos.enter_area_time = 1534403504000;
- cardpos.rec_time = 1534551770000;
- cardpos.work_time = 0;
- cardpos.map_id = 5;
- cardpos.area_id = 5;
- cardpos.dept_id = 2;
- cardpos.stat = 0;
- cardpos.running_stat = 0;
- cardpos.biz_stat = 0;
- cardpos.speed = 6;
- cardpos.landmark_id = 9;
- cardpos.lm_direction = 4;
- cardpos.landmark_dis = 10;
- cardpos.level_id = 0;
- cardpos.is_on_duty = 0;
- cardpos.Type = 2;
- swsTimerThrd.upt_card_pos( cardpos );
- //198 人卡
- cardpos.ID = 198;
- cardpos.x = 4741.15;
- cardpos.y = -37.12;
- cardpos.down_time = 1534390846000;
- cardpos.enter_area_time = 1534403504000;
- cardpos.rec_time = 1534551770000;
- cardpos.work_time = 0;
- cardpos.map_id = 5;
- cardpos.area_id = 6;
- cardpos.dept_id = 2;
- cardpos.stat = 0;
- cardpos.running_stat = 0;
- cardpos.biz_stat = 0;
- cardpos.speed = 6;
- cardpos.landmark_id = 9;
- cardpos.lm_direction = 4;
- cardpos.landmark_dis = 10;
- cardpos.level_id = 0;
- cardpos.is_on_duty = 0;
- cardpos.Type = 2;
- swsTimerThrd.upt_card_pos( cardpos );
- boost::this_thread::sleep( boost::posix_time::millisec( 5000 ) );
- swsTimerThrd.Stop();
- }
- int main()
- {
- //simple_test();
- //wsClient_class_test();
- //wsClientMgr_class_test();
- wsTimerThread_test();
- singleton_test_class1 st1;
- st1.DoSome();
- while ( true )
- {
- std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
- }
- return 0;
- }
|