简体   繁体   English

使用框架发送短信

[英]Text messaging using a-frame

I want to add a text chat system in a-frame.我想在框架中添加一个文本聊天系统。 I'm using.networked a-frame as connectivity can be anything possible with that or I've to do using some other tech.我正在使用网络框架,因为连接性可以是任何可能的,或者我必须使用其他技术。

There is a simple API to send custom messages with the.network layer that is already setup:有一个简单的 API 可以使用已设置的 .network 层发送自定义消息

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

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

So sending an chat message is as simple as:因此,发送聊天消息非常简单:

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>'
})

Check it out in this glitch : 在这个故障中检查一下:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM