L
L
Lavrov952020-12-18 16:06:00
Socket.io
Lavrov95, 2020-12-18 16:06:00

Why does ElephantIO always show user disconnected?

I am sending data from client.php file and can't get data in client.js and it always shows user is logged out. What could be the reason?

clinet.php

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;

$client = new Client(new Version2X('http://localhost:9009', [
        'headers' => [
        'X-My-Header: websocket rocks',
        'Authorization: Bearer 12b3c4d5e6f7g8h9i'
    ]
]));

$client->initialize();
$client->emit('user', ['foo' => 'bar']);
$client->close();


server.js

const server = require('http').createServer();
const io = require("socket.io")(server);

io.on('connection', function (socket) {
    socket.on('listen', function (data) {
        console.log('client connected');
        io.emit('user', {name: 'Marcelo Aires'});
    });

    // If some user disconnect
    socket.on('disconnect', function () {
        console.log('user disconnected');
    })
});

server.listen(9009);


client.js

var socket = io('http://localhost:9009');

socket.on('user', function (data) {
    console.log(data);
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question