4

I have simple node.js websocket server:

var fs = require('fs')
var ws = require('../../')

var options = {
    secure: false,
}

var Server = ws.createServer(options, function(conn){
    conn.on("text", function (str) {
        broadcast(str);
        //conn.sendText(str.toUpperCase() + "!!!")
        console.log('connected');
        console.log(str);
        //console.log(Server.connections);
    })
}).listen(8001, "127.0.0.1");

function broadcast(str){
    Server.connections.forEach(function (connection) {
        connection.sendText(str)
    })
}

That works with JS client, but it doesn't work with PHP client, such as:

function MaskMessage($text)
{
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);

    if($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif($length > 125 && $length < 65536)
        $header = pack('CCn', $b1, 126, $length);
    elseif($length >= 65536)
        $header = pack('CCNN', $b1, 127, $length);
    return $header.$text;
}
$host = 'localhost';
$port = 8001;

$msg = 'hey hi hello';

$msg = MaskMessage('hej hej siema');

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// Bind the source address
$result = socket_connect($socket, $host, $port);
if(!$result)
    echo 'cannot connect '.socket_strerror(socket_last_error());
else{
    echo socket_write($socket, strval($msg), strlen($msg));

}
socket_strerror(socket_last_error());
socket_close($socket);

PHP creates socket and connects, it doesn't return any errors and message is sent, but node.js server doesn't receive anything from this client. What am I doing wrong? This client works with PHP websocket server.

5
  • Why don't you try socket.io instead of this? Commented May 16, 2014 at 19:28
  • going from ws to socket.io is like going from a Ferrari to a Schwinn... Commented May 16, 2014 at 19:38
  • what is processing the php? Is your server which is running the php receiving the request instead? Commented May 16, 2014 at 21:16
  • echo socket_write($socket, strval($msg), strlen($msg));.socket_write returns false on failure. Try to var_dump the return value instead of echoing it. Commented May 17, 2014 at 8:39
  • socket_write returns INT (15 exactly), that means whole message was sent. Commented May 17, 2014 at 18:44

1 Answer 1

7

I don't know what var ws = require('../../') is, so I can't comment on what you're doing wrong, but I just tried doing the same thing, and surprisingly it works!

PHP

<?php

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$result = socket_connect($socket, '127.0.0.1', 1337);

if(!$result) {
    die('cannot connect '.socket_strerror(socket_last_error()).PHP_EOL);
}

$bytes = socket_write($socket, "Hello World");

echo "wrote ".number_format($bytes).' bytes to socket'.PHP_EOL;

Node.js

var net = require('net');

var server = net.createServer();
var host = '127.0.0.1';
var port = 1337;

server.on('listening', function() {
    console.log('listening on '+host+':'+port);
});

server.on('connection', function(socket) {
    socket.on('data', function(buf) {
        console.log('received',buf.toString('utf8'));
    });
});

server.listen(port, host);

That's it! Start the node server first, then run the PHP script. You should see "received Hello World" on the node side, and "wrote 11 bytes to socket" on the PHP side.

Sign up to request clarification or add additional context in comments.

1 Comment

Finally, I decided to send data to NodeJS via Redis and it works. But thank You for Your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.