I'm practicing webSocket
and trying to make a connection php
with javascript - html5
to be able to chat later, so I have the following in the servidor.php
.
$host="127.0.0.1";
$port="3333";
$socket= socket_create(AF_INET,SOCK_STREAM,0);
socket_bind($socket,$host,$port) or die("error al vincular socket en el cliente");
echo socket_strerror(socket_last_error());
socket_listen($socket);
$i=0;
while(true){
$client[$i++]=socket_accept($socket);
$menssage=socket_read($client[$i], 1024);
echo $menssage;
$menssage="Hola".$menssage."\n";
socket_write($client[$i], $menssage."\n\r",1024);
socket_close($client[$i]);
}
socket_close($socket);
And I make a request from my client as follows:
$("#conectar").click(function(){
token=document.querySelector('meta[name="csrf-token"]').getAttribute('content');
conectar(token)
});
function conectar(tokenuser){
loadDoc("ws://127.0.0.1:3333",tokenuser, myFunction);
}
function loadDoc(url,clave ,cFunction) {
if ("WebSocket" in window) {
var ws = new WebSocket(url);
ws.onopen = function (clave) {
ws.send(clave);
};
ws.onerror = function (error) {
console.log('WebSocket Error');
};
ws.onmessage = function (evt) {
cFunction(evt.data);
};
ws.onclose = function () {
console.log("el WS fue cerrado");
};
}else{
console.log("descargue chrome");
}
}
function myFunction(resultado) {
console.log(resultado);
}
Once everything is encoded, I run the service:
C:\Users\minombre>php -f C:\xampp\htdocs\webSocket\servidor.php
La operacin se complet correctamente.
And so far so good, but once I press Connect it throws me an error on the server side:
PHP Notice: Undefined offset: 1 in C:\xampp\htdocs\webSocket\server.php on line 12
Notice: Undefined offset: 1 in C:\xampp\htdocs\webSocket\server.php on line 12 PHP Warning: socket_read() expects parameter 1 to be resource, null given in C:\xampp\htdocs\webSocket\server.php online 12
Warning: socket_read() expects parameter 1 to be resource, null given in C:\xampp\htdocs\webSocket\server.php on line 12 PHP Notice: Undefined offset: 1 in C:\xampp\htdocs\webSocket\server.php online 16
Notice: Undefined offset: 1 in C:\xampp\htdocs\webSocket\server.php on line 16 PHP Warning: socket_write() expects parameter 1 to be resource, null given in C:\xampp\htdocs\webSocket\server.php online 16
Warning: socket_write() expects parameter 1 to be resource, null given in C:\xampp\htdocs\webSocket\server.php on line 16 PHP Notice: Undefined offset: 1 in C:\xampp\htdocs\webSocket\server.php online 17
Notice: Undefined offset: 1 in C:\xampp\htdocs\webSocket\server.php on line 17 PHP Warning: socket_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\webSocket\server.php online 17
Warning: socket_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\webSocket\server.php on line 17
I don't know what I did wrong, if someone could help me I would really appreciate it.
Without testing it, I'd say your problem is here:
There you start with
$i === 0
, and then you increment it , with what$i
happens to be worth1
. Then when you doYou're actually accessing with
$i === 1
, which you don't have defined within the array .The simplest solution: increment
$i
at the end of the loop: