Skip to content
Snippets Groups Projects
Commit 55c24eeb authored by Tiger Wang's avatar Tiger Wang Committed by Tiger Wang
Browse files

No longer send room presence with every message

parent b0425cd6
No related branches found
No related tags found
1 merge request!69Spycraft: Send and receive XCDP through XMPP
import { client, xml } from "@xmpp/client" import { client, xml } from "@xmpp/client"
import uuid from "uuid" import uuid from "uuid"
import debug from "@xmpp/debug"
export default class XMPPConnection extends EventTarget { export default class XMPPConnection extends EventTarget {
async joinChannel(channel) { joinChannel(channel) {
const channelIdent = `${channel}@conference.xmpp.lets-draw.live/${this.username}` const channelIdent = `${channel}@conference.xmpp.lets-draw.live/${this.username}`
const presence = xml( const presence = xml(
"presence", "presence",
{ to: channelIdent }, { to: channelIdent },
xml("x", { xmlns: "http://jabber.org/protocol/muc" }), xml("x", { xmlns: "http://jabber.org/protocol/muc" }),
) )
this.sendOrQueue(presence)
}
sendOrQueue(message) {
if (this.online) { if (this.online) {
await this.xmpp.send(presence) this.xmpp.send(message)
} else { } else {
this.queue.push(presence) this.queue.push(message)
} }
} }
async sendChannelMessage(channel, message) { sendChannelMessage(channel, message) {
const channelIdent = `${channel}@conference.xmpp.lets-draw.live` const channelIdent = `${channel}@conference.xmpp.lets-draw.live`
const sentmessage = xml( const wrappedMessage = xml(
"message", "message",
{ {
type: "groupchat", type: "groupchat",
...@@ -28,7 +32,7 @@ export default class XMPPConnection extends EventTarget { ...@@ -28,7 +32,7 @@ export default class XMPPConnection extends EventTarget {
xml("body", {}, message), xml("body", {}, message),
) )
await this.xmpp.send(sentmessage) this.sendOrQueue(wrappedMessage)
} }
constructor() { constructor() {
...@@ -45,6 +49,19 @@ export default class XMPPConnection extends EventTarget { ...@@ -45,6 +49,19 @@ export default class XMPPConnection extends EventTarget {
password: "beartest", password: "beartest",
}) })
this.xmpp = xmpp
debug(xmpp, true)
xmpp.on('status', status => {
console.debug('🛈', 'status', status)
})
xmpp.on('input', input => {
console.debug('', input)
})
xmpp.on('output', output => {
console.debug('', output)
})
xmpp.on("error", (err) => { xmpp.on("error", (err) => {
console.error("", err.toString()) console.error("", err.toString())
}) })
...@@ -53,7 +70,7 @@ export default class XMPPConnection extends EventTarget { ...@@ -53,7 +70,7 @@ export default class XMPPConnection extends EventTarget {
this.online = false this.online = false
}) })
xmpp.on("stanza", async (stanza) => { xmpp.on("stanza", (stanza) => {
if (stanza.is("message")) { if (stanza.is("message")) {
this.dispatchEvent( this.dispatchEvent(
new CustomEvent("stanza", { new CustomEvent("stanza", {
...@@ -77,13 +94,11 @@ export default class XMPPConnection extends EventTarget { ...@@ -77,13 +94,11 @@ export default class XMPPConnection extends EventTarget {
}) })
xmpp.start().catch(console.error) xmpp.start().catch(console.error)
this.joinChannel("imperial")
this.xmpp = xmpp
} }
sneakilySendTheOtherTeamOur(secrets) { sneakilySendTheOtherTeamOur(secrets) {
this.joinChannel("imperial").then( this.sendChannelMessage("imperial", secrets)
this.sendChannelMessage("imperial", secrets),
)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment