本文整理汇总了C++中webSocket::wsSend方法的典型用法代码示例。如果您正苦于以下问题:C++ webSocket::wsSend方法的具体用法?C++ webSocket::wsSend怎么用?C++ webSocket::wsSend使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webSocket
的用法示例。
在下文中一共展示了webSocket::wsSend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
}
}
示例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;
}
示例3: 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!");
}
示例4: 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);
}
}
示例5: openHandler
/* called when a client connects */
void openHandler(int clientID){
score1 = 0;
score2 = 0;
server.wsSend(clientID, "Welcome!");
}
示例6: 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();
}
}
示例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++){
if (clientIDs[i] != clientID)
server.wsSend(clientIDs[i], os.str());
}
}
示例8: 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());
}
}
示例9: 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());
}
}
}
示例10: 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();
}
示例11: 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;
}
}
}
}
}
示例12: 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(¤t);
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;
}
}
示例13: 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());
}
}
示例14: 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();
}
}
示例15: 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();
}
}