260 lines
8.7 KiB
HTML
260 lines
8.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-us">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<title>Unity WebGL Player | Painter</title>
|
|
<link rel="shortcut icon" href="TemplateData/favicon.ico">
|
|
<link rel="stylesheet" href="TemplateData/style.css">
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="unity-container" class="unity-desktop">
|
|
<canvas id="unity-canvas" width=100% height=100%></canvas>
|
|
<div id="unity-loading-bar">
|
|
<div id="unity-logo"></div>
|
|
<div id="unity-progress-bar-empty">
|
|
<div id="unity-progress-bar-full"></div>
|
|
</div>
|
|
</div>
|
|
<div id="unity-warning"> </div>
|
|
<div id="unity-footer">
|
|
<div id="unity-webgl-logo"></div>
|
|
<div id="unity-fullscreen-button"></div>
|
|
<div id="unity-build-title">Painter</div>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
var myunityInstance;
|
|
var container = document.querySelector("#unity-container");
|
|
var canvas = document.querySelector("#unity-canvas");
|
|
var loadingBar = document.querySelector("#unity-loading-bar");
|
|
var progressBarFull = document.querySelector("#unity-progress-bar-full");
|
|
var fullscreenButton = document.querySelector("#unity-fullscreen-button");
|
|
var warningBanner = document.querySelector("#unity-warning");
|
|
|
|
// Shows a temporary message banner/ribbon for a few seconds, or
|
|
// a permanent error message on top of the canvas if type=='error'.
|
|
// If type=='warning', a yellow highlight color is used.
|
|
// Modify or remove this function to customize the visually presented
|
|
// way that non-critical warnings and error messages are presented to the
|
|
// user.
|
|
function unityShowBanner(msg, type) {
|
|
function updateBannerVisibility() {
|
|
warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
|
|
}
|
|
var div = document.createElement('div');
|
|
div.innerHTML = msg;
|
|
warningBanner.appendChild(div);
|
|
if (type == 'error') div.style = 'background: red; padding: 10px;';
|
|
else {
|
|
if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
|
|
setTimeout(function() {
|
|
warningBanner.removeChild(div);
|
|
updateBannerVisibility();
|
|
}, 5000);
|
|
}
|
|
updateBannerVisibility();
|
|
}
|
|
|
|
var buildUrl = "Build";
|
|
var loaderUrl = buildUrl + "/VideoWeb.loader.js";
|
|
var config = {
|
|
dataUrl: buildUrl + "/VideoWeb.data.unityweb",
|
|
frameworkUrl: buildUrl + "/VideoWeb.framework.js.unityweb",
|
|
codeUrl: buildUrl + "/VideoWeb.wasm.unityweb",
|
|
streamingAssetsUrl: "StreamingAssets",
|
|
companyName: "DefaultCompany",
|
|
productName: "Painter",
|
|
productVersion: "0.1",
|
|
showBanner: unityShowBanner,
|
|
};
|
|
|
|
// By default Unity keeps WebGL canvas render target size matched with
|
|
// the DOM size of the canvas element (scaled by window.devicePixelRatio)
|
|
// Set this to false if you want to decouple this synchronization from
|
|
// happening inside the engine, and you would instead like to size up
|
|
// the canvas DOM size and WebGL render target sizes yourself.
|
|
// config.matchWebGLToCanvasSize = false;
|
|
|
|
if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
|
|
// Mobile device style: fill the whole browser client area with the game canvas:
|
|
|
|
var meta = document.createElement('meta');
|
|
meta.name = 'viewport';
|
|
meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
|
|
document.getElementsByTagName('head')[0].appendChild(meta);
|
|
container.className = "unity-mobile";
|
|
|
|
// To lower canvas resolution on mobile devices to gain some
|
|
// performance, uncomment the following line:
|
|
// config.devicePixelRatio = 1;
|
|
|
|
canvas.style.width = window.innerWidth + 'px';
|
|
canvas.style.height = window.innerHeight + 'px';
|
|
|
|
unityShowBanner('WebGL builds are not supported on mobile devices.');
|
|
} else {
|
|
// Desktop style: Render the game canvas in a window that can be maximized to fullscreen:
|
|
|
|
canvas.style.width = "960px";
|
|
canvas.style.height = "600px";
|
|
}
|
|
|
|
loadingBar.style.display = "block";
|
|
|
|
var script = document.createElement("script");
|
|
script.src = loaderUrl;
|
|
script.onload = () => {
|
|
createUnityInstance(canvas, config, (progress) => {
|
|
progressBarFull.style.width = 100 * progress + "%";
|
|
}).then((unityInstance) => {
|
|
myunityInstance=unityInstance;
|
|
loadingBar.style.display = "none";
|
|
MQTTconnect();
|
|
fullscreenButton.onclick = () => {
|
|
unityInstance.SetFullscreen(1);
|
|
};
|
|
}).catch((message) => {
|
|
alert(message);
|
|
});
|
|
};
|
|
document.body.appendChild(script);
|
|
|
|
|
|
|
|
var mqtt;
|
|
// var host = '49.234.27.18';
|
|
var host='172.16.1.168'
|
|
var port = 10087;
|
|
var subTopic_net="net_server"
|
|
var pubTopic_net="net_client"
|
|
|
|
function myClick(){
|
|
|
|
var jo={'type':'getNet'};
|
|
var raw_message = JSON.stringify(jo);
|
|
message = new Paho.MQTT.Message(raw_message);
|
|
message.destinationName = pubTopic_net;
|
|
console.log('sending message: ' + raw_message );
|
|
mqtt.send(message);
|
|
}
|
|
|
|
function myClick2()
|
|
{
|
|
// var jo={'type':'netplanSet','ethName': 'eth0', 'dhcp4':true, 'addresses':[],'gateway4':'','nameservers':[],'routes':[]};
|
|
var jo={'type': 'netplanSet','ethName': 'eth0','dhcp4':false,'addresses':[],'gateway4':'','nameservers':[],'routes':[]};
|
|
var addrs=['172.16.1.168/24'];
|
|
var dns=['172.16.1.1'];
|
|
// var routes=['172.16.0.0/24,172.16.1.1,100','172.16.10.0/24,172.16.1.10,100'];
|
|
jo.addresses=addrs;
|
|
jo.nameservers=dns;
|
|
//jo.routes=routes;
|
|
|
|
var raw_message = JSON.stringify(jo);
|
|
message = new Paho.MQTT.Message(raw_message);
|
|
message.destinationName = pubTopic_net;
|
|
console.log('sending message: ' + raw_message );
|
|
mqtt.send(message);
|
|
}
|
|
|
|
// onConnect 事件
|
|
function onConnect() {
|
|
console.log('connected.');
|
|
// 订阅 download topic
|
|
var subOptions = {
|
|
qos: 0,
|
|
onSuccess: onSubscribe
|
|
};
|
|
mqtt.subscribe(subTopic_net, subOptions);
|
|
mqtt.subscribe('img0', subOptions);
|
|
mqtt.subscribe('stream0', subOptions);
|
|
// mqtt.subscribe('img1', subOptions);
|
|
// mqtt.subscribe('stream1', subOptions);
|
|
// mqtt.subscribe('img2', subOptions);
|
|
// mqtt.subscribe('stream2', subOptions);
|
|
// mqtt.subscribe('img3', subOptions);
|
|
// mqtt.subscribe('stream3', subOptions);
|
|
// mqtt.subscribe('img4', subOptions);
|
|
// mqtt.subscribe('stream4', subOptions);
|
|
// mqtt.subscribe('img5', subOptions);
|
|
// mqtt.subscribe('stream5', subOptions);
|
|
// mqtt.subscribe('img6', subOptions);
|
|
// mqtt.subscribe('stream6', subOptions);
|
|
}
|
|
|
|
// 订阅主题成功事件
|
|
function onSubscribe(context) {
|
|
console.log('subscribe success');
|
|
console.log(context);
|
|
}
|
|
|
|
// 连接失败事件
|
|
function onFailure(message) {
|
|
console.log('connect failed.');
|
|
}
|
|
|
|
// onMessageArrived 事件
|
|
function onMessageArrived(message) {
|
|
// console.log('new message arrived...');
|
|
console.log(message.payloadString);
|
|
var jo=JSON.parse(message.payloadString);
|
|
if(message.destinationName==subTopic_net)
|
|
{
|
|
//网络
|
|
var type=jo['type'];
|
|
if(type=='getNet')
|
|
{
|
|
var str1=jo['netstates'];
|
|
var str2=jo['netaddres'];
|
|
var jo1=JSON.parse(str1);
|
|
var jo2=JSON.parse(str2);
|
|
}
|
|
}
|
|
else if(message.destinationName=='img0')
|
|
{
|
|
//图片
|
|
// var pic=formatByte2Img(jo['pic']);
|
|
// img.src=pic;
|
|
if(myunityInstance!=null)
|
|
{
|
|
myunityInstance.SendMessage('Canvas', 'Recv', message.payloadString);
|
|
}
|
|
}
|
|
else if(message.destinationName=='stream0')
|
|
{
|
|
//原始数据
|
|
|
|
}
|
|
}
|
|
|
|
// 字节流转图片
|
|
function formatByte2Img(data) {
|
|
let blobUrl = ''
|
|
const bytes = new Uint8Array(data)
|
|
const blob = new Blob([bytes], { type: 'image/png' })
|
|
blobUrl = (window.URL || window.webkitURL).createObjectURL(blob)
|
|
return blobUrl
|
|
}
|
|
|
|
|
|
// 建立 MQTT websocket 连接
|
|
function MQTTconnect() {
|
|
console.log('connecting to ' + host + ':' + port);
|
|
mqtt = new Paho.MQTT.Client(host, port, 'clientidxxx');
|
|
var options = {
|
|
timeout: 3,
|
|
onSuccess: onConnect,
|
|
onFailure: onFailure,
|
|
userName: 'admin',
|
|
password: '123456',
|
|
// mqttVersion: 4
|
|
};
|
|
mqtt.onMessageArrived = onMessageArrived;
|
|
mqtt.connect(options);
|
|
}
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|