使用gos socketBundle的symfony3聊天应用程序

时间:2017-02-16 03:59:31

标签: websocket symfony

我正在使用gos socketBundle开发聊天应用程序我试图在javascript中使用此代码显示已连接的用户。 当订阅完成到频道连接的用户总是connectedUsers是null?有人可以帮我吗?

{{ws_client()}}

<script type="text/javascript">

    var websocket = WS.connect("ws://127.0.0.1:8080");

    //var websocket = WS.connect("ws://{#{{ wsUrl }}#})");
    websocket.on("socket/connect", function (session) {



        session.subscribe("connected-users/channel", function (uri, payload) {

            console.log(session);
            console.log(payload);
            var connectedUsers = JSON.parse(payload.connectedUsers);
            var newtml = '';
            connectedUsers.forEach(function (user) {
                newtml += '<a name="toggle" class="connectedUserLink" onclick="talkToUser(\'' + user.id + '\', \'' + user.username + '\')">' + user.username + '</a>';
            });
            $('#connectedUserNbr').html(connectedUsers.length);
            $('#connectedUserList').html(newtml);
        });
    });
    websocket.on("socket/disconnect", function (error) {
        //error provides us with some insight into the disconnection: error.reason and error.code
        console.log("Disconnected for " + error.reason + " with code " + error.code);

    });
</script>

0 个答案:

没有答案