使用框架发送短信

Text messaging using a-frame

我想在a-frame中添加一个文字聊天系统。我正在使用联网的 a-frame,因为连接性可以是任何可能的,或者我必须使用其他技术。

有一个 simple API to send custom messages 网络层已经设置:

// subscribe to custom data
NAF.connection.subscribeToDataChannel(dataType, (senderId, dataType, data, targetId) => {})

// send custom data
NAF.connection.broadcastData(dataType, data)

所以发送聊天消息很简单:

const btn = document.querySelector("button");      // SEND btn
const input = document.querySelector("input");     // input field with the text
const log = document.querySelector("#messages")    // message log

// when you want to send a message
btn.addEventListener("click", evt => {
  // log your own messages
  messages.innerHTML += NAF.clientId + ": " + input.value + '<br>'
  // broadcast the text as some unique dataType (like "chat")
  NAF.connection.broadcastData("chat", {txt: input.value}) 
})

// when a "chat" type message arrives
NAF.connection.subscribeToDataChannel("chat", (senderId, dataType, data, targetId) => {
  // append the data.txt to the message log
  messages.innerHTML += senderId + ": " + data.txt + '<br>'
})

看看in this glitch: