Search code examples
c#node.jssocketsrevit

Revit connected and disconnected continuity web server nodejs


I'm trying connect Revit with server nodejs localhost, but when i emit data from server to all clients so nothing happens in Revit and my console visible Revit connected and disconnected continuity web server nodejs.

Code on server localhost:

io2.on('connection', function(client){
  console.log(client.id+" connected");
  client.on("disconnect",function(){
    console.log(client.id + " disconnected !");
  })
  var obj = null;
  client.on("send-data", function(data){
    console.log(data);
    obj = {
      method: 'POST',
      headers: {
      
      'Accept': 'application/json',
      'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    };
    io2.sockets.emit("send-data-from-server", obj);
  })
});
app.get("/", function(req, res){
    res.render("index");
});

image Revit connected and disconnected continuity web server nodejs here

Code test with Revit

namespace TF{
[Transaction(TransactionMode.Manual)]
public class ForgeTestCmd : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData,
        ref string message, ElementSet elements)
    {
        var options = new IO.Options()
        {
            IgnoreServerCertificateValidation = true,
            AutoConnect = true,
            ForceNew = true
        };
        const string _url = "http://localhost:3000";
        Quobject.SocketIoClientDotNet.Client.Socket _socket = IO.Socket(_url, options);
        _socket.On(Quobject.SocketIoClientDotNet.Client.Socket.EVENT_CONNECT, () => { });
        _socket.On("send-data-from-server", data
            =>
        {
            MessageBox.Show(data.ToString(), "connect");
        });
        return Result.Succeeded;
    }
}

}

I don't know why Revit can't listen event emited from server. I also don't know the condition to Revit listen server.

Thank in advanced !


Solution

  • It looks as if you are trying to launch your ForgeTestCmd from outside of Revit.

    That is not possible.

    The Revit API is entirely event driven, and the events are raised by Revit itself and nobody else.

    In the case of an external command, the Execute event is generally triggered by the user clicking the appropriate button in the Revit user interface.

    You can work around this limitation by using an external event.

    Please study The Building Coder articles on External Events for Modeless Access and Driving Revit from Outside in more depth.