N
N
nepster-web2014-01-08 17:53:37
PHP
nepster-web, 2014-01-08 17:53:37

How to organize communication between php and node.js on a socket?

php

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        
            $msg = "Пинг !";
            $len = strlen($msg);
        
            socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 8080);
            socket_close($sock);


I'm trying to pull a line at 127.0.0.1:8080 and read it with a node:

var io = require('socket.io').listen(8080);
    io.set('log level', 1);
    io.set('heartbeats', true);
    io.set('heartbeat timeout', 30);
    io.set('heartbeat interval defaults',5);
    io.set('heartbeat interval defaults',5);




io.sockets.on('connection', function (socket) 
{
    
    socket.on('message', function (msg) 
    {
        
        console.log(msg);

    });
    
});


However, everything is quiet, please tell me how to exchange data correctly?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Lerg, 2014-01-08
@Lerg

I'm not a node expert, but most likely your TCP port is listening, but sent to the UDP port.

B
buzzi888, 2014-01-08
@buzzi888

You need to open a UDP socket on the node, instead of socket.io
UDP nodejs

var dgram = require("dgram");

var server = dgram.createSocket("udp4");

server.on("error", function (err) {
  console.log("server error:\n" + err.stack);
  server.close();
});

server.on("message", function (msg, rinfo) {
  console.log("server got: " + msg + " from " +
    rinfo.address + ":" + rinfo.port);
});

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " +
      address.address + ":" + address.port);
});

server.bind(8080);
// server listening 0.0.0.0:8080

S
Sergey, 2014-01-08
Protko @Fesor

And what were the reasons in php to use udp? it is more logical to use tcp.

M
Mikhail Zavalko, 2017-06-02
@Mixa_007

Slick slider is good for this purpose. Slider Syncing section, just swap the navigation in the HTML so it's on top.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question