<p>How can I serve dynamic content to the client? I want to serve a raspberry pi's cpu temperature real-time on a website. I have tried using a static file server, but the issue is the server doesn't recognize the file has changed and re-serve it.</p>
<p><a href="https://github.com/Rampagy/go_website" rel="nofollow">Here is my best attempt at making dynamic content work</a>, but it doesn't work and I am not sure what to try next. I am only a few weeks into Go, so any help is appreciated.</p>
<hr/>**评论:**<br/><br/>madman2233: <pre><p>I've started using server side events to push data to the client and plain old javascript XMLHttpRequest to push to the server. Basically, the server never closes the connection and periodically sends data to the client on the open connection. Check out the source code of this for how to do it: <a href="https://github.com/kljensen/golang-html5-sse-example" rel="nofollow">https://github.com/kljensen/golang-html5-sse-example</a></p>
<p>Websockets isn't supported in HTTP2, so i stopped using it and went this route since it is HTTP2 compatible.</p></pre>shovelpost: <pre><p>Again the <a href="https://caniuse.com/#feat=eventsource" rel="nofollow">usual culprits</a>.</p></pre>pharrisee: <pre><p>Melody together with Chi has been my go to method for websockety things with go for a while, it just works:</p>
<p><a href="https://github.com/olahol/melody" rel="nofollow">https://github.com/olahol/melody</a></p>
<p><a href="https://github.com/go-chi/chi" rel="nofollow">https://github.com/go-chi/chi</a></p>
<p>I created a quick example here:</p>
<p><a href="https://gist.github.com/pharrisee/2b04d9b533cbea3bc1cc3aa5f0c8d327" rel="nofollow">https://gist.github.com/pharrisee/2b04d9b533cbea3bc1cc3aa5f0c8d327</a></p></pre>ShallowBoobs: <pre><p>I was wondering if you could explain what the below code does.</p>
<pre><code>r.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
m.HandleRequest(w, r)
})
</code></pre>
<p>It must be related to the below snippet. I tried typing "ws://http://localhost:3000/ws" into my browser, but nothing happened. Is this the channel that it is sending the temperature through?</p>
<pre><code>var url = 'ws://' + window.location.host + '/ws';
</code></pre></pre>pharrisee: <pre><p>that creates the websocket endpoint. That type of URL isn't generally typed into the browser as there are events that need to happen before the socket is useful (upgrading the connection, <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism</a>). All communication will go through that url.</p>
<p>it will create a long-lasting connection.</p></pre>ShallowBoobs: <pre><p>I am having a difficult time integrating this into my current project. If you run the project as is on github, the system page is where I want the live updating numbers. For some reason when I uncomment the new websocket stuff to try out the dynamicness, it no longer serves anything without any compile errors. Do you have any insight as to what it might be?</p>
<p><a href="https://github.com/Rampagy/go_website" rel="nofollow">https://github.com/Rampagy/go_website</a></p></pre>keipra: <pre><p>Generally on request you would generate the HTML content for the response and return it. This is usually done cus templating (most of the page is predefined, some portion is populated at run time).</p>
<p>Check out the docs for text/template and http/template.</p>
<p>Also just try some Google searching related terms.</p></pre>ShallowBoobs: <pre><p>The issue is that there isn't a request. I want to 'push' it to the site every few seconds, but I don't have a value/file to 'push' because I can't get the new value on the server. That's where my issue lies. How can I get this value and only the value to the client every few seconds?</p></pre>niosop: <pre><p>Websockets or a separate http endpoint that returns only the value and poll it via a timer?</p></pre>keipra: <pre><p>You either poll and update the content with JavaScript, use JavaScript to refresh the page, or use websockets.</p></pre>arrans: <pre><p>Have you looked into websockets? <a href="https://godoc.org/golang.org/x/net/websocket" rel="nofollow">https://godoc.org/golang.org/x/net/websocket</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传