【问题标题】:Text messaging using a-frame使用框架发送短信
【发布时间】:2023-01-02 06:23:55
【问题描述】:

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

【问题讨论】:

    标签: aframe aframe-networked


    【解决方案1】:

    有一个 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

    【讨论】:

    • 非常感谢。你彻底解决了我的问题。这个解决方案对我有很大帮助。认为你在 A 型框架方面经验丰富,对我来说是一个很好的灵感来源。
    • 很高兴我能帮助你。如果您认为答案很明确,请随时标记它:)
    • 当然,我忘了这样做。
    • 我想添加一个名称而不是客户端 ID。你能帮我解决这个问题吗?
    • 当然,我今天晚些时候会检查一下
    猜你喜欢
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多