codeigniter-websocket icon indicating copy to clipboard operation
codeigniter-websocket copied to clipboard

Not sending message via php

Open JacoSJKruger opened this issue 3 years ago • 2 comments

Hi there,

I've changed the php text to "ws://localhost:8282" as having it as 0.0.0.0 was giving a 10049 error (where as localhost doesn't give any error).

Still it seems to not be working correctly. If I connect via javascript it seems to be working fine, but when I send a message using php I don't get anything.

PHP (not working) `public function test_webs(){ $socket_client = new WebSocket\Client("ws://0.0.0.0:8282");

  $socket_client->send(json_encode(array('user_id' => 1, 'message' => null)));
   $socket_client->send(json_encode(array('user_id' => 1, 'message' => 'Super cool message to myself!')));
}`

javascript (working fine) `var conn = new WebSocket('ws://localhost:8282'); var client = { user_id: 5, recipient_id: null, type: 'socket', token: null, message: null };

conn.onopen = function (e) {
    conn.send(JSON.stringify(client));
    console.log("Socket open");
};

conn.onmessage = function (e) {
    var data = JSON.parse(e.data);
    if (data.message) {

        console.log("Incomming User "+data.user_id+": ");
        console.log(data.message);
    }
    if (data.type === 'token') {
        // $('#token').html('JWT Token : ' + data.token);
        console.log('JWT Token : ' + data.token);
    }
};`

Assistance will be appreciated.

JacoSJKruger avatar Dec 21 '21 11:12 JacoSJKruger

For example:

If I send this: image

Then it receives it just fine via Javascript in the other window: image

However, loading that PHP script does not result in the same type of response (as I would have expected). It doesn't seem to be receiving anything.

JacoSJKruger avatar Dec 21 '21 11:12 JacoSJKruger

Try this

$client = new \WebSocket\Client('ws://localhost:8282');

		$cliente['user_id'] = 2;
		$cliente['recipient_id'] = null;
		$cliente['type'] = 'socket';
		$cliente['token'] = null;
		$cliente['message'] = null;

		$client->send(json_encode($cliente));

		$cliente['user_id'] = 2;
		$cliente['recipient_id'] = 1;
		$cliente['type'] = 'chat';
		$cliente['broadcast'] = true;
		$cliente['token'] = null;
		$cliente['message'] = 'teste de mensagem';

		$client->send(json_encode($cliente));

ffontoura avatar Jun 07 '24 11:06 ffontoura