Sunday, March 27, 2016

Websocket with PHP


There are many ways to create real time web application with PHP, but in this
case I will show you a best websocket library will be useful for making a real
time web application.

What is Websockt?

It is full-duplex (data can transmitted in both directions) clinet/Server commnuication over TCP.
so you will find out that websocket is the new feature of HTML5 that allow Client script to open
the bi-directional socket connections to a server. Now it work on Chrome, Firefox, Safari, Opera
and other browser even on mobile.

What is PHPWebsockets?

It is websocket libray server which written in PHP. You can
download from there : https://github.com/ghedipunk/PHP-WebSockets

testwebsock.php
#!/usr/bin/env php
send($user,$message);
  }
  
  protected function connected ($user) {
    // Do nothing: This is just an echo server, there's no need to track the user.
    // However, if we did care about the users, we would probably have a cookie to
    // parse at this step, would be looking them up in permanent storage, etc.
  }
  
  protected function closed ($user) {
    // Do nothing: This is where cleanup would go, in case the user had any sort of
    // open files or other objects associated with them.  This runs after the socket 
    // has been closed, so there is no need to clean up the socket itself here.
  }
}
$echo = new echoServer("0.0.0.0","9000");
try {
  $echo->run();
}
catch (Exception $e) {
  $echo->stdout($e->getMessage());
}


client.html

Please run the client.html and don't forget to lunch the testwebsocket.php via common line.

0 comments:

Post a Comment