当前位置: 首页>>代码示例>>C++>>正文


C++ webSocket::wsClose方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: 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;
	ostringstream ssMove;
	ostringstream playerID;

	game_width << "GW:" << snakeState.GetBoardWidth();
	game_height << "GH:" << snakeState.GetBoardHeight();
	game_board << "GB:" << snakeState.GetBoardState();
	ssMove << "MOVE:" << snakeState.GetPlayerDirection(0) << snakeState.GetPlayerDirection(1);
	playerID << "ID:" << clientID;


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

	if (clientIDs.size() == 2) {
		gameStarted = true;
		message_to_process[0] = "";
		message_to_process[1] = "";
		emptyQueue();
		last_move[0] = 'D';
		last_move[1] = 'A';
		snakeState.StartNewGame();
		return;
	}
	else if (clientIDs.size() > 2)
		server.wsClose(clientID);
	else
		gameStarted = false;

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

示例5: 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

示例6: 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

示例7: periodicHandler

void periodicHandler(){
    
    // Poll the incoming & outgoing message buffers.
    std::pair <int, std::string> message_pair;
    if (send_buffer.isMessageReady()) {
        message_pair = send_buffer.getMessage();
        if (message_pair.first != -1) {
			json time_check = json::parse(message_pair.second);
			if (time_check["MESSAGE_TYPE"] == "TIME_STAMP_REPLY") {
				time_check["T3"] = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1);
				message_pair.second = time_check.dump();
			}
            server.wsSend(message_pair.first, message_pair.second);
        }
    }
    if (receive_buffer.isMessageReady()) {
        message_pair = receive_buffer.getMessage();
        if (message_pair.first != -1) {
            read_message(message_pair.first, message_pair.second);
        }
    }
    
    // If the game is active, update the game state.
    // We want this to occur no sooner than UPDATE_CYCLE_LENGTH_MS.
    // WARNING: This sets the pace of the game, so Milestone 4 client features that
    // perform movement prediction MUST use the same clock speed.
    if (game_p != NULL && game_p->isActive()) {
        unsigned long long currentTime = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1);
        if (currentTime - lastUpdateTime >= UPDATE_CYCLE_LENGTH_MS) {
            // Update the game
            json msg = game_p->update();
            
            // Broadcast the update to all clients
            vector<int> clientIDs = server.getClientIDs();
            for (int i = 0; i < clientIDs.size(); i++) {
                send_message(clientIDs[i], msg);
            }
            
            // Update the clock
            lastUpdateTime = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1);
        }
    }
    
    // If there is a game object but the status is INACTIVE,
    // update the clients and then DESTROY the game object.
    if (game_p != NULL && !game_p->isActive()) {

        json msg = game_p->update();
        resetGame();
        
        // Broadcast the final update to all clients
        // and then disconnect clients
        cout << "GAME OVER BROADCASTING FINAL UPDATE" << endl;
        vector<int> clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsSend(clientIDs[i], msg.dump()); // Don't buffer
        }
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsClose(i);
        }
    }
}
开发者ID:jasaaved,项目名称:Resume,代码行数:62,代码来源:main.cpp


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