5.5 strict warnings, change background color
5.5 strict warnings, change background color

file:a/index.html (deleted)
--- a/index.html
+++ /dev/null
@@ -1,126 +1,1 @@
-<!DOCTYPE html>

-<html>

-<head>

-<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

-

-<script>

-$(document).ready(function() {

-

-  if(!("WebSocket" in window)){

-  $('#chatLog, input, button, #examples').fadeOut("fast");

-  $('<p>Oh no, you need a browser that supports WebSockets. How about <a href="http://www.google.com/chrome">Google Chrome</a>?</p>').appendTo('#container');

-  } else {

-      connect();

-

-      function connect(){

-          var socket;

-          var host = "ws://192.168.0.2:10001/";

-

-          try{

-              var socket = new WebSocket(host);

-

-              message('<p class="event">Socket Status: '+socket.readyState);

-

-              socket.onopen = function(){

-                 message('<p class="event">Socket Status: '+socket.readyState+' (open)');

-              }

-

-              socket.onmessage = function(msg){

-                 message('<p class="message">Received: '+msg.data);

-              }

-

-              socket.onclose = function(){

-                message('<p class="event">Socket Status: '+socket.readyState+' (Closed)');

-              }

-

-          } catch(exception){

-             message('<p>Error'+exception);

-          }

-

-          function send(){

-              var text = $('#text').val();

-

-              if(text==""){

-                  message('<p class="warning">Please enter a message');

-                  return ;

-              }

-              try{

-                  socket.send(text);

-                  message('<p class="event">Sent: '+text)

-

-              } catch(exception){

-                 message('<p class="warning">');

-              }

-              $('#text').val("");

-          }

-

-          function message(msg){

-            $('#chatLog').append(msg+'</p>');

-          }

-

-          $('#text').keypress(function(event) {

-              if (event.keyCode == '13') {

-                send();

-              }

-          });

-

-          $('#disconnect').click(function(){

-             socket.close();

-          });

-

-      }//End connect

-

-  }//End else

-

-});

-</script>

-

-<style type="text/css">

-body {

-    font-family:Arial, Helvetica, sans-serif;

-}

-#container{

-    border:5px solid grey;

-    width:800px;

-    margin:0 auto;

-    padding:10px;

-}

-#chatLog{

-    padding:5px;

-    border:1px solid black;

-}

-#chatLog p {

-    margin:0;

-}

-.event {

-    color:#999;

-}

-.warning{

-    font-weight:bold;

-    color:#CCC;

-}

-</style>

-

-<title>WebSockets Client</title>

-

-</head>

-<body>

-<div id="wrapper">

-

-    <div id="container">

-

-        <h1>WebSockets Client</h1>

-

-        <div id="chatLog">

-

-        </div>

-        <p id="examples">e.g. try 'ping'</p>

-

-        <input id="text" type="text" />

-        <button id="disconnect">Disconnect</button>

-

-    </div>

-

-</div>

-</body>

-</html>​
+

file:b/index.php (new)
--- /dev/null
+++ b/index.php
@@ -1,1 +1,152 @@
+<?php
+session_start ();
+if ( $_GET['code'] ) {
+  session_id ($_GET['code']);
+}
+?>
+<html>
+<head>
+<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 
+<script>
+$(document).ready(function() {
+
+  if(!("WebSocket" in window)){
+    $('#chatLog, input, button, #examples').fadeOut("fast");
+    $('<p>Oh no, you need a browser that supports WebSockets. How about <a href="http://www.google.com/chrome">Google Chrome</a>?</p>').appendTo('#container');
+  } else {
+
+      function connect(){
+          var socket;
+          var host = "ws://192.168.0.2:10001/";
+          console.log (host);
+
+          try{
+              var socket = new WebSocket(host);
+
+              message('<p class="event">Socket Status: '+socket.readyState);
+
+              socket.onopen = function(){
+                 message('<p class="event">Socket Status: '+socket.readyState+' (open)');
+                 sendmessage ('login|<?php echo session_id (); ?>');
+              }
+
+              socket.onmessage = function(msg){
+                 message('<p class="message">Received: '+msg.data);
+                 var msgdata = msg.data;
+                 var _message = msgdata.split ('$');
+                 var cmd = _message[0];
+                 var msg = _message[1];
+                 console.log ('CMD: '+cmd);
+                 console.log ('MSG: '+msg);
+                 if ( cmd == "changebackground" ) {
+                    $("body").css("background-color", msg );
+                 }
+              }
+
+              socket.onclose = function(){
+                message('<p class="event">Socket Status: '+socket.readyState+' (Closed)');
+              }
+
+          } catch(exception){
+            console.log (exception);
+             message('<p>Error'+exception);
+          }
+
+          function sendmessage(text){
+              if(text==""){
+                  message('<p class="warning">Please enter a message');
+                  return ;
+              }
+              try{
+                  socket.send(text);
+                  message('<p class="event">Sent: '+text)
+
+              } catch(exception){
+                 message('<p class="warning">');
+              }
+          }
+
+          function send(){
+              var text = $('#text').val();
+
+              if(text==""){
+                  message('<p class="warning">Please enter a message');
+                  return ;
+              }
+              try{
+                  socket.send(text);
+                  message('<p class="event">Sent: '+text)
+
+              } catch(exception){
+                 message('<p class="warning">');
+              }
+              $('#text').val("");
+          }
+
+          function message(msg){
+            $('#chatLog').append(msg+'</p>');
+          }
+
+          $('#text').keypress(function(event) {
+              if (event.keyCode == '13') {
+                send();
+              }
+          });
+
+          $('#disconnect').click(function(){
+             socket.close();
+          });
+
+      }//End connect
+      connect();
+
+  }//End else
+
+});
+</script>
+
+<style type="text/css">
+body {
+    font-family:Arial, Helvetica, sans-serif;
+}
+#container{
+    border:5px solid grey;
+    width:800px;
+    margin:0 auto;
+    padding:10px;
+}
+#chatLog{
+    padding:5px;
+    border:1px solid black;
+}
+#chatLog p {
+    margin:0;
+}
+.event {
+    color:#999;
+}
+.warning{
+    font-weight:bold;
+    color:#CCC;
+}
+</style>
+
+<title>WebSockets Client</title>
+
+</head>
+<body>
+<div id="wrapper">
+    <div id="container">
+        <h1>WebSockets Client</h1>
+        <div id="chatLog">
+        </div>
+        <p id="examples">e.g. try 'ping'</p>
+        <input id="text" type="text" />
+        <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; ?>" />
+</div>
+</body>
+</html>​

--- a/server.functions.php
+++ b/server.functions.php
@@ -10,10 +10,10 @@
 	}
 }
 
-function sendMessageToAll ($allclients, $session, $msg) {
+function sendMessageToAll ($allclients, $socket, $session, $msg) {
 	global $_sockets;
 	foreach ($allclients as $client) {
-		if ( $_sockets[$client]['session'] == $session ) {
+		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));
 		}

file:a/server.php -> file:b/server.php
--- a/server.php
+++ b/server.php
@@ -37,7 +37,7 @@
 
 while (true) {
 	$changed_sockets = $read_sockets;
-	$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
+	$num_changed_sockets = socket_select($changed_sockets, $write, $except, NULL);
 
 	foreach($changed_sockets as $socket) {
 	    if ($socket == $master) {
@@ -64,6 +64,7 @@
 				@socket_shutdown ($socket, 2);
 				@socket_close ($socket);
 			} else {
+				$session = $_sockets[intval($socket)]['session'];
 				$allclients = $read_sockets;
 				array_shift ($allclients);
 
@@ -85,13 +86,17 @@
 
 				switch ($cmd) {
 					case "chat": // chat|session:message
-						$txt = explode (":", $txt);
-						$session = $txt[0];
-						$message = $txt[1];
-						//if ( $_sockets[intval($socket)]['session'] == $session ) {
-							//sendMesageToYou ($socket, $message);
-							sendMessageToAll ($allclients, $session, $message);
-						//}
+						if ( $session ) {
+							$txt = explode (":", $txt);
+							//$session = $txt[0];
+							$message = $txt[0];
+							//if ( $_sockets[intval($socket)]['session'] == $session ) {
+								//sendMesageToYou ($socket, $message);
+								sendMessageToAll ($allclients, $socket, $session, $message);
+							//}
+						} else {
+							sendMesageToYou ($socket, "wrong session");
+						}
 					break;
 
 					case "login"; // login|session
@@ -99,7 +104,7 @@
 						$session = $txt[0];
 
 						$_sockets[intval($socket)]['session'] = $session;
-						sendMesageToYou ($socket, "true aaa bbb");
+						sendMesageToYou ($socket, "true");
 					break;
 
 					case "ping":

comments