从 PHP 向 socket.io 发送事件

Emitting events to socket.io from PHP

您好,我正在尝试从 PHP 向 socketio 服务器发送事件。我试过这两种叉子:

https://github.com/rase-/socket.io-php-emitter
https://github.com/ashiina/socket.io-php-emitter

PHP代码:

<?php

require_once( 'socketio/src/Emitter.php' );

$emitter = new SocketIO\Emitter( array( 'port' => '6379', 'host' => '127.0.0.1') );

$emitter->broadcast->emit( 'testcall', 'data' );

?>

package.json:

{
    "name": "Emitter",
    "devDependencies": {
        "express": "~4.13.4",
        "socket.io": "~1.7.3",
        "socket.io-redis": "~4.0.0"
    }
}

节点服务器:

var app = require( 'express' )( ),
    http = require( 'http' ).Server( app ),
    io = require('socket.io')( http ),
    redis = require( 'socket.io-redis' );

http.listen( 8081, function( ) {
    console.log( '\n==========================' );
    console.log( 'server listening on *:8081' );
    console.log( '==========================\n' );
});

io.adapter( redis({ host: '127.0.0.1', port: 6379 }) );

io.on( 'connection', function( socket ) {
    socket.on( 'testcall', function( ) {
        console.log( 'testcall' );
    });
});

我打开了 redis-cli 监视器,我可以看到它向 redis 发布数据,但我从未看到日志出现。我可以从浏览器发出事件,但 PHP 永远不会出现。

知道我做错了什么吗?

它有效我只是愚蠢。来自 PHP 的事件正在发送,但发送给客户端而不是服务器。貌似忘记怎么用了哈哈