function wsOnMessage($clientID, $message, $messageLength, $binary) { global $users; $message = explode(' ', $message); $command = array_shift($message); if ($command == 'J') { if (isUser($clientID)) { wsClose($clientID); return; } $name = trim($message[0]); if ($name == '') { wsClose($clientID); return; } if (nameTaken($name)) { wsClose($clientID); return; } addUser($clientID, $name); } else { if ($messageLength == 0 || !isUser($clientID)) { wsClose($clientID); return; } else { if ($command == 'D') { $c = $users[$clientID]; // Update client parameters $c->ang = $message[0]; $c->pos[0] = $message[1]; $c->pos[1] = $message[2]; $c->vel[0] = $message[3]; $c->vel[1] = $message[4]; // Let other clients know that new data is available foreach ($users as $userID => $data) { if ($userID != $clientID) { $users[$userID]->queue[$clientID] = 1; } } $data = ''; // Send updates back to the client foreach ($c->queue as $userID => $one) { if ($one) { $u = $users[$userID]; $data .= ' ' . $u->name . ' ' . $u->ang . ' ' . $u->pos[0] . ' ' . $u->pos[1] . ' ' . $u->vel[0] . ' ' . $u->vel[1]; $c->queue[$userID] = 0; } } wsSend($clientID, 'D' . $data); } elseif ($command == 'Q') { removeUser($clientID); } else { wsClose($clientID); } } } }
function wsOnMessage($clientID, $message, $messageLength, $binary) { // check if message length is 0 if ($messageLength == 0) { wsClose($clientID); return; } // split the message by spaces into an array, and fetch the command $message = explode(' ', $message); $command = array_shift($message); // check which command was received if ($command == 'TEXT') { // a client has sent chat text to the server if (!isUser($clientID)) { // the client has not yet sent a JOIN with a valid username, and is trying to send a TEXT wsClose($clientID); return; } // put the message back into a string $text = implode(' ', $message); if ($text == '') { // the text is blank wsSend($clientID, 'SERVER Message was blank.'); return; } // fetch the client's username, and send the chat text to all clients // the text is actually also sent back to the client which sent the text, which sort of acts as a confirmation that the text worked $username = getUsername($clientID); sendChat($username, $text); } elseif ($command == 'JOIN') { // a client is joining the chat if (isUser($clientID)) { // the client has already sent a JOIN with a valid username wsClose($clientID); return; } // fetch username, and trim any whitespace before and after the username $username = trim($message[0]); if ($username == '') { // the username is blank wsClose($clientID); return; } if (strlen($username) > CB_MAX_USERNAME_LENGTH) { // username length is more than CB_MAX_USERNAME_LENGTH wsSend($clientID, 'SERVER Username length cannot be more than ' . CB_MAX_USERNAME_LENGTH . '.'); wsClose($clientID); return; } if (isUsername($username)) { // username is already being used by another client wsSend($clientID, 'SERVER Username already taken.'); wsClose($clientID); return; } // add the user addUser($clientID, $username); } elseif ($command == 'QUIT') { // a client is leaving the chat if (!isUser($clientID)) { // the client has not yet sent a JOIN with a valid username, and is trying to send a QUIT wsClose($clientID); return; } // remove the user removeUser($clientID); } else { // unknown command received, close connection wsClose($clientID); } }