1

我需要通过不会自动删除自身的 Node 动态创建私人和持久聊天室。

我在网上搜索过,但找不到太多关于如何操作的信息。这是我用来创建聊天室的代码片段:

var cl = new xmpp.Client({
    jid: jabber_creds.jid,
    password: jabber_creds.password,
    host: jabber_creds.host,
    port: jabber_creds.port
});


cl.on('online', function() {
    var room_jid = jabber_creds.room_jid.replace("%s", chatRoomName);
    // join room (and request no chat history)
    cl.send(new xmpp.Element('presence', { to: room_jid }).
        c('x', { xmlns: 'http://jabber.org/protocol/muc' })
    );

    // create room
    cl.send(new xmpp.Element('iq', { to: room_jid, id: 'create', type: 'set' }).
        c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' }).
        c('x', { xmlns: 'jabber:x:data',type: 'submit' })
    );
});
4

3 回答 3

0

You can reference https://github.com/node-xmpp/node-xmpp/blob/master/examples/create_room.js and send configuration you want by XEP-0045

//create_room.js
'use strict'

var xmpp = require('../index')
    ,argv = process.argv

if (argv.length < 5) {
    console.error('Usage: node create_room.js <my-jid> <my-password> <room-name>')
    process.exit(1)
}

var cl = new xmpp.Client({ jid: argv[2],  password: argv[3] })

cl.on('online', function(data) {
    var userJid = data.jid.user + '@' + data.jid.domain,
        roomJid = argv[4] + '@conference.' + data.jid.domain,
        pres,
        iq

    console.log('Connected as ' + userJid + '/' + data.jid.resource)

    console.log('Create room - ' + argv[4])

    pres = new xmpp.Element(
        'presence',
        { from: userJid, to: roomJid + '/' + data.jid.user })
        .c('x', {'xmlns':'http://jabber.org/protocol/muc'})

    cl.send(pres.tree())

    iq = new xmpp.Element(
        'iq',
        { to: roomJid, type: 'set' })
    .c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' })
    .c('x', { xmlns: "jabber:x:data", type: "submit"})
    //set room to be hidden by sending configuration. ref: http://xmpp.org/extensions/xep-0045.html
    iq.c('field', { 'var': 'FORM_TYPE' })
    .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
    .c('field', { 'var': 'muc#roomconfig_publicroom'})
    .c('value').t('0').up().up()

    cl.send(iq.tree())

    // exit later for sending configuration done
    setTimeout(function() {
        cl.end()
    }, 100)

})

cl.on('error', function(e) {
    console.error(e)
    process.exit(1)
})
于 2014-08-08T07:41:43.897 回答
0

尝试关注 XEP-0045

http://xmpp.org/extensions/xep-0045.html#createroom

只需阅读创建和配置此类房间的工作流程如下部分

您需要执行以下操作:

  1. 将第一个存在发送到新房间
  2. 服务器向您返回几条有关房间已创建的消息,但您应该将其解锁
  3. 您应该创建配置表单并发送到服务器。在此表单中,您可以设置“持久”类型和“仅限成员”类型
  4. 如果一切正常,服务器将返回成功结果
于 2014-01-06T11:18:25.580 回答
0

聊天室持久性在服务器而不是客户端处理。是的,客户端可以请求服务器保留聊天室,但您实际上不能从客户端持久保存它。检查您正在使用的服务器文档以确保它支持这一点。

于 2013-12-25T10:51:58.760 回答