我正在构建一个基于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