Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help creating a "fake" server-side socket connnection (make a bot) #245

Open
Tubusy opened this issue Oct 1, 2020 · 1 comment
Open

Comments

@Tubusy
Copy link

Tubusy commented Oct 1, 2020

Hi everyone!

I'm working on a game and need to create server-side bots. Ideally I'd do this by 'faking' a real client connection, or a real one from server-side. It has to behave like a real player's socket.

Stack Overflow suggests it is this easy to do in node.js:

var clientIo = require('socket.io-client');
var socket = clientIo.connect("http://" + node.ip + ":" + node.port);

//Then use it like normal socket to add a new player to the game.

However, I just can't figure out how to translate this into what we'd do in php.

The closest I can get to looks something like this:

$clientIo = new \PHPSocketIO\Client($server,$conn);
$socket = $clientIo->connect(???);

But despite my research I can't figure out what it wants from $server and $conn (I've tired the obvious) or the other attributes I need to give to it. Or if I'm barking up the wrong tree entirely. I thought I'd ask about this here as Stack Overflow is often not helpful with phpsocket.io.

Help would be greatly appreciated.

@walkor
Copy link
Owner

walkor commented Oct 1, 2020

Now there's no \PHPSocketIO\Client, but you can simulate it with AsyncTcpConnection, something like this.

<?php

use Workerman\Worker;
use PHPSocketIO\SocketIO;
use Workerman\Connection\AsyncTcpConnection;
use Workerman\Timer;

// Composer autoload
include 'vendor/autoload.php';
$io = new SocketIO(2020);

$io -> on('connection', function($socket) use ($io) {
    $socket->on('login', function($data) use($socket) {
        $socket->emit('login_success', array('a'=>'b', 'c'));
    });
});

$io -> on('workerStart', function() use ($io) {
    $socket = new AsyncTcpConnection('ws://127.0.0.1:2020/socket.io/?EIO=3&transport=websocket');
    $socket->onWebSocketConnect = function ($socket) {
        emit($socket, 'login', ['name' => 'tom']);
    };
    $socket->onMessage = function ($socket, $buffer) use ($io) {
        // 0{"sid":"1ec4093764ddd74103f9b51e","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
        if (strpos($buffer, '0') === 0) {
            return;
        }
        // 3
        if ($buffer === '3') {
            return;
        }
        // Receive emit from server
        // 42["event", "data"]
        if (strpos($buffer, '42') === 0) {
            $package = json_decode(substr($buffer, 2), true);
            $event = $package[0];
            $data = $package[1];
            switch ($event) {
                case 'login_success':
                    echo "login_success:";
                    var_dump($data);
                    $io->emit('new guy', $data);
                    break;
                case 'some other event':
                    // ......
                    break;
            }
        }

    };
    // heartbeat
    Timer::add(25, function () use ($socket) {
        $socket->send('2');
    });
    $socket->connect();
});

// emit from bot client
function emit($socket, $event, $data) {
    $socket->send('42'.json_encode([$event, $data]));
}

Worker::runAll();

image
The data format transmitted by the client and the server is similar to this. You can see it in network in Google browser. You can learn more about the data format through observation.

image

This is the js client side code for the data format above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants