你怎么能得到每个数据的网址?

时间:2013-08-01 20:35:12

标签: firebase

我正在构建一个基于Firebase的IM平台,我希望每个用户都有一个地址将他们引导到聊天室。

http://chatt3r.sitecloud.cytanium.com/

2 个答案:

答案 0 :(得分:0)

散列。

如果我正确理解您的问题,您希望能够共享一个网址,允许点击该网址的用户登录同一个聊天室。

我为我制作过一次的Firebase应用做了这个。您需要做的第一件事是使用.push() method。将房间推送到Firebase,然后使用toString()方法获取推送的URL。一些快速的JS字符串操作 - window.location.hash = pushRef.toString().slice(x) - 其中“x”是您要剪切URL的任何位置。 window.location.hash会为您设置哈希值。将哈希添加到共享URL,然后进行下一步:

你需要一个哈希监听器来检查你打开页面时是否已经有一个哈希,所以$(window).bind('hashchange', function() {UpdateHash()})进入doc.ready函数,然后......

function UpdateHash() {
    global_windowHash = window.hash.replace("#", "");
    // to assign the hash to a global hash variable
    if (global_windowHash) {
        // if there was already a hash
        chatRef = new Firebase("[Your Firebase URL here]" + "/chatrooms/" + global_windowHash);
        // chatRef is the global that you append the chat data to, and listen from.
    } else {
        // there wasn't a hash, so you can auto-create a new one here, in which case:
        chatRef = new Firebase("[Your Firebase URL here]" + "/chatrooms/");
        chatRef = chatRef.push();
        window.location.hash = chatRef.toString().slice(x);
    }
}

我希望有所帮助(并且有效:P)。如果有任何问题或疑问,请问!

答案 1 :(得分:0)

<head>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://cdn.firebase.com/v0/firebase.js"></script>

    <script>
        var hash; // global incase needed elsewhere

        $(function() {
            hash = window.location.hash.replace("#", "");;
            myRootRef = new Firebase("http://xxxx.firebaseio.com");

            var postingRef;

            if (hash) {
                // user has been given a hashed URL from their friend, so sets firebase root to that place
                console.log(hash);

                postingRef = new Firebase("http://xxxx.firebaseio.com/chatrooms/" + hash);
            } else {
                // there is no hash, so the user is looking to share a URL for a new room
                console.log("no hash");

                postingRef = new Firebase("http://xxxx.firebaseio.com/chatrooms/");
                // push for a unique ID for the chatroom
                postingRef = postingRef.push();
                // exploit this unique ID to provide a unique ID host for you app
                window.location.hash = postingRef.toString().slice(34);
            }

            // listener
            // will pull all old messages up once bound
            postingRef.on("child_added", function(data) {
                console.log(data.val().user + ": " + data.val().message);
            });
            // later:
            postingRef.push({"message": "etc", "user": "Jimmybobjimbobjimbobjim"});
        });
    </script>

</head>

那对我来说很有帮助。您需要将xxxx更改为您所在的URL,并添加第一部分位于.slice()位的许多字符。