5.5 strict warnings, change background color
[php-js-websockets.git] / index.php
blob:a/index.php -> blob:b/index.php
--- a/index.php
+++ b/index.php
@@ -1,126 +1,152 @@
-<!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>

+<?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>​

comments