@@ -18,263 +18,53 @@ Just by running following command in the folder of your project :
1818``` sh
1919composer require takielias/codeigniter-websocket
2020```
21- Or by adding following lines to your ` composer.json ` file :
22- ``` json
23- "require" : {
24- "takielias/codeigniter-websocket" : " ^1.0.1"
25- },
26- ```
2721Don't forget to include your autoload to CI config file :
2822``` php
2923$config['composer_autoload'] = FCPATH.'vendor/autoload.php';
3024```
31- ### :arrow_right : Step 2 : Create library config file in your project (Optional)
25+ ### :arrow_right : Step 2 : One command Installation
3226
33- You have to create in your CI config folder located in ` ./application/config/codeigniter_websocket.php ` or the library will take his own config file based on host ` 0.0.0.0:8282 `
27+ If you want one click installation just Execute the Command in the Project directory
3428
35- ``` php
36- <?php
37- defined('BASEPATH') OR exit('No direct script access allowed');
38-
39- /**
40- * Ratchet Websocket Library: config file
41- * @author Romain GALLIEN <
romaingallien [email protected] >
42- * @var array
43- */
44- $config['codeigniter_websocket'] = array(
45- 'host' => '0.0.0.0',
46- 'port' => 8282,
47- 'timer_enabled' => false,
48- 'timer_interval' => 1, //1 means 1 seconds
49- 'auth' => true,
50- 'debug' => true
51- );
29+ ** N.B:** It will make 2 new controllers Welcome.php and User.php
30+ ``` sh
31+ php vendor/takielias/codeigniter-websocket/install.php --app_path=application
5232```
33+ Here app_path defines your default Codeigniter Application directory Name
5334
35+ ![ one click installation] ( https://user-images.githubusercontent.com/38932580/57182660-74df9a80-6ec3-11e9-8b31-37f3fcbf4ccd.png )
5436
55- You have to create in your CI config folder located in ` ./application/config/jwt.php ` or the library will take his own config file based on host ` 0.0.0.0:8282 `
37+ ** WOW You made it !!! ** : heavy_check_mark :
5638
57- ``` php
58- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
59-
60- $config['jwt_key'] = 'your JWT Private Key';
39+ Open three pages of your project on following url with different IDs :
6140
62- /*Generated token will expire in 1 minute for sample code
63- * Increase this value as per requirement for production
64- */
65- $config['token_timeout'] = 1;
41+ ` http://localhost/your project directory/index.php/user/index/1 `
6642
67- /* End of file jwt.php */
68- /* Location: ./application/config/jwt.php */
69- ```
43+ ` http://localhost/your project directory/index.php/user/index/2 `
7044
71- ### : arrow_right : Step 3 : Loading the library
45+ : heavy_exclamation_mark : In this example, ** recipient_id ** is defined by ** user_id ** , as you can see, it's the ** auth callback ** who defines recipient ids.
7246
73- You can add the following lines direclty in your Controller file or your MY_Controller global file
47+ If you have something like that, everything is ok for you:
7448
75- ``` php
76- $this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
77- $this->load->library('Codeigniter_websocket');
78- $this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
79- ```
49+ ![ user_1] ( https://user-images.githubusercontent.com/38932580/57090224-21851500-6d28-11e9-9321-20d02e146d62.png )
8050
81- ### You'r almost done :heavy_check_mark :
8251
83- ## Examples of use
52+ ![ user_2 ] ( https://user-images.githubusercontent.com/38932580/57090269-44afc480-6d28-11e9-8ea1-30079a3a47e9.png )
8453
85- #### : arrow_right : Create your first App
54+ You can try typing and sending something in each page (see cmd for more logs).
8655
87- It's not very difficult, the library will do your job and much more !
88- - Edit your CI controller ` Welcome.php ` with the following lines (this will be our server)
56+ ![ cmd] ( https://user-images.githubusercontent.com/38932580/57090313-5abd8500-6d28-11e9-8644-8e0323a36a41.png )
8957
90- ``` php
91- class Welcome extends CI_Controller
92- {
93- public function index()
94- {
95- // Load package path
96- $this->load->add_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
97- $this->load->library('Codeigniter_websocket');
98- $this->load->remove_package_path(FCPATH . 'vendor/takielias/codeigniter-websocket');
9958
100- // Run server
101- $this->codeigniter_websocket->run();
102- }
103- }
104- ```
105- - Create CI controller ` User.php ` and add following lines
106- ``` php
107- class User extends CI_Controller
108- {
109- public function index($user_id = null)
110- {
111- // We load the CI welcome page with some lines of Javascript
112- $this->load->view('welcome_message', array('user_id' => $user_id));
113- }
114- }
115- ```
116- - Edit your CI view ` welcome_message.php ` with following lines (again :stuck_out_tongue_winking_eye : )
117- ``` php
118- <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
119- <!DOCTYPE html>
120- <html lang =" en" >
121- <head >
122- <meta charset =" utf-8" >
123- <title >Welcome to CodeIgniter</title >
124- <style type =" text/css" >
125- #container , code {
126- border : 1px solid #D0D0D0
127- }
128-
129- ::selection {
130- background-color : #E13300 ;
131- color : #fff
132- }
133-
134- ::-moz-selection {
135- background-color : #E13300 ;
136- color : #fff
137- }
138-
139- body {
140- background-color : #fff ;
141- margin : 40px ;
142- font : 13px /20px normal Helvetica , Arial , sans-serif ;
143- color : #4F5155
144- }
145-
146- a , h1 {
147- background-color : transparent ;
148- font-weight : 400
149- }
150-
151- a {
152- color : #039
153- }
154-
155- h1 {
156- color : #444 ;
157- border-bottom : 1px solid #D0D0D0 ;
158- font-size : 19px ;
159- margin : 0 0 14px ;
160- padding : 14px 15px 10px
161- }
162-
163- code {
164- font-family : Consolas, Monaco, Courier New, Courier , monospace ;
165- font-size : 12px ;
166- background-color : #f9f9f9 ;
167- color : #002166 ;
168- display : block ;
169- margin : 14px 0 ;
170- padding : 12px 10px
171- }
172-
173- #body {
174- margin : 0 15px
175- }
176-
177- p .footer {
178- text-align : right ;
179- font-size : 11px ;
180- border-top : 1px solid #D0D0D0 ;
181- line-height : 32px ;
182- padding : 0 10px ;
183- margin : 20px 0 0
184- }
185-
186- #container {
187- margin : 10px ;
188- box-shadow : 0 0 8px #D0D0D0
189- }
190- </style >
191- </head >
192- <body >
193- <div id =" container" >
194- <h1 >Welcome to CodeIgniter!</h1 >
195- <div id =" body" >
196- <div id =" messages" ></div >
197- <input type =" text" id =" text" placeholder =" Message .." >
198- <input type =" text" id =" recipient_id" placeholder =" Recipient id .." >
199- <button id =" submit" value =" POST" >Send</button >
200- </div >
201-
202- <p class =" footer" >
203- <span style =" float : left ;" id =" token" ></span >
204- Page rendered in <strong >{elapsed_time}</strong >
205- seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong >' . CI_VERSION . '</strong >' : '' ?>
206- </p >
207- </div >
208- <script src =" https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" ></script >
209- <script >
210- var conn = new WebSocket (' ws://localhost:8282' );
211- var client = {
212- user_id: < ? php echo $user_id; ? > ,
213- recipient_id: null ,
214- type: ' socket' ,
215- token: null ,
216- message: null
217- };
218-
219- conn .onopen = function (e ) {
220- conn .send (JSON .stringify (client));
221- $ (' #messages' ).append (' <font color="green">Successfully connected as user ' + client .user_id + ' </font><br>' );
222- };
223-
224- conn .onmessage = function (e ) {
225- var data = JSON .parse (e .data );
226- if (data .message ) {
227- $ (' #messages' ).append (data .user_id + ' : ' + data .message + ' <br>' );
228- }
229- if (data .type === ' token' ) {
230- $ (' #token' ).html (' JWT Token : ' + data .token );
231- }
232- };
233-
234- $ (' #submit' ).click (function () {
235- client .message = $ (' #text' ).val ();
236- client .token = $ (' #token' ).text ().split (' : ' )[1 ];
237- client .type = ' chat' ;
238- if ($ (' #recipient_id' ).val ()) {
239- client .recipient_id = $ (' #recipient_id' ).val ();
240- }
241- conn .send (JSON .stringify (client));
242- });
243- </script >
244- </body >
245- </html >
246- ```
247- ** Ok you just created your first app !** :heavy_check_mark : (easy with CTRL+C and CTRL+V)
248- #### :arrow_right : Run the Websocket server
249- If you wan't to check you'r work, you have to run the server.
250- Open you'r command prompt then type the command bellow in you'r project folder :
59+ #### :arrow_right : Run the Websocket server Manually
60+ If you want to enable debug mode type the command bellow in you'r project folder :
25161``` sh
25262php index.php welcome index
25363```
25464If you see the message the message bellow, you are done (don't close your cmd) !
25565
25666![ First_launch.png] ( https://user-images.githubusercontent.com/14097222/40981263-d568413a-68da-11e8-9ab2-7b3f7224526e.PNG )
25767#### :arrow_right : Test the App
258- Open three pages of your project on following url with different IDs :
259-
260- ` http://localhost/myproject/index.php/user/index/1 `
261-
262- ` http://localhost/myproject/index.php/user/index/2 `
263-
264- :heavy_exclamation_mark : In my example, ** recipient_id** is defined by ** user_id** , as you can see, it's the ** auth callback** who defines recipient ids.
265-
266- If you have something like that, everything is ok for you:
267-
268- ![ user_1] ( https://user-images.githubusercontent.com/38932580/57090224-21851500-6d28-11e9-9321-20d02e146d62.png )
269-
270-
271- ![ user_2] ( https://user-images.githubusercontent.com/38932580/57090269-44afc480-6d28-11e9-8ea1-30079a3a47e9.png )
272-
273-
274- You can try is by typing and sending something in each page (see cmd for more logs).
275-
276- ![ cmd] ( https://user-images.githubusercontent.com/38932580/57090313-5abd8500-6d28-11e9-8644-8e0323a36a41.png )
277-
27868
27969## Broadcast messages with your php App :boom : !
28070If you want to broadcast message with php script or something else you can use library like [ textalk/websocket] ( https://github.com/Textalk/websocket-php ) *** (who is included in my composer.json as required library)***
0 commit comments