我正在使用Firechat,我可以成功启动聊天窗口。我正在使用Firebase custom authentication,我可以毫无问题地登录。但是,我现在尝试创建一个新的聊天室然后输入它。根据{{3}}我做了以下内容:
<!doctype html>
<html>
<head>
<title>Test</title>
<meta charset="UTF-8" />
<script src='https://cdn.firebase.com/js/client/2.0.2/firebase.js'></script>
<link rel='stylesheet' href='https://cdn.firebase.com/libs/firechat/2.0.1/firechat.min.css' />
<script src='https://cdn.firebase.com/libs/firechat/2.0.1/firechat.min.js'></script>
</head>
<body>
<script type='text/javascript'>
var fireBase = new Firebase("https://XXXXXXXXX.firebaseio.com/");
function initChat(authData) {
var Firechat = new FirechatUI(fireBase, document.getElementById('firechat'));
Firechat.setUser(authData.uid, "Username");
Firechat.createRoom("Test chat room", "public");
}
fireBase.authWithCustomToken("UNIQUE_TOKEN", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Login successful", authData);
initChat(authData);
}
});
</script>
<div id='firechat'>
</div>
</body>
</html>
在javascript控制台中,我可以看到登录成功:
Login successful Object { auth: Object, expires: XXXXXXXXX, token: "XXXXXXXX…", uid: "XXXXXX", provider: "custom" }
但找不到createRoom函数:
TypeError: Firechat.createRoom is not a function
知道这里出了什么问题吗?
答案 0 :(得分:2)
来自文档:
Firechat.createRoom(roomName, roomType, callback(roomId))
创建一个具有给定名称(字符串)和类型(字符串 - 公共或私有)的新房间,并在完成时调用带有房间ID的回调。
看起来你没有回调。
Firechat.prototype.createRoom = function(roomName, roomType, callback) {
var self = this,
newRoomRef = this._roomRef.push();
var newRoom = {
id: newRoomRef.name(),
name: roomName,
type: roomType || 'public',
createdByUserId: this._userId,
createdAt: Firebase.ServerValue.TIMESTAMP
};
if (roomType === 'private') {
newRoom.authorizedUsers = {};
newRoom.authorizedUsers[this._userId] = true;
}
newRoomRef.set(newRoom, function(error) {
if (!error) {
self.enterRoom(newRoomRef.name());
}
if (callback) {
callback(newRoomRef.name());
}
});
};
答案 1 :(得分:1)
事实证明,Firechat javascript插件中有两个类(是正确的单词):
var chat = new FirechatUI
var chat = new Firechat
因为它们看起来很相似,所以我没有注意到它们之间的区别。在文档中没有任何地方我能够找到FirechatUI实例的详细信息(即使在github readme上建议使用此代码)。
无论如何,问题是new FirechatUI
加载了聊天的实际用户界面。 new Firechat
加载API,允许您与聊天插件对话(但不到UI)。这是一个重要的区别。找到的文档here仅与API相关,因此如果您启动new Firechat
实例。然而,诀窍是让UI启动并运行,然后直接与它交互(做一些事情,比如创建新房间或进入房间)。我老实说没有找到如何以官方/推荐方式这样做。我唯一能想到的就是黑客攻击。这很难看,但它确实有效。下面的代码包括创建新聊天室(使用Firechat
)以及在UI中打开特定聊天室的功能(该位被黑客攻击,因为我无法找到直接与UI交互的方式)
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Title</title>
<!-- jQuery -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<!-- Firebase -->
<script src='https://cdn.firebase.com/js/client/2.1.0/firebase.js'></script>
<!-- Firechat -->
<link rel='stylesheet' href='https://cdn.firebase.com/libs/firechat/2.0.1/firechat.min.css' />
<script src='https://cdn.firebase.com/libs/firechat/2.0.1/firechat.min.js'></script>
<!-- This plugin here: https://gist.github.com/buu700/4200601 -->
<script type="text/javascript" src="js/arrive.js"></script>
<style type="text/css">
#firechat{width:400px}
</style>
</head>
<body>
<h1>Test</h1>
<script type='text/javascript'>
var fireBase = new Firebase("https://XXXXXX.firebaseio.com/");
function roomChatSetup(authData) {
var chat = new Firechat(fireBase);
chat.setUser(authData.uid, "My User Name", function(user) {
console.log("Creating chatroom...");
chat.createRoom("New Chatroom Name", "public", function(roomId) {
console.log("Created room "+roomId);
});
$("#firechat").html("<div class='alert alert-success'>Your chatroom has been set up. Refresh to view</div>");
});
}
function initChat(authData) {
var chatUI = new FirechatUI(fireBase, document.getElementById('firechat'));
chatUI.setUser(authData.uid, "My User Name");
console.log("Simulating clicks...");
$("#firechat-tab-content div.tab-pane").waitUntilExists(function(){
console.log("Close all other tabs by simulating clicking the X button");
$("#firechat-tab-content div.tab-pane:not(#XXXXXXXXX) a.close").click(); // XXXXX should have the chatroom name of the one you want to open
});
$("#firechat-btn-rooms").waitUntilExists(function(){
$("#firechat-btn-rooms").click();
console.log("Open submenu to load all possible rooms");
});
$("li[data-room-id='XXXXXXXXXXXXX']").waitUntilExists(function(){
$("li[data-room-id='XXXXXXXXXX'] a").click();
console.log("Simulating clicking on chatroom XXXXXXXXXXXXXX");
});
}
fireBase.authWithCustomToken("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Login successful", authData);
// Here you can use a programming language to decide. If you already have a
// chatroom, run initChat. If you don't, run createRoom. I haven't been
// able to run them both at the same time.
initChat(authData);
// or:
roomChatSetup(authData);
}
});
</script>
<div id="firechat"></div>
</body>
</html>
答案 2 :(得分:0)
FirechatUI对象与Firechat对象是分开的。 FirechatUI没有与Firechat相同的方法。
为了从FirechatUI对象获取关联的Firechat对象,您可以执行以下操作:
let chatUI = new FirechatUI(chatRef, document.getElementById("firechat-wrapper"));
let chat = chatUI._chat;
然后,您可以毫无问题地执行任何正常的Firechat操作。
chat.setUser(user.uid, firstName, function(user) {
chat.resumeSession();
});
请记住,_chat元素并不应该被使用(正如你可以从命名约定中看出来的那样),但是由于FirechatUI没有正确地暴露出足够的功能,这可能是最干净的方法。