title: Socket Programming Cross Platform (PHP & Javascript)
body: <p>I have created a socket server using PHP, the code is like this</p>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>function listen()
  {
  // set some variables
      $host = "127.0.0.1";
      $port = 25003;
  // don't timeout!
      set_time_limit(0);
  // create socket
      $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  // bind socket to port
      $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
  // start listening for connections
      $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
  
  // accept incoming connections
  // spawn another socket to handle communication
      $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
  // read client input
      $input = socket_read($spawn, 1024) or die("Could not read input\n");
  // clean up input string
      $input = trim($input);
      echo "Client Message : " . $input."\n";
  // reverse client input and send back
      $output = strrev($input) . "\n";
      socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");
  // close sockets
      socket_close($spawn);
      socket_close($socket);
      listen();
  }
  
  set_time_limit(0);
  listen();
  </code></pre>
  
  <p>Taken from <a href="http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP" rel="nofollow noreferrer">http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP</a></p>
  
  <p>And I create the client socket using Javascript, like this</p>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript"&gt;
          function WebSocketTest()
          {
              if ("WebSocket" in window)
              {
                  alert("WebSocket is supported by your Browser!");
  
                  // Let us open a web socket
                  var ws = new WebSocket("ws://localhost:25003");
  
                  ws.onopen = function()
                  {
                      // Web Socket is connected, send data using send()
                      ws.send("Message to send");
                      alert("Message is sent...");
                  };
  
                  ws.onmessage = function (evt)
                  {
                      var received_msg = evt.data;
                      alert("Message is received...");
                  };
  
                  ws.onclose = function()
                  {
                      // websocket is closed.
                      alert("Connection is closed...");
                  };
              }
  
              else
              {
                  // The browser doesn't support WebSocket
                  alert("WebSocket NOT supported by your Browser!");
              }
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div id="sse"&gt;
      &lt;a href="javascript:WebSocketTest()"&gt;Run WebSocket&lt;/a&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Then I run the PHP server, it's work, it's listening. 
  And the I run the Javascript client, it's work, the PHP Server is receiving request from Javascript.</p>
  
  <p>BUt the response from PHP is like this</p>
  
  <p><a href="https://i.stack.imgur.com/FRPdR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FRPdR.png" alt="enter image description here"></a></p>
  
  <p>I don't understand how to change the client message, let's say I want to send message just like "Hello", 
  1. How can I call it from javascript so the PHP server is receive only "Hello" ?
  2. Anyone can explain to me what is the function of each url like this ?</p>
  
  <pre><code>ws://localhost:25003/daemon.php
  </code></pre>
  
  <p>what is the second parameter? is this connect to my php file? because I try to locate to my php it doesn't work.</p>