Jump to content

Web Test Fixture


Recommended Posts

I have some code I use for test that adds a web interface to my worlds that I thought I would share.

I created a little demo project that moves a cylinder around from a browser and reports on clicked objects.

 
image.thumb.png.a7bc1ed26980ecd82315000f49f40a04.png

I'm still pretty new to Unigine and I'm not a programmer, so no guarantees :).

You should run this from Visual Studio. Sometimes running from the editor doesn't release all resources and you'll need to restart the editor. Also, if you keep your console window open it will hang Unigine. (Just close the console, or "Press any key to continue").

The tester is based on a two channel WebSocket connection. To enable this the project needs to use the Web SDK. The project file has:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>10.0</LangVersion>
    <Nullable>enable</Nullable>
    <RootNamespace>UWeb</RootNamespace>
    <StartupObject>UWeb.Program</StartupObject>
 
  ...

The file "Socket.cs" has the C# webhosting and socket parts.

"Script.Bios.js" has the main browser code. The socket parts are:

self.AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;

//::Fragment:ControlSocket
self.control = new WebSocket(location.origin.replace('http', 'ws') + "/");
control.onopen = () => { control.send(context.name); };
control.onmessage = async (msg) => {
	try { s = await (new AsyncFunction(msg.data))(); control.send(!(s) ? "success" : s); }
	catch (e) { control.send(`Fail: ${e}`); }
};

//::Fragment:TransitSocket
self.transit = new WebSocket(location.origin.replace('http', 'ws') + "/Transit");
transit.onopen = () => { transit.send(context.name); };

The control socket channel executes JavaScript sent from Unigine/App.
The transit channel sends messages from the browser.

The startup code goes in main.

Sockets_ = new(new(root, 5001, WebReceiver, BrowserBios()));
_ = SetForegroundWindow(FindWindow(null, "UWeb"));
Sockets.Control.Say("console.log('success')");

The World part consists of a simple component "Mover".
Mover exposes a Target Port (just a public field here). On frame, the mover checks to see if the Target position is different than the current position. If so, it moves to the new position. When it gets there it sends a notice that is shown in the browser.

The zip contains a project. I've also attached some of the key files.

Reminder that this should be run from Visual Studio, not the editor.

 

 

 

 

TestFixture.zip main.cs Mover.cs Script.bios.js Socket.cs

  • Like 4
Link to comment
  • 10 months later...
×
×
  • Create New...