我正在構建一個基於Firebase的IM平臺,我希望每個用戶都有一個地址將他們引導到聊天室。你怎樣才能得到每一塊數據的網址?
http://chatt3r.sitecloud.cytanium.com/
我正在構建一個基於Firebase的IM平臺,我希望每個用戶都有一個地址將他們引導到聊天室。你怎樣才能得到每一塊數據的網址?
http://chatt3r.sitecloud.cytanium.com/
<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()位的許多字符。
謝謝你的幫助。 – user2643642
沒問題,告訴我如果你有任何問題:) –
哈希值。
如果我正確理解您的問題,您希望能夠共享一個URL,以允許任何點擊該URL的人登錄到同一個聊天室。
我爲一個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)。如果有任何疑問或問題,那就問!
你可以給我一個HTML例子。 – user2643642
我稍後會發布 - 我現在沒有時間,對不起 - 如果您想讓我直接發送郵件,可以發郵件給我:) –
這就像使用FireBase在客戶端之間同步數據。 – user2643642
只要創建一個新用戶並將其與一個ID或一個簡短(小)的URL關聯,就創建一條記錄。然後讓你的頁面從URL中讀取ID。這是否回答你的問題? – user1477388
您只需進入頁面並且生成一個url就沒有註冊或渴望系統。 – user2643642
您也可以在加載頁面時創建插入語句。 – user1477388