简体   繁体   English

从HTML调用Golang

[英]Calling Golang from HTML

I'm helping with an open source project. 我正在帮助一个开源项目。 It's a small Go webserver running on a device containing a Raspberry Pi. 这是一个运行在包含Raspberry Pi的设备上的小型Go网络服务器。 I want to be able to have a user click a button on an html screen, which calls a routine in Go, which returns 2 values, a boolean and a string. 我希望能够让用户单击html屏幕上的按钮,该按钮在Go中调用一个例程,该例程返回2个值,一个布尔值和一个字符串。

What we are wanting to do is see which network interfaces are up on the raspberry pi eg is the lan connection up? 我们想要做的是查看树莓派上哪些网络接口可用,例如lan连接是否可用?

To do this I really need to ping a site from each interface. 为此,我确实需要从每个界面ping一个站点。 This takes a few seconds for each of 3 interfaces: Lan, WiFi, and 3G. 三个接口(LAN,WiFi和3G)中的每个接口都需要花费几秒钟的时间。

I can do this when the page is requested and fill in an html template as the page loads, but it means waiting maybe 10 to 15 secs for the page to load, so it seems like something is broken. 我可以在请求页面时执行此操作,并在页面加载时填写html模板,但这意味着可能要等待10到15秒才能加载页面,因此好像有些东西坏了。

So I want to be able to list each of the 3 interfaces on the page and have the user click 'test' which then calls a routine in the underlying Go webserver. 因此,我希望能够列出页面上的3个界面中的每个界面,并让用户单击“测试”,然后在底层Go Web服务器中调用例程。

I then need to be able to display the results from the call in a couple of text areas for each interface. 然后,我需要能够在每个界面的几个文本区域中显示调用的结果。

What I have tried: 我试过的

I have tried registering a Go function (in this case IsLANConnectionUp) using funcmap from the net/html package and calling it from the html template from a JavaScript function, like this: 我尝试使用net / html包中的funcmap注册一个Go函数(在本例中为IsLANConnectionUp),然后从JavaScript函数的html模板中调用它,如下所示:

  <button onclick = "getLANStatus()" class="btn btn-primary">Test</button> <script> function getLANStatus() { var status = document.getElementById('status'); {{ if IsLANConnectionUp }} status.innerHTML = "Lan is up!" {{ else }} status.innerHTML = "Lan is down!" {{ end }} } </script> 

But having the template code inside the javascript code doesn't seem to work. 但是将模板代码包含在javascript代码中似乎不起作用。 Also, I'd like the text output from the ping command (which my Go function getLANStatus and I don't know how to extract that data from the function call. The documentation says only one value can be returned. 另外,我希望从ping命令(我的Go函数getLANStatus和我不知道如何从函数调用中提取数据)输出文本。文档说只能返回一个值。

Searching on StackOverflow I see this: calling Golang functions from within javascript code 在StackOverflow上搜索,我看到了这一点: 从javascript代码中调用Golang函数

 $.ajax({ url: "http://www.example.com/signup", data: {username: "whatever"} //If the request needs any data }).done(function (data) { // Do whatever with returned data }); 

But it says things like "// Do whatever with the returned data" I'm new to web programming and so don't know how to use that code. 但是它说的是“ //对返回的数据做任何事情”之类的东西,这对Web编程来说是新手,因此不知道如何使用该代码。 If this is the way to go, could someone please expand on this a little? 如果这是要走的路,有人可以请您对此进行扩展吗?

Any help would be much appreciated. 任何帮助将非常感激。

So couple different concepts here. 因此,在这里结合不同的概念。

Render: On the initial request to your html that generates the Test button. 呈现:在对您的html的初始请求中,该请求生成Test按钮。 Your go server will render that html 1 time and return it to your browser. 您的Go服务器会将HTML渲染1次,并将其返回到浏览器。 It does not re-request dynamically unless you wire some stuff up to make the web page change. 除非您进行连接以更改网页,否则它不会动态重新请求。

Client: So when someone clicks your button, the function getLANStatus will be ran. 客户端:因此,当有人单击您的按钮时,将运行getLANStatus函数。 You will want that function to do a few things 您将希望该功能执行一些操作

  1. Through ajax, communicate with your go server through an api that will return the status of your connections as a json object. 通过ajax,通过api与go服务器通信,该api将作为json对象返回连接状态。 Something like 就像是

    { "3g": "up", "lan": "down", "wifi": "up" } {“ 3g”:“上”,“ lan”:“下”,“ wifi”:“上”}

  2. Second, in the done part of your ajax, you will manipulate something in the DOM in order to convey that the status of the interfaces is what it is. 其次,在ajax的done部分中,您将操作DOM中的某些内容,以传达接口的状态就是它的状态。 You could do that by finding the element, then changing the text to what is returned by the object. 您可以通过找到元素,然后将文本更改为对象返回的内容来实现。

As a simple first step, you can alert the payload in the function that would look like this 作为简单的第一步,您可以在如下所示的函数中alert有效负载

$.ajax({
  url: "http://YOUR_GO_SERVER_IP_OR_DNS:PORT/interfaces_status.json"
}).done(function (data) {
  alert(data);
  console.log(data);
  debugger;
});

Then if you request that with the console open in chrome, you will be able to directly play with the returned data so that you know what all it reponds to. 然后,如果您要求控制台在chrome中打开,您将可以直接播放返回的data从而知道其所有响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM