notices, changebackground message
notices, changebackground message

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -18,8 +18,7 @@
 
       function connect(){
           var socket;
-          var host = "ws://192.168.0.2:10001/";
-          console.log (host);
+          var host = "ws://"+window.location.host+":10001/";
 
           try{
               var socket = new WebSocket(host);
@@ -81,7 +80,7 @@
               } catch(exception){
                  message('<p class="warning">');
               }
-              $('#text').val("");
+              //$('#text').val("");
           }
 
           function message(msg){
@@ -142,8 +141,8 @@
         <div id="chatLog">
         </div>
         <p id="examples">e.g. try 'ping'</p>
-        <input id="text" type="text" />
-        <button id="disconnect">Disconnect</button>
+        <input id="text" type="text" value="chat|changebackground$red" />
+        <!--button id="disconnect">Disconnect</button-->
 </div>
 <?php $url = urlencode ("http://".$_SERVER['HTTP_HOST']."/?code=".session_id ()); ?>
 <img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?php echo $url; ?>" />

--- a/server.functions.php
+++ b/server.functions.php
@@ -1,9 +1,8 @@
 <?php
 
-function sendMesageToYou ($socket, $msg, $handshake=false) {
-	$msg = $msg;
-	echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($socket)." ".$msg.")\n";
+function sendMesage ($socket, $msg, $handshake=false) {
 	if ( $handshake == false ) {
+		echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($socket)." ".$msg."\n";
 		@socket_write ($socket, encode($msg));
 	} else {
 		@socket_write ($socket, $msg, strlen($msg));
@@ -14,48 +13,10 @@
 	global $_sockets;
 	foreach ($allclients as $client) {
 		if ( $_sockets[intval ($client)]['session'] == $session && intval ($client) != intval ($socket) ) {
-			echo "[".date('Y-m-d H:i:s')."] MESSAGE to ".intval($client)." ".$msg.")\n";
-			@socket_write ($client, encode ($msg));
+			sendMesage ($client, $msg);
 		}
 	}
 }
-
-function send_Msg($allclients,$socket,$id,$msg) {
-	global $_sockets;
-
-	if (!empty($_sockets[intval($socket)])) {
-		$nicks = array(); //amig fut a parancs ebben vannak a nickek.
-
-		foreach ($_sockets as $_socket){
-			foreach ($_socket as $key=>$val){
-				// this check's the onliners
-				if (empty($nicks[$val])) $nicks[$val]=1;
-				else $nicks[$val]=$nicks[$val]+1; // we shouldn't have duplicated nicks, but what if...
-			}
-		}
-
-		foreach($allclients as $client) {
-			if (!empty($_sockets[$client]['nick']) && ($_sockets[$client]['nick'] == $id)) {
-				$_client = $client;
-				$out = "<msg aid=\"".$_sockets[$socket]['nick']."\" time=\"".date("H:i:s")."\" msg=\"".$msg."\" from=\"".$_sockets[$client]['nick']."\" />";
-			}
-			elseif(empty($nicks[$id])) {
-				//backto the sender
-				$_client = $socket;
-				$out = "<error value=\"User is already left.\"/>";
-			}
-		}
-	} else {
-		//backto the sender
-		$_client = $socket;
-		$out = "<error value=\"Not identified.\"/>";
-	}
-	if (!empty($out)) {
-		socket_write($socket, $out.chr(0)); //send to back ourself. we have to handle it in flash
-		socket_write($_client, $out.chr(0)); //send to the recipient
-	}
-}
-
 
 function dohandshake($socket, $buffer) {
 	// Determine which version of the WebSocket protocol the client is using
@@ -81,8 +42,8 @@
 	                    "Connection: Upgrade\r\n" .
 	                    "Sec-WebSocket-Accept: $accept_key\r\n\r\n";
 
-		echo "[".date('Y-m-d H:i:s')."] Sending handshake)\n";
-		sendMesageToYou ($socket, $upgrade, true);
+		echo "[".date('Y-m-d H:i:s')."] Sending handshake to ".intval ($socket)."\n";
+		sendMesage ($socket, $upgrade, true);
 	    return true;
 	}
 	else {

file:a/server.php -> file:b/server.php
--- a/server.php
+++ b/server.php
@@ -64,25 +64,22 @@
 				@socket_shutdown ($socket, 2);
 				@socket_close ($socket);
 			} else {
-				$session = $_sockets[intval($socket)]['session'];
+				$session = isset ($_sockets[intval($socket)]['session']) ? $_sockets[intval($socket)]['session'] : null;
 				$allclients = $read_sockets;
 				array_shift ($allclients);
 
-				if ( !$_sockets[intval($socket)]['handshake'] ) {
+				if ( !isset($_sockets[intval($socket)]['handshake']) ) {
 					$_sockets[intval($socket)]['handshake'] = true;
 					dohandshake($socket, $buffer);
 				}
 
 				$buffer = unmask($buffer);
-				//echo "[".date('Y-m-d H:i:s')."] BUFFER ".$buffer."\n";
 				$piece = explode("|",trim($buffer)); // we strip out all unwanted data
 				$cmd = strtolower ($piece[0]);
 
 				if ( !empty($piece[1]) ) {
 					$txt = $piece[1];
 				}
-
-				echo "[".date('Y-m-d H:i:s')."] Received ".$cmd." ".$txt."\n";
 
 				switch ($cmd) {
 					case "chat": // chat|session:message
@@ -91,11 +88,11 @@
 							//$session = $txt[0];
 							$message = $txt[0];
 							//if ( $_sockets[intval($socket)]['session'] == $session ) {
-								//sendMesageToYou ($socket, $message);
+								//sendMesage ($socket, $message);
 								sendMessageToAll ($allclients, $socket, $session, $message);
 							//}
 						} else {
-							sendMesageToYou ($socket, "wrong session");
+							sendMesage ($socket, "wrong session");
 						}
 					break;
 
@@ -104,11 +101,11 @@
 						$session = $txt[0];
 
 						$_sockets[intval($socket)]['session'] = $session;
-						sendMesageToYou ($socket, "true");
+						sendMesage ($socket, "true");
 					break;
 
 					case "ping":
-						sendMesageToYou ($socket, "PONG");
+						sendMesage ($socket, "PONG");
 					break;
 				}
 			}

comments