【问题标题】:quickblox for web - create 1 to 1 chatquickblox 网页版 - 创建 1 对 1 聊天
【发布时间】:2014-01-14 13:31:51
【问题描述】:

我正在尝试从网络客户端创建一对一聊天。

我下载了 SDK 和群聊示例。 除了网络之外,所有平台似乎都有很好的例子。

(例如:http://quickblox.com/developers/Android_XMPP_Chat_Sample

谁能提供代码/示例/方向?

是我遗漏了什么,还是真的缺少网络文档?

谢谢

【问题讨论】:

  • 有人吗?在哪里可以找到如何使用 Web SDK 创建一对一聊天的说明?

标签: quickblox


【解决方案1】:

WebSDK 已经足够新了。我们致力于其文档。 但是,在这里,我将向您展示一些代码 sn-ps 如何创建一对一聊天。

如您所知,QuickBlox 使用 XMPP 服务器作为聊天服务。 WebSDK 没有围绕 XMPP API 的包装器,因此您应该包含额外的 XMPP JS 库。

对于我们的示例,我们建议使用 Strophe.js (http://strophe.im/strophejs/)

让我们开始吧:

1) 包含您的 xmpp js 库和 WebSDK

<script src="quickblox.js"></script>
<script src="strophe.js"></script>

2) 描述您的 QB 凭据

var QBAPP = {
  app_id: '<your QuickBlox id>',
  auth_key: '<your QuickBlox key>',
  auth_secret: '<your QuickBlox secret>'
};

// our parameters to connect to QuickBlox Chat service
var CHAT = {
  server: 'chat.quickblox.com',
  bosh_server: 'http://chat.quickblox.com:5280'
};

3) 使用用户身份验证创建 QB 会话

var params, connection;
params = {
  login: '<your QB login>',
  password: '<your QB password>'
};

QB.init(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);
QB.createSession(params, function(err, res) {
  if (err) {
    console.log(err.detail);
  } else {
    connectChat(res.user_id, params.password);
  }
});

4) 使用您的用户 JID 和密码 (http://quickblox.com/developers/Chat#Connecting_to_server) 连接到 QuickBlox 聊天服务器

function connectChat(user_id, password) {
  var userJID = user_id + "-" + QBAPP.app_id + "@" + CHAT.server;
  var userPass = password;

  connection = new Strophe.Connection(CHAT.bosh_server);
  connection.rawInput = rawInput;
  connection.rawOutput = rawOutput;

  connection.connect(userJID, userPass, function (status) {
    switch (status) {
    case Strophe.Status.ERROR:
      console.log('[Connection] Error');
      break;
    case Strophe.Status.CONNECTING:
      console.log('[Connection] Connecting');
      break;
    case Strophe.Status.CONNFAIL:
      console.log('[Connection] Failed to connect');
      break;
    case Strophe.Status.AUTHENTICATING:
      console.log('[Connection] Authenticating');
      break;
    case Strophe.Status.AUTHFAIL:
      console.log('[Connection] Unauthorized');
      break;
    case Strophe.Status.CONNECTED:
      console.log('[Connection] Connected');

      // Create an event handler for getting messages
      connection.addHandler(onMessage, null, 'message', null, null, null);
      // send a presence message
      connection.send($pres().tree());

      break;
    case Strophe.Status.DISCONNECTING:
      console.log('[Connection] Disconnecting');
      break;
    case Strophe.Status.DISCONNECTED:
      console.log('[Connection] Disconnected');
      break;
    case Strophe.Status.ATTACHED:
      console.log('[Connection] Attached');
      break;
    }
  });
}

// logs
function rawInput(data) {console.log('RECV: ' + data);}
function rawOutput(data) {console.log('SENT: ' + data);}

5) 接收消息函数

function onMessage(msg) {
  console.log(msg);

  var to = msg.getAttribute('to');
  var from = msg.getAttribute('from');
  var type = msg.getAttribute('type');
  var elems = msg.getElementsByTagName('body');

  // we must return true to keep the handler alive.  
  // returning false would remove it after it finishes.
  return true;
}

6) 发送消息函数

function sendMessage() {
  params = {
    to: '<some JID>', // JID of recipient QB User
    from: connection.jid, // JID of sender QB user
    type: 'chat' // type of the message
  }
  var msg = $msg(params).c('body').t('Hello world!');
  connection.send(msg.tree());
}

我敢肯定,它可以帮助您使用 Javascript 创建与 QuickBlox 的一对一聊天。 如果您想使用群聊,您可以查看 Web XMPP 聊天示例开发分支中的“聊天模块代码”: https://github.com/QuickBlox/sample-chat-xmpp-web/blob/develop/qbchatroom.js

今天我们完成了新样本的设计http://i.imgur.com/r8CSdNV.png,很快就会将其部署到生产环境中。

【讨论】:

  • 嘿,非常感谢!我仍然有一个问题。让我首先说(附注)我认为您发布的代码需要一个小改动:变量“连接”未在函数 sendMessage 中定义。无论如何,我现在面临的问题是:当调用 connection.send(msg.tree()) 时,我得到: POST chat.quickblox.com:5280 405 (Invalid hostname.) 不知道是不是代理问题,是个问题带有 URL 或 clinet 中的内容。 (尽管我的代码与您发布的代码 98% 相同)您有什么想法吗?谢谢。
  • 你能告诉我来自函数 sendMessage 的console.log(params) 吗?
  • 当然。当前尝试由同一用户发送和接收:
  • 对象{至:“767579-5339@chat.quickblox.com”,来自:“767579-5339@chat.quickblox.com”,输入:“聊天”}
  • 问题只出在post上。如果我注释掉这一行:connection.send(msg.tree());我没有错误。
【解决方案2】:

我建议您可能会看到 405 错误,因为您在调用 connectChat 函数之后立即调用了 sendMessage 函数。

连接到聊天需要一些时间,所以在您的客户端(浏览器)一开始还没有完成连接到聊天服务器之前,您无法发送消息。您需要将 sendMessage 函数的调用放在状态为“已连接”的函数 connectChat 的回调中。或者,您可以对标签 &lt;button&gt; 或其他内容的 onclick 事件执行 sendMessage 功能。对于您的示例,请在此处插入 sendMessage:

case Strophe.Status.CONNECTED:
    console.log('[Connection] Connected');

    connection.addHandler(onMessage, null, 'message', null, null, null);
    connection.send($pres().tree());

    sendMessage();

    break;

【讨论】:

  • 通过调用'sendMessage();'解决的问题来自“connectChat”谢谢!
【解决方案3】:

从今天开始,QuickBlox 拥有自己的 WebSDK 的 Web XMPP 聊天插件。 您可以在此处查看使用此库的 1:1 聊天的新示例: http://quickblox.com/developers/Web_XMPP_Chat_Sample

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多