Welcome Guest, Not a member yet? Register   Sign In
WebSockets with codeigniter
#1

[eluser]kaos78414[/eluser]
So I've been reading about websockets and I'm very excited for the possibilities and for what lies ahead. However, I've been reading some tutorials and I'm not sure what the best way to implement websockets is (I would like to have this done in CodeIgniter)

For instance, let's say I want to have a chat bar similar to that of Facebook, and I want to use WebSockets to do it.

How would you best accomplish this?

Here's a link to a sample usage of WebSockets is PHP: http://code.google.com/p/phpwebsocket/so...server.php

Now, I'd like to know where code like this is best placed. Obviously we'd need to first run the connection, which could happen in a Controller function or as part of one, and then consistently check if the connection is still open possibly in a controller constructor before allowing the sending and/or receiving of data.

I'm just experimenting with it for now so I'm not too worried about security. But would it be too much overhead to use the form_validation class to validate websockets input?

Tell me how you would implement maybe a chat application in CodeIgniter using websockets. Any thoughts are appreciated! Thanks! Big Grin
#2

[eluser]jaykaneda[/eluser]
I've also been looking into integrating WebSockets into a CI project. I too found the phpwebsocket script and was trying to figure out how it could be integrated into CI. Just wondering if any progress was made here?
#3

[eluser]Nicholas Bello[/eluser]
As an aside - I found a chat bar similar to facebook a while back but sadly it doesn't look like there has been any progress on it. Ajax IM

Sockets are very exciting (and I still need to get a better grasp on them) but I'm not sure a bar like you want is best done by sockets in PHP, I would think that's a task better left to javascript.
#4

[eluser]jaykaneda[/eluser]
I did manage to implement phpwebsocket as a CI controller, and got the example working, but it all seemed a bit fragile.

WebSockets is definitively exciting, but I think I'll be going for an ajax type solution for my project.
#5

[eluser]Vheissu[/eluser]
[quote author="jaykaneda" date="1312335750"]I did manage to implement phpwebsocket as a CI controller, and got the example working, but it all seemed a bit fragile.

WebSockets is definitively exciting, but I think I'll be going for an ajax type solution for my project.[/quote]

Any example code of how you achieved such a thing with Websockets you can share with us?
#6

[eluser]OliverHR[/eluser]
A good and short example at:

http://mylittlehacks.appspot.com/phpwebsocket
#7

[eluser]jaykaneda[/eluser]
[quote author="Vheissu" date="1313486305"][quote author="jaykaneda" date="1312335750"]I did manage to implement phpwebsocket as a CI controller, and got the example working, but it all seemed a bit fragile.

WebSockets is definitively exciting, but I think I'll be going for an ajax type solution for my project.[/quote]

Any example code of how you achieved such a thing with Websockets you can share with us?[/quote]

phpwebsocket contains websocket.class.php, and I just made this a CI controller

Code:
class WebSocket{
...
}

to...

Code:
class WebSocket extends CI_Controller{
...
}

It also contains a script server.php. I also implemented this as a controller

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Server extends CI_Controller {

    function __construct(){
        parent::__construct();
    }
    
    public function index(){
        error_reporting(E_ALL);
        set_time_limit(0);
        ob_implicit_flush();    
        
        $master = $this->WebSocket("0.0.0.0",12345);
        $sockets = array($master);
        $users   = array();
        $debug   = false;
        
        while(true){
          $changed = $sockets;
          socket_select($changed,$write=NULL,$except=NULL,NULL);
          foreach($changed as $socket){
            if($socket==$master){
              $client=socket_accept($master);
              if($client<0){ console("socket_accept() failed"); continue; }
              else{ $this->connect($client); }//
            }
            else{
              $bytes = @socket_recv($socket,$buffer,2048,0);
              if($bytes==0){ $this->disconnect($socket); }//
              else{
                $user = $this->getuserbysocket($socket);//
                if(!$user->handshake){ $this->dohandshake($user,$buffer); }//
                else{ $this->process($user,$buffer); }//
              }
            }
          }
        }
    }
    
    function process($user,$msg){
        ...
        }

        ...

}

Then I just put the client.html in my views directory, as client.php. And finally a client contrroller which points to it

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client extends CI_Controller {

    function __construct(){
        parent::__construct();
    }
    
    public function index(){    
        
        $this->load->view('client');
    }
}

I changed a couple of lines of code, as outlined here, before I could get the example to work, using the steps on the project home page, and that's about it.

http://code.google.com/p/phpwebsocket/is...tail?id=33

I didn't take it any further than this, let us know how you get on.
#8

[eluser]Unknown[/eluser]
Hello,
I have two questions:
1.-How do you start the server? Is it enough writing the url in the browser to start? For example: http://localhost/my-project/index.php/server

2.-I can't connect the client with the server. Firefox says "Can't establish a connection to the server at ws://localhost:12345/my-project/index.php/server

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB