Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not sending message via php #24

Open
JacoSJKruger opened this issue Dec 21, 2021 · 2 comments
Open

Not sending message via php #24

JacoSJKruger opened this issue Dec 21, 2021 · 2 comments

Comments

@JacoSJKruger
Copy link

JacoSJKruger commented Dec 21, 2021

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
Copy link
Author

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.

@ffontoura
Copy link

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));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants