【问题标题】:Elephant.io + Socket.io return [object Object]Elephant.io + Socket.io 返回 [object Object]
【发布时间】:2015-12-18 20:31:06
【问题描述】:

我试图在 symfony2 应用程序上与 Elephant.io(Elephantio 捆绑包)和 Socket.io 进行实时聊天。

socket.io 服务器和我的 symfony 应用程序之间的连接运行良好,但是当我尝试将数据传递给 socket.io 时,它返回 [object Object]

我就是这样做的:

#Config.yml
nc_elephant_io:
    clients:
        default:
            connection: http://localhost:3006
            version: 1.x
        socketio_key:
            connection: http://localhost:3000
            version: 1.x

我的控制器

public function indexAction()
{
    $client = $this->get('elephantio_client.socketio_key');
    $elephantIOClient = $client->getElephantIO();

    $elephantIOClient->initialize();
    $elephantIOClient->emit('chat message', ['msg' => 'bar']);
    $elephantIOClient->close();

    return $this->render('default/index.html.twig');
}

我的 server.js

var io = require('socket.io').listen(3000);

io.on('connection', function(socket){
    console.log('a user connected');
    
    socket.on('chat message', function(msg){
        console.log('message: ' + msg);
    });

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

当我刷新我的索引页面时,它会返回

连接的用户

但我的“味精”数据就在这里声明

$elephantIOClient->emit('聊天消息', ['msg' => 'bar']);

未渲染或未传递给socket.io并返回

连接的用户

消息:[对象对象]

用户断开连接

我已经阅读过有关此问题的主题,但从未阅读过关于 php/lephant.io 实现的文章。

是否有人已经遇到过这个问题,或者你也有一些想法来解决这个问题?

谢谢。

【问题讨论】:

    标签: php symfony socket.io


    【解决方案1】:

    很抱歉回复晚了,但因为我在我的项目中处理 socket.io 和大象.io 而来到这里。 无论如何,您的连接是完美的,并且由于您在客户端 console.log('message: ' + msg); 中记录一个对象,因此您将获得 [object Object]。尝试.操作符获取实际数据。

    socket.on('chat message', function(msg){  
       console.log('message: ' + msg.msg);
                                   //^^^^-- here since you are sending your object as "msg" from server
    }
    

    【讨论】:

    • 是的,这很好用,但现在我只使用 node.js 和没有大象.io 的 js 文件,它的工作方式很迷人,但我不知道这是否是好方法.
    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2015-07-16
    • 2022-01-19
    相关资源
    最近更新 更多