How to call a WebSocket programmatically (using PHP)?

前端 未结 5 2045
孤街浪徒
孤街浪徒 2021-02-04 18:46

I have a situation where I need to update one browser window based on input from the other. Right now I\'m using WebSockets and it\'s working great.

Now I want to sen

5条回答
  •  囚心锁ツ
    2021-02-04 19:20

    The most important part is that the message needs to be sent on the existing socket, meaning you cant call socket_connect, fsockopen, or any other function within PHP that will attempt an unsolicited connection to the client. This isn't a websocket thing - that's a fundamental concept in network programing.

    On phpwebsocket it would be somethin like:

    $msg = "hello world";
    $phpwebsocket->send($user->socket, $msg);
    

    where '$phpwebsocket' is the PHP WebSocket object, $user->socket is a connected user who connected priory using with a javascript WebSocket(), and send() is a method within the WebSocket object that will properly encode the message into a frame (or should as it will soon be required).

    However, if for any reason you want to connect to the websocket server using websockets from PHP, you'll want to check out https://github.com/nicokaiser/php-websocket. The server in the link wont be of any importance if your happy with your current solution, but the package also contains a PHP Websocket client class which is what you would need.

提交回复
热议问题