UE4 nDisplay外部应用发送ClusterEvent

private int SendClusterCommand(string nodeAddress, int port, string cmd, bool bQuiet = false)
        {
            int ResponseCode = 1;
            TcpClient nodeClient = new TcpClient();

            if (!bQuiet)
            {
                AppLogger.Log(string.Format("Sending command {0} to {1}...", cmd, nodeAddress));
            }

            try
            {
                // Connect to the listener
                nodeClient.Connect(nodeAddress, port);
                NetworkStream networkStream = nodeClient.GetStream();

                byte[] OutData = ASCIIEncoding.ASCII.GetBytes(cmd);
                byte[] OutSize = BitConverter.GetBytes((short)OutData.Length);

                networkStream.Write(OutSize, 0, OutSize.Length);
                networkStream.Write(OutData, 0, OutData.Length);
                AppLogger.Log("Event sent");

                byte[] InLength = new byte[2];
                int InBytesCount = networkStream.Read(InLength, 0, 2);
                AppLogger.Log("Received " + InBytesCount + " bytes");

                int MessageSize = InLength[0] + ((UInt16)InLength[1] << 8);
                byte[] InData = new byte[MessageSize];
                InBytesCount = networkStream.Read(InData, 0, MessageSize);
                AppLogger.Log("Received " + InBytesCount + " bytes");

                string Response = ASCIIEncoding.ASCII.GetString(InData, 0, InBytesCount);
                AppLogger.Log("Response " + Response);
            }
            catch (Exception ex)
            {
                if (!bQuiet)
                {
                    AppLogger.Log("An error occurred while sending a command to " + nodeAddress + ". EXCEPTION: " + ex.Message);
                }
            }
            finally
            {
                nodeClient.Close();
            }

            return ResponseCode;
        }

出自UE4源码。

cmd参数的构造:

{

  "Category" : "",

  "Type" : "",

  "Name" : "",

  "Parameters":{},

}

名称(Name)类型(Type) 和 类别(Category) 字段为必填字段,但可以忽略参数(Parameters)字段。

具体写法可以使用nDisplayLauncher发送一个Cluster Event然后看日志:

 

原文地址:https://www.cnblogs.com/CodeSnippet/p/11375804.html