本文整理汇总了C++中webSocket类的典型用法代码示例。如果您正苦于以下问题:C++ webSocket类的具体用法?C++ webSocket怎么用?C++ webSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了webSocket类的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
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);
}
}
示例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]); }
}
}
}
示例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());
}
}
示例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());
}
}
示例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!");
}
示例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();
}
示例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);
}
}
示例9: openHandler
/* called when a client connects */
void openHandler(int clientID){
score1 = 0;
score2 = 0;
server.wsSend(clientID, "Welcome!");
}
示例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(¤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;
}
}
示例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;
}
示例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();
}
}
示例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());
}
示例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());
}
}
}
示例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());
}
}