我使用Kolor Application播放3D电影。此应用程序在UDP端口上发送播放器状态。
任何其他应用程序都可以使用UDP消息。例如,3D音频引擎可以使用这些信息根据Kolor Eyes制作的视频播放产生3D声音。
UDP消息采用JSON格式(http://www.json.org/)。因此,您必须使用JSON解析器来解码消息。
以下是UDP消息的当前结构:
"id": "ked" --- message identifier
"yaw": float --- yaw in radians
"pitch": float --- pitch in radians
"roll": float --- roll in radians
"url": string --- current video url
"state": enum --- playback state, integer possible values are : 0 (StoppedState), 1 (PlayingState), 2 (PausedState)
"position": int --- current video playback position in milliseconds
我创建c#应用程序从UDP端口接收数据并将其转换为ASCII字符串
static void Main(string[] args)
{
int localPort = 7755;
IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);
// Create UDP client
UdpClient client = new UdpClient(localPort);
UdpState state = new UdpState(client, remoteSender);
// Start async receiving
client.BeginReceive(new AsyncCallback(DataReceived), state);
// Wait for any key to terminate application
Console.ReadKey();
client.Close();
}
private static void DataReceived(IAsyncResult ar)
{
UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
// Check sender
bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;
if (isRightHost && isRightPort)
{
string receivedText = Encoding.Default.GetString(receyiveBytes);
Console.WriteLine(receivedText);
}
// Restart listening for udp data packages
c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
}
但是控制台中的输出结果显示错误的结果
qbjs☺ E ☼ ¬ >☻ ☻ id♥ ked ↕♣ ♣ pitch U← position ' ♦ roll
♥ url ) file:///C:/Users/iman/Desktop/FIN_hi2.mp4 '¶ ♥ yaw ♀ ∟
0 @ T ` ~
答案 0 :(得分:0)
KolorEyes发送的消息不是编码为JSON,而是一些二进制衍生物。 可能是Qt Binary Json(qbjs),但我找不到太多关于此的信息。
Qt5在内部将JSON文档转换为二进制表示,这是通过UDP传输的。 http://doc.qt.io/qt-5/qjsondocument.html
这在Qt5应用程序中为我提供了JSON文档:
void MainWindow::initSocket()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::LocalHost, 7755);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}
void MainWindow::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
QJsonDocument document = QJsonDocument::fromBinaryData(datagram);
qDebug() << document;
}
}