• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C++ webSocket类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中webSocket的典型用法代码示例。如果您正苦于以下问题:C++ webSocket类的具体用法?C++ webSocket怎么用?C++ webSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了webSocket类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: periodicHandler

/* called once per select() loop */
void periodicHandler() {
	vector<int> clientIDs = server.getClientIDs();

		static time_t next = time(NULL)+1;
		time_t current = time(NULL);
		messageDelay();
		
		if (gameStarted) {
			if (current   >= next) {
				snakeState.UpdateBoardState();

				ostringstream ss;
				ostringstream score1;
				ostringstream score2;
				ss << "GB:" << snakeState.GetBoardState();
				score1 << "1:" << player1 + " score: " << snakeState.GetPlayerScore(0);
				score2 << "2:" << player2 + " score: " << snakeState.GetPlayerScore(1);

				for (int i = 0; i < clientIDs.size(); i++){
					server.wsSend(clientIDs[i], ss.str());
					server.wsSend(clientIDs[i], score1.str());
					server.wsSend(clientIDs[i], score2.str());
				}

				next = time(NULL) + 1;
			}
		}
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:29,代码来源:main.cpp


示例2: openHandler

void openHandler(int clientID){
    
    json msg; // Our first message to the client
    
    // If Player 1 just connected...
    if (server.getClientIDs().size() == 1) {
        // Send msg: you've been assigned player 1
        msg["MESSAGE_TYPE"] = "PLAYER_ASSIGNMENT";
        msg["PLAYER_NUMBER"] = 1;
		msg["UPDATE_CYCLE_LENGTH"] = UPDATE_CYCLE_LENGTH_MS;
        send_message(clientID, msg);
    }
    
    // If Player 2 just connected...
    else if (server.getClientIDs().size() == 2) {
        // Send msg: you've been assigned player 2
        msg["MESSAGE_TYPE"] = "PLAYER_ASSIGNMENT";
        msg["PLAYER_NUMBER"] = 2;
		msg["UPDATE_CYCLE_LENGTH"] = UPDATE_CYCLE_LENGTH_MS;
        send_message(clientID, msg);
    }
    
    // Or if there are too many connections, reject it:
    else {
        msg["MESSAGE_TYPE"] = "CONNECTION_REJECTED";
        send_message(clientID, msg);
        server.wsClose(clientID);
    }
}
开发者ID:jasaaved,项目名称:Resume,代码行数:29,代码来源:main.cpp


示例3: InterpretCommand

void InterpretCommand(int clientID, std::string message) {
	bool named = false;
	vector<int> clientIDs = server.getClientIDs();

	ostringstream os;
	ostringstream os2;

	if (message.find("NewPlayer:") == 0) {
		if (message.length() > 10) {
			if (player1 == "")
				player1 = message.substr(10);
			else
				player2 = message.substr(10);
		}
		named = true;
	}

	if (!named) {
		vector<int> clientIDs = server.getClientIDs();
		if (message.length() > 7) {
			if (clientID == clientIDs[0]) {
				snakeState.SetPlayerInput(0, message[8]);
			}
			else { snakeState.SetPlayerInput(1, message[8]); }
		}
	}
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:27,代码来源:main.cpp


示例4: messageHandler

/* called when a client sends a message to the server */
void messageHandler(int clientID, string message){
    ostringstream os;
    os << "Stranger " << clientID << " says: " << message;

    vector<int> clientIDs = server.getClientIDs();
    for (int i = 0; i < clientIDs.size(); i++){
        if (clientIDs[i] != clientID)
            server.wsSend(clientIDs[i], os.str());
    }
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:11,代码来源:mile1main.cpp


示例5: closeHandler

/* called when a client disconnects */
void closeHandler(int clientID){
    ostringstream os;
    os << "Stranger " << clientID << " has leaved.";

    vector<int> clientIDs = server.getClientIDs();
    for (int i = 0; i < clientIDs.size(); i++){
        if (clientIDs[i] != clientID)
            server.wsSend(clientIDs[i], os.str());
    }
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:11,代码来源:realMain.cpp


示例6: openHandler

/* called when a client connects */
void openHandler(int clientID){
    ostringstream os;
    os << "Stranger " << clientID << " has joined.";

    vector<int> clientIDs = server.getClientIDs();
    for (int i = 0; i < clientIDs.size(); i++){
        if (clientIDs[i] != clientID)
            server.wsSend(clientIDs[i], os.str());
    }
    server.wsSend(clientID, "Welcome!");
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:12,代码来源:mile1main.cpp


示例7: closeHandler

/* called when a client disconnects */
void closeHandler(int clientID){
	ostringstream os;
	os << "Stranger " << clientID << " has leaved.";

	vector<int> clientIDs = server.getClientIDs();
	for (int i = 0; i < clientIDs.size(); i++){
		server.wsSend(clientIDs[i], "Disconnected");
		server.wsClose(clientIDs[i]);
	}
	ReceiveQueue.clear();
	SendQueue.clear();

}
开发者ID:turtleaki,项目名称:Two-Player-Snake,代码行数:14,代码来源:main.cpp


示例8: ProcessMessages

void ProcessMessages() {
	time_t t1 = time(NULL);

	int index = 0;
	if (message_to_process[0].find("AVGL") != std::string::npos) {
		index = message_to_process[0].find("AVGL");
	}
	else {
		if (message_to_process[0].find("COMMAND") != std::string::npos) {
			index = message_to_process[0].find("COMMAND");
			InterpretCommand(0, message_to_process[0].substr(index));
		}
		else if (message_to_process[0].find("NewPlayer") != std::string::npos) {
			index = message_to_process[0].find("NewPlayer");
			InterpretCommand(0, message_to_process[0].substr(index));
		}

		time_t t2 = time(NULL);

		stringstream ss;
		ss << "t0:" << message_to_process[0].substr(4, message_to_process[0].length() - (message_to_process[0].length() - index + 5)) << ";t1:" << time_received[0] << ";t2:" << t2;
		server.wsSend(0, ss.str());
		delay_time1 = time(NULL) + (rand() % 2);
	}

	index = 0;

	if (message_to_process[1].find("AVGL") != std::string::npos) {
		index = message_to_process[1].find("AVGL");
		int delay = convertToInt(message_to_process[1].substr(index + 5));
	}
	else {
		if (message_to_process[1].find("COMMAND") != std::string::npos) {
			index = message_to_process[1].find("COMMAND");
			InterpretCommand(1, message_to_process[1].substr(index));
		}
		else if (message_to_process[1].find("NewPlayer") != std::string::npos) {
			index = message_to_process[1].find("NewPlayer");
			InterpretCommand(1, message_to_process[1].substr(index));
		}
		time_t t2 = time(NULL);

		stringstream ss;
		ss << "t0:" << message_to_process[1].substr(4, message_to_process[1].length() - (message_to_process[1].length() - index + 5)) << ";t1:" << time_received[1] << ";t2:" << t2;
		server.wsSend(1, ss.str());
		delay_time2 = time(NULL) + (rand() % 2);
	}
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:48,代码来源:main.cpp


示例9: openHandler

/* called when a client connects */
void openHandler(int clientID){
	score1 = 0;
	score2 = 0;

	server.wsSend(clientID, "Welcome!");

}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:8,代码来源:realMain.cpp


示例10: periodicHandler

/* called once per select() loop */
void periodicHandler(){
    static time_t next = time(NULL) + 10;
    time_t current = time(NULL);
    if (current >= next){
        ostringstream os;
        string timestring = ctime(&current);
        timestring = timestring.substr(0, timestring.size() - 1);
        os << timestring;

        vector<int> clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++)
            server.wsSend(clientIDs[i], os.str());

        next = time(NULL) + 10;
    }
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:17,代码来源:realMain.cpp


示例11: main

int main(int argc, char *argv[]){
    int port;

    cout << "Please set server port: ";
    cin >> port;

    /* set event handler */
    server.setOpenHandler(openHandler);
    server.setCloseHandler(closeHandler);
    server.setMessageHandler(messageHandler);
    //server.setPeriodicHandler(periodicHandler);

    /* start the chatroom server, listen to ip '127.0.0.1' and port '8000' */
    server.startServer(port);
    return 1;
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:16,代码来源:realMain.cpp


示例12: messageDelay

void messageDelay() {
	time_t t1 = time(NULL);

	if (message_queue.size() > 0 && t1 >= delay_time) {
		std::string message = message_queue.front();
		int clientID = message[0] - '0';
		int index = 0;

		if (message.find("COMMAND") != std::string::npos) {
			index = message.find("COMMAND");
			InterpretCommand(clientID, message.substr(index));
		}
		else if (message.find("NewPlayer") != std::string::npos){
			index = message.find("NewPlayer");
			InterpretCommand(clientID, message.substr(index));
		}

		time_t t2 = time(NULL);

		stringstream ss;

		ss << "t0:" << message.substr(4, message.length() - (message.length() - index + 2)) << ";t1:" << t1 << ";t2:" << t2;
		server.wsSend(clientID, ss.str());


		delay_time = time(NULL) + (rand() % 10);
		message_queue.pop();
	}
		

}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:31,代码来源:main.cpp


示例13: messageHandler

/* called when a client sends a message to the server */
void messageHandler(int clientID, string message) {
	vector<int> clientIDs = server.getClientIDs();

	stringstream ss;
	ss << clientID << ":" << message;
	if(clientID == 0)
		message_queue[0].push(ss.str());
	else
		message_queue[1].push(ss.str());
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:11,代码来源:main.cpp


示例14: messageHandler

/* called when a client sends a message to the server */
void messageHandler(int clientID, string message){
	bool scored = false;
	bool named = false;

    ostringstream os;
	ostringstream os2;

	if (message.find("Player1:")==0) {
		if(message.length()>8)
			player1 = message.substr(8);
		named = true;
	}
	if (message.find("Player2:")==0) {
		if (message.length()>8)
			player2 = message.substr(8);
		named = true;
	}


	if(!named){
		if (message == "1: addScore") {
			score1++;
			scored = true;
		}
		else if (message == "2: addScore") {
			score2++;
			scored = true;
		}

		if(scored){
			
			os << ("1:" + player1 + " Score: ")<<score1;
			os2 << ("2:" + player2 + " Score: " )<< score2;
		}
		vector<int> clientIDs = server.getClientIDs();
		for (int i = 0; i < clientIDs.size(); i++) {
			server.wsSend(clientIDs[i], os.str());
			server.wsSend(clientIDs[i], os2.str());
		}
	}

}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:43,代码来源:realMain.cpp


示例15: closeHandler

/* called when a client disconnects */
void closeHandler(int clientID) {
	vector<int> clientIDs = server.getClientIDs();

	if (clientIDs.size() <= 2)
		gameStarted = false;

	ostringstream os;
	std::string player_name;
	if (clientID == 0)
		player_name = player1;
	else
		player_name = player2;
	os << player_name << " has left.";


	for (int i = 0; i < clientIDs.size(); i++) {
		if (clientIDs[i] != clientID)
			server.wsSend(clientIDs[i], os.str());
	}
}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:21,代码来源:main.cpp


示例16: openHandler

/* called when a client connects */
void openHandler(int clientID) {
	vector<int> clientIDs = server.getClientIDs();

	server.wsSend(clientID, "Welcome!");
	
	ostringstream game_width;
	ostringstream game_height;
	ostringstream game_board;

	game_width << "GW:" << snakeState.GetBoardWidth();
	game_height << "GH:" << snakeState.GetBoardHeight();
	game_board << "GB:" << snakeState.GetBoardState();

	server.wsSend(clientID, game_width.str());
	server.wsSend(clientID, game_height.str());
	server.wsSend(clientID, game_board.str());
	

	if (clientIDs.size() == 2) {
		gameStarted = true;
		snakeState.StartNewGame();
		return;
	}
	else if (clientIDs.size() > 2)
		server.wsClose(clientID);
	else
		gameStarted = false;

}
开发者ID:jellesse92,项目名称:MileStone1,代码行数:30,代码来源:main.cpp


示例17: periodicHandler

/* called once per select() loop */
void periodicHandler(){
	if (clientSnakes.size() == 2){
		std::chrono::steady_clock::time_point other = std::chrono::steady_clock::now();
		if (!ReceiveQueue.empty()){
			//use iterators so that we can edit queue during iteration.
			for (auto msg = ReceiveQueue.begin(); msg != ReceiveQueue.end();){
				for (auto vect = msg->second.begin(); vect != msg->second.end();){
					if (std::chrono::duration_cast<std::chrono::milliseconds>(other - msg->first).count() > vect->latencyVal){
						messageHandler(vect->clientID, vect->message);
						//removes message from queue
						vect = msg->second.erase(vect);
					}
					else{
						++vect;
					}
				}
				if (msg->second.empty()){
					msg = ReceiveQueue.erase(msg);
				}
				else{
					++msg;
				}
			}
		}
		std::chrono::steady_clock::time_point other1 = std::chrono::steady_clock::now();
		if (!SendQueue.empty()){
			for (auto key = SendQueue.begin(); key != SendQueue.end();){
				for (auto vec = key->second.begin(); vec != key->second.end();){
					if (std::chrono::duration_cast<std::chrono::milliseconds>(other1 - key->first).count() > vec->latencyVal){
						if (vec->message == "latency"){
							vec->message = "latency;" + std::to_string(serverDelayStart);
						}
						server.wsSend(vec->clientID, vec->message);
						vec = key->second.erase(vec);
					}
					else{
						++vec;
					}
				}
				if (key->second.empty()){
					key = SendQueue.erase(key);
				}
				else{
					++key;
				}
			}
		}

	}

}
开发者ID:turtleaki,项目名称:Two-Player-Snake,代码行数:52,代码来源:main.cpp


示例18: closeHandler

void closeHandler(int clientID){
    
    // If game is ongoing, kill it and send out
    // an error to whomever is still connected:
    if (game_p != NULL && game_p->isActive()) {
        json errorMsg;
        errorMsg["MESSAGE_TYPE"] = "ERROR";
        errorMsg["ERROR_MSG"] = "Other player disconnected";
        
        // Send the message to whomever is connected
        vector<int> clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsSend(clientIDs[i], errorMsg.dump()); // Don't buffer
        }
        
        // Close all open connections (must be done separately)
        clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsClose(i);
        }
        resetGame();
    }
}
开发者ID:jasaaved,项目名称:Resume,代码行数:23,代码来源:main.cpp


示例19: check_winner

//Checks for winners
std::string check_winner(){
	vector<int> clientIDs = server.getClientIDs();
	std::string winner;
	if (clientSnakes[clientIDs[0]].score > clientSnakes[clientIDs[1]].score){
		winner = "Winner: " + clientSnakes[clientIDs[0]].ID;
	}
	else if (clientSnakes[clientIDs[0]].score < clientSnakes[clientIDs[1]].score){
		winner = "Winner: " + clientSnakes[clientIDs[1]].ID;
	}
	else if (clientSnakes[clientIDs[0]].score == clientSnakes[clientIDs[1]].score){
		winner = "It's a tie";
	}
	return winner;

}
开发者ID:turtleaki,项目名称:Two-Player-Snake,代码行数:16,代码来源:main.cpp


示例20: openHandler

/* called when a client connects */
void openHandler(int clientID){
	ostringstream os;
	vector<int> clientIDs = server.getClientIDs();

	std::cout << "in openhandler " << clientIDs.size() << std::endl;
	if (clientIDs.size() > 3){
		server.wsClose(clientID);
		cout << "Connection rejected: Only two connection allowed at a time.";
	}
	else if (clientIDs.size() == 2)
	{
		std::cout << "size is 2" << std::endl;
		os << "Stranger " << clientID << " has joined.";

		for (int i = 0; i < clientIDs.size(); i++){
			clientSnakes[clientIDs[i]].ID = clientID;
			server.wsSend(clientIDs[i], "Welcome!");
			//SendQueue[std::chrono::steady_clock::now()].push_back(createMessage(clientIDs[i], "Welcome!"));
		}

		gameStartState();

	}
}
开发者ID:turtleaki,项目名称:Two-Player-Snake,代码行数:25,代码来源:main.cpp



注:本文中的webSocket类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ weibull_distribution类代码示例发布时间:2022-05-31
下一篇:
C++ weak_ptr类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap