Trying to create a socket between servers with SocketIO and PHPws

I have a problem with websockets. So, I have an Rpi that provides me some data through the socketIO client in a rather simple way. The following code shows how I can get this data:

<!DOCTYPE html> <html> <header> <title>SocketIO test</title> <script src="http://192.168.5.5:8000/socket.io/socket.io.js"></script> </header> <body> <script type="text/javascript"> var client = io.connect('http://192.168.5.5:8000'); client.on('connect', function() { console.log('connected'); }); client.on('raw', function(data){ console.log(data); }); client.on('state', function(data){ console.log(data); }); </script> </body> 

However, what I need to implement is a bit more complicated. I need to use the Apache server to determine some data before it gets to the client side. The following figure shows what I'm trying to build:

enter image description here

To achieve my goal, I tried several WebSocket servers and client libraries for PHP until I found PHPws , which looks like the best solution for my scenario. So, I read the examples, I test them, and everything went well until I tried to connect to Rpi with the following code:

 require_once("../vendor/autoload.php"); $loop = \React\EventLoop\Factory::create(); $logger = new \Zend\Log\Logger(); $writer = new Zend\Log\Writer\Stream("php://output"); $logger->addWriter($writer); $client = new \Devristo\Phpws\Client\WebSocket("ws://192.168.5.5:8000", $loop, $logger); $client->on("connect", function() use ($logger, $client){ $logger->notice("Or we can use the connect event!"); $client->send("Hello world!"); }); $client->on("raw", function($message) use ($client, $logger){ $logger->notice("Got message: ".$message->getData()); $client->close(); }); $client->open()->then(function() use($logger, $client){ $logger->notice("We can use a promise to determine when the socket has been connected!"); }); $loop->run(); 

I more or less accepted this example from Devristo github.

On the server side, program execution does not cause any errors or messages.

Is it possible to build what I want to build here with PHPws? If so, am I connecting to the Rpi server correctly with the PHPws code example shown?

+5
source share
1 answer

Maybe:

[Node] Socket Server (this will be your RPi )

A simple socket.io server in node for verification (success).

 var io = require('socket.io')(1337); io.on("connection",function(socket){console.log("[+] client",socket.id);}) 

[PHP] Socket Client

Using Elephant.IO , we configure the client ( Client example for Socket.IO v2.0 )

 <?php use ElephantIO\Client; use ElephantIO\Engine\SocketIO\Version2X; require __DIR__ . '/vendor/autoload.php'; $client = new Client(new Version2X('http://localhost:1337', [ 'headers' => [ 'X-My-Header: websocket rocks', 'Authorization: Bearer 12b3c4d5e6f7g8h9i' ] ])); $client->initialize(); $client->emit('broadcast', ['foo' => 'bar']); $client->close(); 

With this simple Client / Server example, you will see the "on connection" event on the node server when the browser opens client.php

0
source

Source: https://habr.com/ru/post/1271328/


All Articles