How to communicate with JavaScript in the web page in PaletteSet (c # calls or sends data to javascript)

How to communicate with JavaScript in the web page in PaletteSet (c # calls or sends data to javascript)

906521597
Participant Participant
395 Views
0 Replies
Message 1 of 1

How to communicate with JavaScript in the web page in PaletteSet (c # calls or sends data to javascript)

906521597
Participant
Participant

What APIs can control the webview opened in PaletteSet?
Is there any API in C# that can control this webview? For example, the DOM and JavaScript that control it, like Microsoft's webview2, c# can control the webview2 itself and communicate with the JavaScript running in it.

 

 

This is the command to display PaletteSet, and show a web browser ( is it chrome? ) :

 

 

 

 

[CommandMethod( "showWeb" )]
public void showWeb() {
  var ps = new PaletteSet( "pages" ) {
    { "page1", new Uri( "...." ) }
  };
  ps.Visible = true;
  ps.Size = new System.Drawing.Size( 1200, 600 );
}

 

 

 

 

 

This is the command called for JavaScript:

 

 

 

 

[ JavaScriptCallback( "helloWorld" )]
public string helloWorld( string dataFromJavaScript ) {
  var serializer = new JavaScriptSerializer();
  return serializer.Serialize( new Hashtable() {
        { "retCode", 0 },
        { "retValue", "data from c#" }
      });
}

 

 

 

 

 

This is the code for JavaScript to call c#( The execAsync function comes from the global variable (window) in the browser in PaletteSet.):

 

 

 

 

function callNET( fnName, fnParams ) {
  const data = JSON.stringify({
    functionName: fnName,
    invokeAsCommand: false,
    functionParams: fnParams
  })
  
  return new Promise( (resolve, reject) => {
    execAsync(
      data,
      json => {
        const { retCode, retValue } = JSON.parse( json )
        retCode === 0 ? resolve( retValue ) : reject( "err", retCode )
      },
      err => reject( err )
    )
  })
}

 

 

 

 

 

 

There is some JavaScript code in my web page. I know that the JavaScript in the web page can call the method of c # through AutoCAD JavaScript APIs, and can send string data to the c#, and then c# can return the data to JavaScript.
It's normal when JavaScript calls c# actively, but I don't know how c# actively calls the JavaScript function in the web page, or how c# actively sends data to JavaScript, just like websocket, the server actively sends data to browser clients.
I don't want to use a timer to poll c# in JavaScript to get data. I want to know how c# actively sends data to JavaScript in the browser in PaletteSet.

 

[ The subject line of this post has been edited to include the product name by @handjonathan ]

0 Likes
396 Views
0 Replies
Replies (0)